Create a menu with 5 options and selected options should appear in text box.
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Menu Options!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.q6_menu_options">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Q6_Menu_Options">
<meta-data
android:name="com.google.android.actions"
android:resource="@menu/themenu" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:
name="android.intent.action.MAIN" />
<category android:
name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.example.q6_menu_options;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.themenu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(this, "1. Android",
Toast.LENGTH_LONG).show();
break;
case R.id.item2:
Toast.makeText(this, "2. BitBucket",
Toast.LENGTH_LONG).show();
break;
case R.id.item3:
Toast.makeText(this, "3. Code",
Toast.LENGTH_LONG).show();
break;
case R.id.item4:
Toast.makeText(this, "4. Dart",
Toast.LENGTH_LONG).show();
break;
case R.id.item5:
Toast.makeText(this, "5. Enterprise",
Toast.LENGTH_LONG).show();
break;
}
return true;
}
}