In android, there are three types of Menus available to define a set of options and actions in our android applications.
The Menus in android applications are the following:
- Android Options Menu
- Android Context Menu
- Android Popup Menu
Android Option Menus are the primary menus of android. They can be used for settings, search, delete item etc. When and how this item should appear as an action item in the app bar is decided by the Show Action attribute. The values that can be given for the showAsAction attribute:
- always: This ensures that the menu will always show in the action bar.
Syntax:
app:showAsAction="always"
Example:
<
item
android:id
=
"@+id/message"
android:icon
=
"@android:drawable/ic_menu_send"
app:showAsAction
=
"always"
android:title
=
"message"
/>
chevron_rightfilter_none - never: This means that the menu will never show, and therefore will be available through the overflow menu
Syntax:
app:showAsAction="never"
Example:
<item
android:id=
"@+id/exit"
app:showAsAction=
"never"
android:title=
"exit"
/>
chevron_rightfilter_none
Below is the complete code for implementing the Options Menu in Android is given below:
activity_main.xml
< menu tools:context = ".MainActivity" > < item android:id = "@+id/message" android:icon = "@android:drawable/ic_menu_send" app:showAsAction = "always" android:title = "message" /> < item android:id = "@+id/picture" android:icon = "@android:drawable/ic_menu_gallery" app:showAsAction = "always|withText" android:title = "picture" /> < item android:id = "@+id/mode" android:icon = "@android:drawable/ic_menu_call" app:showAsAction = "always" android:title = "mode" /> < item android:id = "@+id/about" android:icon = "@android:drawable/ic_dialog_info" app:showAsAction = "never|withText" android:title = "calculator" /> < item android:id = "@+id/exit" app:showAsAction = "never" android:title = "exit" /> </ menu > |
MainActivity.java
package com.example.menu; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; import static android.widget.Toast.LENGTH_LONG; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); } public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu, menu); return true ; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.message: Toast .makeText( getApplicationContext(), "Shows share icon" , Toast.LENGTH_SHORT) .show(); return true ; case R.id.picture: Toast .makeText( getApplicationContext(), "Shows image icon" , Toast.LENGTH_SHORT) .show(); startActivity(i2); return ( true ); case R.id.mode: Toast .makeText( getApplicationContext(), "Shows call icon" , Toast.LENGTH_SHORT) .show(); return ( true ); case R.id.about: Toast .makeText( getApplicationContext(), "calculator menu" , Toast.LENGTH_SHORT) .show(); return ( true ); case R.id.exit: finish(); return ( true ); } return ( super .onOptionsItemSelected(item)); } } |
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.