Belphegor
Belphegor

Reputation: 1843

Unable to add action buttons to my action bar in android studio

I am currently closely following the steps on the website

https://developer.android.com/training/basics/actionbar/adding-buttons.html and at this stage of the tutorial it wants me to copy and paste the code to add an action search and action settings. However, when I run my application, the action search doesn't want to appear.

I have also made sure to include a .png for the icon of the action search, but still won't show. enter image description here

I have also tried changing the minimumsdk version in my build.gradle from 8 to 11 as suggested by the website, but didn't work either. However, if I am not mistaken, the action bar is present in the app though since the overflow is there.

From my wild guess, it might be that the code is outdated since I have noticed a lot of things have changed since this tutorial was written. But I am still clueless about this weird problem.

Upvotes: 1

Views: 878

Answers (2)

Priya Singhal
Priya Singhal

Reputation: 1291

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

<item
    android:id="@+id/action_settings"
    android:orderInCategory="1"
    app:showAsAction="always"
    android:icon="@drawable/ic_action_settings"
    android:title="@string/action_settings"/>
<item
    android:id="@+id/volume"
    android:orderInCategory="2"
    android:title="Volume"
    android:icon="@drawable/ic_action_volume_on"
    app:showAsAction="always"/>

You need to xmlns referencing res-auto and then use it as I have used in my code. Hope this helps.

Upvotes: 1

Arslan Ahmed
Arslan Ahmed

Reputation: 288

you have to create an entry inside res/menu,override onCreateOptionsMenu and inflate it

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.yourentry, menu);
    return true;
}


<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/action_cart"
        android:icon="@drawable/cart"
        android:orderInCategory="100"
        android:showAsAction="always"/> 
</menu>

Upvotes: 4

Related Questions