Reputation: 11341
I read different post, different things and never find the answer. I try to create a simple menu with "..." in the right top corner of my main AppCompatActivity.
I create my XML
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item android:title="Settings"
android:icon="@mipmap/ic_settings" android:id="@+id/MnuSettings" />
</menu>
I am not able to add this menu to my main AppCompatActivity. I try different thing.
I do in my main "layout activity"
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
I try include in my main layout ...
I sure we can add the menu to all the layout but i don't know how and find nothing.
Can somebody help me please.
Did i need to create something in the manifest.xml for the menu. Did i need to add something in the xml of my layout file ... never find the answer .. :-(
Upvotes: 1
Views: 86
Reputation: 11341
I found what I was searching for in the tutorial bellow.
I was missing the toolbar in my main layout folder
<android.support.v7.widget.Toolbar
android:id="@+id/SettingToolBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" android:elevation="4dp" android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/AlertDialog.AppCompat.Light">
After that, I needed this in my OnCreate of my main Activity
Toolbar toolbar = (Toolbar)findViewById(R.id.SettingToolBar);
setSupportActionBar(toolbar);
Lastly
onCreateOptionsMenu(Menu menu)
will be called.
Tutorial.
https://developer.android.com/training/appbar/setting-up.html
Upvotes: 1
Reputation: 6704
Suppose your menu xml called menu_main
. Change it to following,
<item android:title="Settings"
android:icon="@mipmap/ic_settings"
android:id="@+id/MnuSettings"
android:title="Settings"
app:showAsAction="never"/>
Then change onCreateOptionsMenu to this
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
Upvotes: 2