This is the XML for the Toolbar layout...
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.Toolbar>
I'm using the support library to use a Toolbar as my ActionBar. I did this in onCreate...
Toolbar mToolbar = (Toolbar)getLayoutInflater().inflate(R.layout.toolbar, null);
setActionBar(mToolbar);
But if gives me the red squigglies and tells me that message in the title. I'm like, LULWUT?!
To use the ActionBar utility methods, call the activity's getSupportActionBar() method. This method returns a reference to an appcompat ActionBar object. Once you have that reference, you can call any of the ActionBar methods to adjust the app bar. For example, to hide the app bar, call ActionBar.
Right-click on the res folder and selects New -> Directory. Give the name “menu” to the new directory. Further, create a new Menu Resource File by right click on the menu directory. As the ActionBar is being created for the main Activity, type the name as “main” to the Menu Resource File.
androidx.appcompat.widget.Toolbar. A standard toolbar for use within application content. A Toolbar is a generalization of action bars for use within application layouts.
You probably need setSupportActionBar
instead. Using the Toolbar
means you should be using AppCompatActivity
, and all this stuff lives in the appcompat (~support) library.
Edit: this answer still applies if you are using the now-deprecated ActionBarActivity
, since ActionBarActivity
inherits the setSupportActionBar
method from AppCompactActivity
.
2019 UPDATE
According to API 29, Toolbar is now in package androidx.appcompat.widget
. So, in the layout file:
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"/>
In Java code:
import androidx.appcompat.widget.Toolbar;
...
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With