MainActivity.java
package com.cfsuman.androidtutorials;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the widgets reference from XML layout
mTextView = findViewById(R.id.textView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.toolbar_options_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
String title = item.getTitle().toString();
if (id == R.id.delete) {
mTextView.append("\n" + title + " Clicked.");
return true;
} else if (id == R.id.copy) {
mTextView.append("\n" + title + " Clicked.");
return true;
} else if (id == R.id.paste) {
mTextView.append("\n" + title + " Clicked.");
return true;
}else {
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu){
// Add new Menu Item
// Specify an id for the new menu item
int purpleId = 101;
// Check the menu item already added or not
if(menu.findItem(purpleId) == null){
// If it not exists then add the menu item to menu
MenuItem cut = menu.add(
Menu.NONE, // groupId
purpleId, // itemId
2, // order
"Cut" // title
);
// Set an icon for the new menu item
cut.setIcon(R.drawable.ic_action_content_cut);
// Set the show as action flags for new menu item
cut.setShowAsActionFlags(
MenuItem.SHOW_AS_ACTION_WITH_TEXT |
MenuItem.SHOW_AS_ACTION_ALWAYS
);
// Set a click listener for the new menu item
cut.setOnMenuItemClickListener(menuItem -> {
mTextView.append("\n" + cut.getTitle() + " Clicked.");
return true;
});
mTextView.append("\nCut MenuItem Added.");
}
// Remove a menu item from menu
if(menu.findItem(R.id.copy)!=null){
// If copy menu item is not deleted then
// delete/remove it from the menu
menu.removeItem(R.id.copy);
mTextView.append("\nCopy MenuItem Deleted.");
}
// Update/modify/change a menu item
MenuItem paste = menu.findItem(R.id.paste);
if(paste.getTitle() != "Paste Text"){
// If paste menu item title not updated
// then update/change it
paste.setTitle("Paste Text");
mTextView.append("\nPaste MenuItem Edited.");
}
super.onPrepareOptionsMenu(menu);
return true;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tool="http://schemas.android.com/tools"
android:background="#DCDCDC"
android:padding="24dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tool:text="TextView"
android:fontFamily="sans-serif"
android:textSize="26sp"
android:textStyle="italic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
res/menu/toolbar_options_menu.xml
<?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/delete"
android:title="Delete"
android:icon="@drawable/ic_action_delete"
app:showAsAction="always|withText"
android:orderInCategory="1" />
<item
android:id="@+id/copy"
android:title="Copy"
android:icon="@drawable/ic_action_content_copy"
app:showAsAction="ifRoom|withText"
android:orderInCategory="2" />
<item
android:id="@+id/paste"
android:title="Paste"
android:icon="@drawable/ic_action_content_paste"
app:showAsAction="ifRoom|withText"
android:orderInCategory="3" />
</menu>