How to add a button in Action Bar?

Asked

Viewed 2,271 times

0

I would like to put only one button in the Action Bar.

follows an example image of the internet.

imagem da internet.

1 answer

3


You need to add a method called onCreateOptionsMenu, example:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   getMenuInflater().inflate(R.menu.main, menu);
   return true;
}

Inside the directory res, you must create the directory menu, and within the directory menu, you create the file main.xml.

Follow an example of the XML menu:

<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context=".MainActivity">

    <item 
        android:id="@+id/action_back" 
        android:icon="@drawable/ic_back"
        android:orderInCategory="100" 
        app:showAsAction="always" />

</menu>

Each item which you add in XML, will be a button in your Actionbar.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.