Home Ask Login Register

Developers Planet

Your answer is one click away!

Ambu February 2016

Toolbar items shown only in activity_main

I've got menu_main.xml that works ONLY for activity_main.xml. How to make menu_main.xml works elswhere than only in one activity? When I change content view to another layout the toolbar items from menu_main disappears and leaves me with empty app_bar.

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=".Przepisy">

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

</menu>

main.java:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_przepisy, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }


        return super.onOptionsItemSelected(item);
    }

AndroidMainfest.xml:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".Przepisy"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>        

Answers


v4_adi February 2016

You need to add
getMenuInflater().inflate(R.menu.menu_przepisy, menu); return true; to all activities where you want this menu. Inside onCreateOptionsMenu(Menu menu) method.

Post Status

Asked in February 2016
Viewed 2,493 times
Voted 8
Answered 1 times

Search




Leave an answer


Quote of the day: live life