📝
Android Programming
  • Introduction
  • Contents
  • Android
    • Q1
    • Q2
    • Q3
    • Q4
    • Q5
    • Q6
    • Q7
    • Q8
    • Q9
    • Q10
    • Q11
    • Q12
    • End
Powered by GitBook
On this page
  • Create a menu with 5 options and selected options should appear in text box.
  • Code
  • Output
  1. Android

Q6

Question 6

PreviousQ5NextQ7

Last updated 4 years ago

Create a menu with 5 options and selected options should appear in text box.

App Insights:

Created the menu folder "res" folder. Made the Java Activity code and use Menu resource for the option menu and display using TextView

Code

<?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"?>
<menu 
xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/item1"
            android:title="1. Android"
            />

        <item android:id="@+id/item2"
            android:title="2. BitBucket"
            />
        <item android:id="@+id/item3"
            android:title="3. Code"
            />
        <item android:id="@+id/item4"
            android:title="4. Dart"
            />
        <item android:id="@+id/item5"
            android:title="5. Enterpise"
            />

    </menu>
<?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;
    }
}

Output

****

Download to Run and Test Locally

Q6_Menu_Options.apk
Browse Source Code
Studio Screenshot Main Activity (1/14)
App Screenshot Main Activity (2/14)
Studio Screenshot Displaying Menu Options (3/14)
App Screenshot Displaying Menu Options (4/14
Studio Screenshot Displaying Chosen Option (5/14)
App Screenshot Displaying Chosen Option (6/14)
Studio Screenshot Displaying Chosen Option (7/14)
App Screenshot Displaying Chosen Option (8/14)
Studio Screenshot Displaying Chosen Option (9/14)
App Screenshot Displaying Chosen Option (10/14)
Studio Screenshot Displaying Chosen Option (11/14)
App Screenshot Displaying Chosen Option (12/14)
Studio Screenshot Displaying Chosen Option (13/14)
App Screenshot Displaying Chosen Option (14/14)