Skip to main content

Creating Options Menu with Sub Items - Android Studio Tutorial


Step 1

  1. Go to res -> drawable -> New -> Vector Asset -> Select Menu Icon -> Give it suitable name
  2. res -> new -> Android Resource Directory 
    1. Select Resource Type = Menu
    2. Give a suitable name, we used topnav_menu
  3.  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

Popular posts from this blog

How to enable pinch zoom on a web view loading a url with pdf(Android)?

How to enable pinch zoom on a web view loading a url with pdf(Android)? Ref: https://stackoverflow.com/questions/16630382/how-to-enable-pinch-zoom-on-a-web-view-loading-a-url-with-pdfandroid   WebView mWebView = (WebView) findViewById(R.id.webView);         mWebView.getSettings().setJavaScriptEnabled( true ); WebSettings webSettings = mWebView.getSettings(); webSettings.setBuiltInZoomControls( true ); webSettings.setSupportZoom( true );

[SOLVED] Is Android Studio supported on Mac OS 10.15 (Catalina)? Android Studio Not Working on MAC

 Technically Yes! But not that all versions work the same way. While I upgraded to Android Studio to Version 4.1.2. But it did not work in OS 10.15. I deleted my previous android installations following this guide , but still nothing seemed to work. The hell bent installer seemed to get stuck in the middle. I contacted few experts who also appear clueless. A lot of time in my developers' life, I prompt to self - "Read Sid, Read!'. This was no different this time. Instead of searching for help online / offline, I started reading the Android Studio Release Note for version 4.1.2 . It clearly says ,  System Requirements  For MAC:  Mac® OS X® 10.10 (Yosemite) or higher, up to 10.14 (macOS Mojave) So, it may not work with MAC OS Catalina anyway. And it did not work as in my case. Strangely, my previous Android Version 4.0 worked. It is bit tricky to find old releases. But here is where you can find old Android Studio Archives . I downloaded Android Version 4.0, and it wor...

How to Align Views at The Bottom of The Screen

This is h ow you can Align Views at The Bottom of The Screen You know, you have to go to layout_main.xml file (If this is the layout you want to tweak with). Here we add a LinearLayout Component. Then place a TextView Component within that liner layout. Most important note: Add android:gravity="bottom" in the LinearLayout Component Add  android :gravity ="center_horizontal" in the TextView Component You are done! Here goes your SAMPLE code. < LinearLayout android :layout_width ="match_parent" android :layout_height ="match_parent" android :gravity ="bottom" android :orientation ="horizontal" > < TextView android :id ="@+id/bottomText" android :layout_width ="match_parent" android :padding ="2dp" android :textSize ="18sp" android :layout_height ="wrap_content" android :background ="@color/cardview_...