Search code examples
javaandroiddialogandroid-actionbar

Default ActionBar with Settings button


Although I am new in android developing, lately, I have been trying to add a settings button in the default ActionBar that Android Studio provides (as shown in the picture below). However, all the tutorials I have stamped on start with changing the app Theme to: android:theme="@style/Theme.APpCompat.Light.NoActionBar"

Is there any way I can avoid changing Theme and just add that setting about in the default ActionBar?

ActionBar that Android documentation suggests


Solution

  • You can do this by inflating your menu to the toolbar. Override this method in your java class.

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

    You custom menu should look something like this:

    <?xml version="1.0" encoding="utf-8"?>
    

    >
    <item
        android:id="@+id/night_mode"
        android:title="?attr/theme_text"
        app:showAsAction="never"
        android:icon="?attr/theme_icon"/>
    <item
        android:id="@+id/action_about"
        android:title="@string/about_text"
        app:showAsAction="never"
        android:icon="?attr/about_icon"/>
    <item
        android:id="@+id/action_logout"
        android:title="@string/action_logout_text"
        app:showAsAction="never"
        android:icon="?attr/logout_icon"/>
    

    Here's something more about menus: https://developer.android.com/guide/topics/ui/menus