Android toolbar not showing overflow menu
Asked Answered
T

6

7

I'm new to android apps and I'm hoping someone with keen eyes can spot what I'm doing wrong. I've tried to follow the steps here, to create a toolbar for my application. However, it's not showing the three dots overflow menu (which I thought was automatic).

Here's what I see:

enter image description here

I'm using minSdkVersion 19.

MainAcivitiy.java

import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;


public class MainActivity extends AppCompatActivity {

@SuppressLint("ShowToast")
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(myToolbar);

layout\main.xml

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true">

<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

menu\main.xml

<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="100"
    android:title="@string/action_settings"
    app:showAsAction="never"
    android:visible="true" />

styles.xml

<resources>
<!--
    Base application theme, dependent on API level. This theme is replaced
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppTheme.Base" parent="Theme.AppCompat.Light">
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

Temikatemp answered 10/1, 2016 at 8:8 Comment(0)
K
18

I Guess you missed this

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
Kocher answered 10/1, 2016 at 8:21 Comment(2)
Even as late as the end of 2016, this essential note is not in the training documentation. Perhaps an update is needed?Africanize
command-line developers insert before: import android.view.Menu;Copilot
L
2

if your phone has a physical menu button you can't see three dots

if you want to force to show three dots do as follow:

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

<item
    android:id="@+id/menu"
    app:showAsAction="always"
    android:icon="@drawable/ic_action_overflow"
    android:visible="true" >
 <menu>
    <item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never"
    android:visible="true" />
 </menu>
</item>
Lithic answered 10/1, 2016 at 8:39 Comment(0)
D
1

Solution: i've tried the following method and i've found the issue.

I think this is refering that theme to Theme.AppCompat.Light that's why this theme has a toolbar.

i just did this and it's working:

<resources>
    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>

    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

Results:

enter image description here

it's working. but,

With your codes and getSupportActionBar();:

enter image description here

Note that our Toolbar has a gray color like this:

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/darker_gray"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

P.s: Also i want to make sure you'll do that perfectly, as doc said, we have to add this to Manifest:

<application
    android:theme="@style/Theme.AppCompat.Light.NoActionBar"
    />

and seems like it's not working or we've lost something.BTW, i didn't do that on my manifest and that fixed without above code on manifest.

Dipsomania answered 10/1, 2016 at 8:57 Comment(0)
L
1

Call setHasOptionsMenu(true) in onCreateView.

In case you are inflating your menu from a fragment, e.g. by overriding onCreateOptionsMenu method.

Leffen answered 11/10, 2020 at 10:56 Comment(0)
R
0

I had the same problem. I tried everything here and here, and nothing helped. Everything was just like it should be. After whole day of investigating, I found this statement in my onCreate method:

toolbar.setVisibility(View.INVISIBLE);

When I removed it, geuss what happend: 3 dots menu appears now, just like it should.

Rusert answered 13/2, 2018 at 11:22 Comment(0)
L
0

After following all the answer's suggestions, the overflow button was still not showing. This is how I solved it:

  • Make sure to override both methods "onPrepareOptionsMenu" and "onCreateOptionsMenu" in your activity
  • Return always true.

Check return comments for this methods on Activity.java :

@return You must return true for the menu to be displayed, if you return false it will not be shown.

Lorylose answered 29/5, 2018 at 11:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.