Option Menu and Context Menu
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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
style="@style/Theme.Material3.Light.NoActionBar">
<Button
android:id="@+id/clickbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.818" />
<WebView
android:id="@+id/webview1"
android:layout_width="386dp"
android:layout_height="301dp"
android:layout_marginEnd="12dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.46" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="409dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.094" />
</androidx.constraintlayout.widget.ConstraintLayout>
menu_example.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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
style="@style/Theme.Material3.Light.NoActionBar">
<Button
android:id="@+id/clickbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.818" />
<WebView
android:id="@+id/webview1"
android:layout_width="386dp"
android:layout_height="301dp"
android:layout_marginEnd="12dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.46" />
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="409dp"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.094" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.menu_application;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
Button click;
Toolbar toolbar1;
WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
toolbar1= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar1);
click=(Button)findViewById(R.id.clickbtn);
click.setOnCreateContextMenuListener(this);
wv=(WebView)findViewById(R.id.webview1);
WebSettings webSettings = wv.getSettings();
webSettings.setBuiltInZoomControls(true);
wv.loadUrl("https://www.google.com/");
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v,
insets) -> {
Insets systemBars =
insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right,
systemBars.bottom);
return insets;
});
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
createMenu(menu);
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
return menuChoice(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_example, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
if(itemId==R.id.send) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]
{"[email protected]"}); // Recipient's email
intent.putExtra(Intent.EXTRA_SUBJECT, "Sample Android Program");
intent.putExtra(Intent.EXTRA_TEXT, " This is a sample mail");
// Check if there's an email client available
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent); // Start the email activity (Gmail or
other email apps)
} else {
Toast.makeText(this, "No email client found",
Toast.LENGTH_SHORT).show();
}
return true;
}
else if(itemId==R.id.gallery) {
// Create an Intent to view images
Intent intent = new Intent(Intent.ACTION_VIEW);
// Set the URI to the external storage (which typically contains
images)
intent.setDataAndType(Uri.parse(
"content://media/internal/images/media"),
"image/*");
// Check if there is an app to handle this intent
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent); // Start the gallery activity
} else {
Toast.makeText(this, "No gallery app found",
Toast.LENGTH_SHORT).show();
}
return true;
}else if(itemId==R.id.call) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:9865805218"));
startActivity(intent);
return true;
}
else if(itemId==R.id.calculator){
// Create an intent with ACTION_VIEW to open the calculator
Intent intent = new Intent(Intent.ACTION_VIEW);
// You can try using this URI for opening the calculator
intent.setData(Uri.parse(
"market://search?q=calculator"));
// Check if there's an activity that can handle this intent
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent); // Open the calculator or search for
a calculator app
} else {
Toast.makeText(this, "No calculator app found",
Toast.LENGTH_SHORT).show();
}
return true;
}else if(itemId==R.id.exit){
finish();
return true;
}else{
return false;
}
}
private boolean menuChoice(MenuItem item) {
switch(item.getItemId()){
case 0:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(
"http://www.pexels.com/search/rose/"));
startActivity(intent);
return true;
case 1:
Intent intent1 = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.pexels.com/search/Lotus/"));
startActivity(intent1);
return true;
case 2:
Intent intent2 = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.pexels.com/search/lilly/"));
startActivity(intent2);
return true;
}
return false;
}
public void createMenu(Menu menu){
MenuItem mi1= menu.add(0,0,0,"Rose");
MenuItem mi2= menu.add(0,1,1,"Lotus");
MenuItem mi3 = menu.add(0,2,2,"Lilly");{
mi3.setAlphabeticShortcut('a');
}
}
}
Output