Search code examples
javaandroidandroid-toolbarandroid-sdk-2.3

Menu items not showing inToolbar SDK 29


The issue I am having is the menu buttons are not showing in the toolbar. I set one button to display always, and one to only display if room. None are showing.

This was compiled with Android SDK 29 (Q).

(I have scoured similar questions on Stack Overflow, but other issues were written at least a few years ago and are using older SDKs.)

enter image description here

activity_main.xml

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

    <androidx.constraintlayout.widget.ConstraintLayout 
    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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/tealBlue"
    tools:context=".view.MainActivity">

    <androidx.appcompat.widget.Toolbar
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/toolbar_top_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        tools:menu="@menu/top_menu_overflow"
        />

    <ExpandableListView
        android:id="@+id/expandableListViewPolls"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:indicatorLeft="? 
   android:attr/expandableListPreferredItemIndicatorLeft"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/toolbar_top_main"

        tools:layout_editor_absoluteX="16dp" />
    ...

top_menu_overflow.xml

<?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_start_voting"
    android:title="@string/start_voting_text"
    app:showAsAction="always"/>
<item android:id="@+id/action_see_voting_results"
    android:title="@string/see_voting_results_text"
    app:showAsAction="ifRoom"/>
</menu>

MainActivity.java

   import androidx.appcompat.widget.Toolbar;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            // setup toolbar
            Toolbar myToolbar = findViewById(R.id.toolbar_top_main);
            setSupportActionBar(myToolbar);
        }

styles.xml

   <resources>
           <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        ...

AndroidManifest.xml

  <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        package="com.example.votetodo">

        <application
            android:allowBackup="false"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme"
        ...

build.gradle

apply plugin: 'com.android.application'

    android {
    compileSdkVersion 29
    buildToolsVersion "29.0.1"
    defaultConfig {
        applicationId "com.example.votetodo"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true


        javaCompileOptions {
            annotationProcessorOptions {
                arguments = ["room.schemaLocation": 
    "$projectDir/schemas".toString()]
            }
        }

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    dataBinding {
        enabled = true
    }
    }

    dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'


    implementation 'com.google.android.material:material:1.0.0'

    // room
    def room_version = "2.2.0-alpha01"  
    implementation "androidx.room:room-rxjava2:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"


    // rxandroid
    implementation "io.reactivex.rxjava2:rxandroid:2.0.1"

    }

Solution

  • Found the issue. I needed to add the following method in my MainActivity.java in order to inflate the menu items.

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