Experiment 9 menu
Experiment 9 menu
Experiment 9 menu
Aim: Design an application to create an options menu for the android activity.
Solution:
Step 1: Change an element in <application> attribute in AndroidManifest.xml:
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
<item
android:id="@+id/search"
android:icon="@android:drawable/ic_menu_search"
android:title="Search"
app:showAsAction="always" />
<item
android:id="@+id/settings"
android:title="Settings"
app:showAsAction="never" />
<item
android:id="@+id/deleteid"
android:title="Delete"
app:showAsAction="never" />
</menu>
Step 5: 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:layout_constraintBottom_toTopOf="@+id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<TextView
android:id = "@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf = "parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 6: MainActivity.java:
package com.example.menuexampleprogram;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar; Make sure that
androidx.appcompat.widget.Toolb
public class MainActivity extends AppCompatActivity { ar package imported to java code.
TextView res;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
res = findViewById(R.id.textView);
Toolbar myToolBar = findViewById(R.id.my_toolbar);
setSupportActionBar(myToolBar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
if(item.getItemId()==R.id.search){
res.setText("Search");
Toast.makeText(this, "Search Menu", Toast.LENGTH_SHORT).show();
return true;
} else if (item.getItemId()==R.id.settings) {
res.setText("Settings");
Toast.makeText(this, "Settings Menu", Toast.LENGTH_SHORT).show();
return true;
} else if (item.getItemId()==R.id.deleteid) {
res.setText("Delete");
Toast.makeText(this, "Delete Menu", Toast.LENGTH_SHORT).show();
return true;
}
else
return super.onOptionsItemSelected(item);
}
}
Output: