0% found this document useful (0 votes)
3 views3 pages

Menu

The document provides code examples for implementing option menus and context menus in an Android application. It includes XML menu definitions and Java code for handling menu item selections in the MainActivity class. The menus allow users to select options that update a TextView with the selected option's text.

Uploaded by

loc71321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Menu

The document provides code examples for implementing option menus and context menus in an Android application. It includes XML menu definitions and Java code for handling menu item selections in the MainActivity class. The menus allow users to select options that update a TextView with the selected option's text.

Uploaded by

loc71321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Menu

Option menu

Usage:
src/main/res/menu

<?xml version="1.0" encoding="utf-8"?>


<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/option1"
android:title="Option 1" />
<item
android:id="@+id/option2"
android:title="Option 2" />
</menu>

Activity

package com.example.optionmenudemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView optionTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
optionTV = findViewById(R.id.optionTV);
}

@Override
public boolean onCreateOptionsMenu(@NonNull Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {

Menu 1
case R.id.option1:
optionTV.setText("Select option 1");
break;
case R.id.option2:
optionTV.setText("Select option 2");
break;
}
return super.onOptionsItemSelected(item);
}
}

Context menu
menu

<?xml version="1.0" encoding="utf-8"?>


<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<item
android:id="@+id/option1"
android:title="Option 1" />
<item
android:id="@+id/option2"
android:title="Option 2" />
</menu>

`Activity`

package com.example.contextmenudemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView contentTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contentTV = findViewById(R.id.contentTV);
registerForContextMenu(contentTV);
}

@Override

Menu 2
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId() == R.id.contentTV) {
getMenuInflater().inflate(R.menu.context, menu);
}
}

@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.option1:
contentTV.setText("Select option 1");
break;
case R.id.option2:
contentTV.setText("Select option 2");
break;
}
return super.onContextItemSelected(item);
}
}

Menu 3

You might also like