setSupportActionBar toolbar cannot be applied to (android.widget.Toolbar) error
Solution 1
In your Activity.java
import android.support.v7.widget.Toolbar
instead of android.widget.Toolbar
:
import android.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.widget.Toolbar;
public class rutaActivity extends AppCompactActivity {
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); //NO PROBLEM !!!!
Update:
If you are using androidx, replace
import android.support.v7.widget.Toolbar;
import android.support.v7.app.AppCompatActivity;
with newer imports
import androidx.appcompat.widget.Toolbar;
import androidx.appcompat.app.AppCompatActivity;
Solution 2
In using toolbar you should extends AppCompatActivity and
import android.support.v7.widget.Toolbar
Solution 3
For adding a ToolBar that supports Material Design, the official documentation directions are probably the best to follow.
- Add the v7 appcompat support library.
-
Make your activity extend
AppCompatActivity
.public class MyActivity extends AppCompatActivity { // ... }
-
Declare
NoActionBar
in the Manifest.<application android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
-
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" ... />
-
Call
setSupportActionBar
in the activity'sonCreate
.@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;
Solution 4
For Android version above 3:
import androidx.appcompat.widget.Toolbar;
For Android version below 3:
import android.widget.Toolbar;
Solution 5
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
HGRC
Updated on July 09, 2022Comments
-
HGRC almost 2 years
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?
-
nyxaria over 8 yearsUse
AppCompatActivity
instead ofActionBarActivity
as Google has depreciated it. -
Harsha over 8 yearswhen i use toolbar in android class extens actionbaractivity setsupportActionBar(tool) showing error setsupport toolbar v7 appdelegar error please help me
-
Francisco Romero over 7 yearsExtends
AppCompatActivity
gave me the clue I was missing. -
Mike Miller over 6 yearsForgot to extend AppCompatActivity and was getting an Unresolved Reference on setSupportActionBar. Thanks!
-
Ümañg ßürmån over 5 yearsThis will not help.
-
trustidkid over 5 yearsIt tried it and it works for me. take note of the import statement import android.support.v7.widget.Toolbar and the toolbar <android.support.v7.widget.Toolbar.
-
Admin about 5 years@AbhinavSaxena stop spamming other people's answers with a link to another answer.
-
most venerable sir almost 5 yearsThis is genius!
-
Rushabh Gedam almost 4 yearsIt's most sophisticated way I've ever found, did it helped to u ?