Rapid Subscribe Android App

Rapid Subscribe Android App
Rapid Subscriber

Recent Posts

How to create OptionsMenu in Android



In this post, we will create an OptionsMenu for Android (3 dots menu) to handle immediate actions in our app.

OptionsMenu normally has WebView Reload button, BookMark Button, etc.

To create OptionsMenu we will create a menu resource directory.

For that, right click on the res folder and create a menu directory


Then create a menu resource file

For that, right click on menu directory which you have created and click new--> menu resource file.


In your menu.xml file create your first item.
 <?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/reload"  
     app:showAsAction="always"  
     android:title="Reload"  
     android:orderInCategory="100"  
     android:icon="@drawable/ic_refresh_layer"  
     />  
 </menu>  



In your activity file, create an override method called, onCreateOptionsMenu, after activity.onCreate
 @Override  
   public boolean onCreateOptionsMenu(Menu menu) {  
     getMenuInflater().inflate(R.menu.menu_main,menu);  
     return super.onCreateOptionsMenu;  
   }  


To handle actions on your menu button, create an override method called, onOptionsItemSelected after onCreateOptionsMenu.
 @Override  
   public boolean onOptionsItemSelected(MenuItem item) {  
   
     int id = item.getItemId();   
     if(id==R.id.reload)  
     {  
       mywebview.reload();  
     }  
     
     return super.onOptionsItemSelected(item);  
   }  

Follow us for more posts like this, 
Subscribe Harpreet studio on Youtube  
Like Harpreet Studio on Facebook  
Follow me on Instagram 

No comments