1

I want use custom toolbar because of title and subtitles but I also want to add overflow menu and I did everything and my toolbar doesn't showing overflow menu what I'm suppose to do

Main Activity Layout xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BluetoothMain"
    android:orientation="vertical"
    tools:menu="@menu/bluetooth_overflow_menu">

<include
        layout="@layout/bluetooth_toolbar" 
        android:visibility="visible"/>


</LinearLayout>

Custom action bar layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar 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:id="@+id/BluetoothToolBar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/yellowGreenColorWheel"
    android:elevation="4dp"
    android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:title="Bluetooth"
    app:subtitle="Not Connected"
    tools:menu="@menu/bluetooth_overflow_menu">

</androidx.appcompat.widget.Toolbar>

Overflow menu, here I add Bluetooth icon and Action setting

<?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_favorite"
        android:icon="@drawable/bluetooth_white_icon"
        android:title="action_favorite"
        app:showAsAction="ifRoom"/>

    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
        android:title="Settings"
        app:showAsAction="never"/>

</menu>

Build Gradle File minSdk = 28, tergetSdk = 32

android {
    compileSdk 32
    defaultConfig {
        applicationId "com.indiebloog.IndieNode"
        minSdk 28
        targetSdk 32
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    namespace 'com.indiebloog.IndieNode'
}
indiebloog
  • 11
  • 3
  • 1
    The `tools` prefix is for things that you only want to see at design time. If you want that menu to be created from that attribute at runtime, change to the `app` prefix: `app:menu="@menu/bluetooth_overflow_menu"`. Alternatively, you could handle it in code with `toolbar.inflateMenu(R.menu.bluetooth_overflow_menu)`, which is what that attribute ends up doing internally. – Mike M. Oct 11 '22 at 12:45
  • 1
    That attribute should be on the ``, btw, not the ``. – Mike M. Oct 11 '22 at 16:57
  • @MikeM. you should put this as an accepted answer. It worked for me. +1. – CodeNovice Jun 07 '23 at 09:45
  • @CodeNovice Oh, I'm _waaay_ too lazy for that, these days. :-) Please feel free to post one yourself, if you'd want to. Thanks, though. Glad I could help. Cheers! – Mike M. Jun 09 '23 at 17:17

0 Answers0