Why is the menu not being added to the Toolbar in Android?

crmepham :

I am attempting to add a simple menu to my Toolbar, but it is not displaying.

Here is my Activity:

package com.github.crmepham.bluetoothmessenger.activities

import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import android.widget.Toolbar
import androidx.appcompat.app.AppCompatActivity


class ConversationsActivity : AppCompatActivity() {

    private var mTopToolbar: Toolbar? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(com.github.crmepham.bluetoothmessenger.R.layout.conversation_list)
        mTopToolbar = findViewById(com.github.crmepham.bluetoothmessenger.R.id.my_toolbar)
        setActionBar(mTopToolbar)
    }

    override fun onPrepareOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(com.github.crmepham.bluetoothmessenger.R.menu.main_menu, menu)

        return super.onCreateOptionsMenu(menu)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(com.github.crmepham.bluetoothmessenger.R.menu.main_menu, menu)
        super.onCreateOptionsMenu(menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.getItemId()

        if (id == com.github.crmepham.bluetoothmessenger.R.id.action_favorite) {
            Toast.makeText(this@ConversationsActivity, "Action clicked", Toast.LENGTH_LONG).show()
            return true
        }

        return super.onOptionsItemSelected(item)
    }
}

The conversation_list.xml layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    style="@style/HeaderBar"
    android:elevation="4dp" />

</RelativeLayout>

The main_menu.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"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.github.crmepham.bluetoothmessenger.activities.ConversationsActivity">

    <item
        android:id="@+id/action_favorite"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:orderInCategory="300"
        android:title="@string/app_name"
        android:visible="true"
        app:showAsAction="always"></item>

</menu>

and the build.gradle file:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.github.crmepham.bluetoothmessenger"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.0.0'
}

Could someone help me understand why the menu is not getting added to the Toolbar?

Mike M. :

The android.widget.Toolbar class and the setActionBar() method are from the native framework. The androidx AppCompatActivity, and its options menu methods, will deal only with the androidx.appcompat.widget.Toolbar class, and that needs to be set as the support ActionBar, with the corresponding setSupportActionBar() method.

You will also need to use the fully-qualified class name in the Toolbar's layout element. That is, change it to <androidx.appcompat.widget.Toolbar>.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=161516&siteId=1