Search code examples
androidandroid-actionbarandroid-optionsmenu

Overflow menu's 3 dots are not showing up in ActionBar


I am new to android and I am trying to create an overflow menu.

The 3 dots on the ActionBar are not showing (even after changing the app theme). After changing the app theme, it gets shown on the center of the screen "Android..CoordinatorLayout" how to get these 3 dots?

My menu_main.xml:

<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_settings" 
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="always" />

    <item android:id="@+id/rtfm" 
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />
</menu>

Solution

  • You should need something like this.

    A menu.xml layout for the items under the res/menu directory.

    <?xml version="1.0" encoding="utf-8"?>
    <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:title="@string/action_settings"
            app:showAsAction="never" />
        ...
    </menu>
    

    Then in your Activity you must include this methods.

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
    
        return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                // Do something
    
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }