I'm new to Android and I've been trying to add a simple add button as mentioned below
list_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_insert"
android:icon="@android:drawable/ic_menu_add"
android:title="@string/menu_insert"
/>
</menu>
MyActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.list_menu, menu);
return true;
}
I read in Dummies series book that ic_menu_add is already there in resources and I don't need to add it, but when I run this code it does not display. I've tried to add a custom icon with same name still there is no button. Can someone help me with it please.
This is going to be in res/menu/main_menu . Right click the res folder and choose New > Android Resource File. Type main_menu for the File name. Choose Menu for the Resource type.
In Android, an Option Menu is a set of primary options of an application which users can select one of the options to perform an action. The Option Menu appears on the right side of the App Bar.
If you use a fragment then you need this in onCreate():
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With