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, searching, deleting items, 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:
XML
< item android:id = "@+id/message" android:icon = "@android:drawable/ic_menu_send" app:showAsAction = "always" android:title = "message" /> |
never: This means that the menu will never show, and therefore will be available through the overflow menu Syntax:
app:showAsAction="never"
Example:
XML
< item android:id = "@+id/exit" app:showAsAction = "never" android:title = "exit" /> |
Below is the complete code for implementing the Options Menu in Android is given below:
XML
tools:context = ".MainActivity" > < item android:id = "@+id/message" android:icon = "@android:drawable/ic_menu_send" android:title = "message" app:showAsAction = "always" /> < item android:id = "@+id/picture" android:icon = "@android:drawable/ic_menu_gallery" android:title = "picture" app:showAsAction = "always|withText" /> < item android:id = "@+id/mode" android:icon = "@android:drawable/ic_menu_call" android:title = "mode" app:showAsAction = "always" /> < item android:id = "@+id/about" android:icon = "@android:drawable/ic_dialog_info" android:title = "calculator" app:showAsAction = "never|withText" /> < item android:id = "@+id/exit" android:title = "exit" app:showAsAction = "never" /> </ menu > |
Java
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)); } } |
Kotlin
import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.widget.Toast import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.main) } override fun onCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.menu, menu) return true } override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.message -> { Toast.makeText(applicationContext, "Shows share icon" , Toast.LENGTH_SHORT).show() return true } R.id.picture -> { Toast.makeText(applicationContext, "Shows image icon" , Toast.LENGTH_SHORT).show() startActivity(SPARC.i2) return true } R.id.mode -> { Toast.makeText(applicationContext, "Shows call icon" , Toast.LENGTH_SHORT).show() return true } R.id.about -> { Toast.makeText(applicationContext, "calculator menu" , Toast.LENGTH_SHORT).show() return true } R.id.exit -> { finish() return true } } return super .onOptionsItemSelected(item) } } |