Step 1
- Go to res -> drawable -> New -> Vector Asset -> Select Menu Icon -> Give it suitable name
- res -> new -> Android Resource Directory
- Select Resource Type = Menu
- Give a suitable name, we used topnav_menu
- Add Menu Items to topnav_menu
<?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/item1"
android:icon="@drawable/ic_right_arrow"
android:title="Item 1"
app:showAsAction="never">
</item>
<item android:id="@+id/item2"
android:icon="@drawable/ic_right_arrow"
android:title="Item 2"
app:showAsAction="never">
</item>
<item android:id="@+id/item3"
android:icon="@drawable/ic_right_arrow"
android:title="Item 3"
app:showAsAction="never">
</item>
</menu>
4. Add following functions in MainActivity.java file
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.topnav_menu, menu);
return true; /* super.onCreateOptionsMenu(menu); */
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(this,"Item 1 Selected", Toast.LENGTH_SHORT ).show();
return true;
case R.id.item2:
Toast.makeText(this,"Item 2 Selected", Toast.LENGTH_SHORT ).show();
return true;
case R.id.item3:
Toast.makeText(this,"Item 3 Selected", Toast.LENGTH_SHORT ).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}5. Build & Run your App.6. You are done.
Here is the complete Video Guide on Creating Menu.
Comments
Post a Comment