[android] setSupportActionBar toolbar cannot be applied to (android.widget.Toolbar) error

I've been looking for an answer and I've tried many possible solutions, but nothing seems to work..

I'm trying to setup a Material Action Bar following this tutorial.

Here's my code:

tool_bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorPrimary"
    android:elevation="4dp">

</android.support.v7.widget.Toolbar>

activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">
    <!-- The main content view -->
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/app_bar"
            layout="@layout/tool_bar" />
    </RelativeLayout>
    <!-- Navigation Drawer -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="220dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#1C1C1C"
        android:divider="@android:color/darker_gray"
        android:dividerHeight="1dp" />

</android.support.v4.widget.DrawerLayout>

And finally my activity.java:

import android.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toolbar;


public class rutaActivity extends ActionBarActivity {

private Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ruta);

        getSupportActionBar().hide();//Ocultar ActivityBar anterior

        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar); //HERE'S THE PROBLEM !!!!

Error:

setSupporActionBar (android.support.v7.widget.Toolbar) in ActionBarActivity cannot be applied to (android.widget.Toolbar)

How can I fix this?

This question is related to android android-actionbar material-design toolbar

The answer is


Certify that your Manifest declaration includes android:theme="@style/AppTheme.NoActionBar" tag, like the following:

<activity
    android:name=".PointsScreen"
    android:theme="@style/AppTheme.NoActionBar">
</activity>

In You MainActivity.java import android .support.v7.widget.Toolbar insert of java program


For Android version above 3:

import androidx.appcompat.widget.Toolbar;

For Android version below 3:

import android.widget.Toolbar;

I was using previously this code:

Toolbar toolbar =  findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

and extended AppCompatActivity also but I was getting the same error. So in place of using Toolbar class, I have imported the below class and it worked for me:

android.support.v7.widget.Toolbar

With the addition of androidx in Studio 3.0+ the Toolbar compatibility is now in a new library, accessible like this

import androidx.appcompat.widget.Toolbar

class must be extend of AppCompatActivity to resolve the problem of setSuppertActionBar that is not recognizable


import android.support.v7.widget.Toolbar;

import it into your java class


ToolBar's name can easily be changed using

android:label="My Activity"

in your Manifest File. I just going through Manifest & found

android:label 

Helps to change according to the specific activity, Hope you'll give it a try


For adding a ToolBar that supports Material Design, the official documentation directions are probably the best to follow.

  1. Add the v7 appcompat support library.
  2. Make your activity extend AppCompatActivity.

    public class MyActivity extends AppCompatActivity {
      // ...
    }
    
  3. Declare NoActionBar in the Manifest.

    <application
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"
        />
    
  4. Add a toolbar to your activity's xml layout.

    <android.support.v7.widget.Toolbar
       android:id="@+id/my_toolbar"
       android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
       ...
       />
    
  5. Call setSupportActionBar in the activity's onCreate.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
    }
    

Note: You will have to import the following in the activity.

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

In using toolbar you should extends AppCompatActivity and

import android.support.v7.widget.Toolbar 

Adding import android.support.v7.widget.Toolbar to the import list resolve this issue.

Then add the toolbar widget layout file:

<android.support.v7.widget.Toolbar
    android:id="@+id/list_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/actionBarTheme"
    />

In onCreate method of java code

//call to

Tootbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar);

Source: https://developer.android.com/training/appbar/up-action


Just change

import android.widget.Toolbar;

To

import android.support.v7.widget.Toolbar;

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-actionbar

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? Failed to load AppCompat ActionBar with unknown error in android studio Android transparent status bar and actionbar ActionBarActivity is deprecated Manage toolbar's navigation and back button from fragment in android setSupportActionBar toolbar cannot be applied to (android.widget.Toolbar) error How to use SearchView in Toolbar Android How to get Toolbar from fragment? Display Back Arrow on Toolbar Remove title in Toolbar in appcompat-v7

Examples related to material-design

Flutter: Setting the height of the AppBar How to use the new Material Design Icon themes: Outlined, Rounded, Two-Tone and Sharp? Selected tab's color in Bottom Navigation View Material UI and Grid system How to set a ripple effect on textview or imageview on Android? How can a divider line be added in an Android RecyclerView? How to show Snackbar when Activity starts? How to change the new TabLayout indicator color and height Android Design Support Library expandable Floating Action Button(FAB) menu Android Support Design TabLayout: Gravity Center and Mode Scrollable

Examples related to toolbar

setSupportActionBar toolbar cannot be applied to (android.widget.Toolbar) error How to change Toolbar home icon color Creating a SearchView that looks like the material design guidelines How to set Toolbar text and back arrow color How do you set the title color for the new Toolbar? In android app Toolbar.setTitle method has no effect – application name is shown as title How do I use DrawerLayout to display over the ActionBar/Toolbar and under the status bar?