Example : How to create an Options Menu (Three Vertical Dot) in an Android App using XML and Android Java.
				
					NB:
First of all, right click on res folder - New - Android resource directory - select menu from resource type - ok - right click on menu folder - New - Menu resource file - name option_menubars - now option_menubars.xml file created.


activity_main.xml(optional)
-----------------------

<?xml version="1.0" encoding="utf-8"?>
<linearlayout 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="Hello World!"></textview>
</linearlayout>


option_menubars.xml
-----------------------

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

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:title="Option 1" android:id="@+id/option_one"></item>
    <item android:title="Option 2" android:id="@+id/option_two"></item>
    <item android:title="Option 3" android:id="@+id/option_three"></item>
    <item android:title="Option 4" android:id="@+id/option_four"></item>
</menu>


MainActivity.java
---------------------

package com.rkm.menubardemo;

import androidx.annotation.NonNull;
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);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu opmenu)
    {
        getMenuInflater().inflate(R.menu.option_menubars,opmenu);        
        return true;


        //MenuInflater inflater = getMenuInflater();
        //inflater.inflate(R.menu.option_menubars, menu);
        //return super.onCreateOptionsMenu(menu);

    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item)
    {
        int item_id=item.getItemId();
        switch (item_id)
        {
            case R.id.option_one:
                Toast.makeText(this, "Option one Clicked", Toast.LENGTH_SHORT).show();

            case R.id.option_two:
                Toast.makeText(this, "Option two Clicked", Toast.LENGTH_SHORT).show();

            case R.id.option_three:
                Toast.makeText(this, "Option three Clicked", Toast.LENGTH_SHORT).show();

            case R.id.option_four:
                Toast.makeText(this, "Option four Clicked", Toast.LENGTH_SHORT).show();
            default:
                return super.onOptionsItemSelected(item);
        }
        
        /*-------------------  OR  -------------------
        
        if (item.getItemId() == R.id.option_one)
        {
            Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
        }
        else if (item.getItemId() == R.id.option_two)
        {
            Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
        }
        else if (item.getItemId() == R.id.option_three)
        {
            Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
        }
        else if (item.getItemId() == R.id.option_four)
        {
            Toast.makeText(getApplicationContext(), item.getTitle() + " is selected", Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);*/
        
    }
}
				
			
Example : How to create a Context Menu  in an Android App using XML and Android Java.
				
					NB:
(i) First of all, right click on 'res' folder - New - Android resource directory - select menu from resource type - ok - right click on 'menu' folder - New - Menu resource file - put the name 'context_menu' - now 'context_menu.xml' file created.

(ii) If switch case does not work properly then, add this statement -  android.nonFinalResIds=false    in 'Gradle Properties' file of 'Gradle Scripts' in left pane/side.

(iii) Context Menu is a menu appear from the list of items when long press over on an item (not clicking).



activity_main_activity2_context_menu.xml
---------------------------------------------

<?xml version="1.0" encoding="utf-8"?>
<linearlayout 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" android:orientation="vertical" tools:context=".MainActivity2ContextMenu">

    <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/contacts_list"></listview>

</linearlayout>


context_menu.xml
---------------------


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/call" android:title="CALL"></item>

    <item android:id="@+id/sms" android:title="SMS"></item>

    <item android:id="@+id/share" android:title="SHARE"></item>
</menu>



MainActivity2ContextMenu.java
---------------------------------

package com.rkm.myapplication;

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.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity2ContextMenu extends AppCompatActivity
{
    ListView cList;

    String[] Contacts={"User1","User2","User3","User4"};

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity2_context_menu);


        cList=(ListView) findViewById(R.id.contacts_list);
        ArrayAdapter<string> adapter= new ArrayAdapter&lt;&gt;(this, android.R.layout.simple_list_item_1,Contacts);
        cList.setAdapter(adapter);
        registerForContextMenu(cList);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
    {
        getMenuInflater().inflate(R.menu.context_menu,menu);

        //super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item)
    {

        int id= item.getItemId();
        switch (id)
        {
            case R.id.call:
                Toast.makeText(this, "User wants to Call", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.share:
                Toast.makeText(this, "User wants to Share", Toast.LENGTH_SHORT).show();
                return true;
            case R.id.sms:
                Toast.makeText(this, "User wants to send SMS", Toast.LENGTH_SHORT).show();
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }


}

</string>
				
			
Example : How to create a Popup Menu in an Android App using XML and Android Java.
				
					NB:
(i) First of all, right click on 'res' folder - New - Android resource directory - select menu from resource type - ok - right click on 'menu' folder - New - Menu resource file - put the name 'pop_up_menu' - now 'pop_up_menu.xml' file created.

(ii) If switch case does not work properly then, add this statement -  android.nonFinalResIds=false    in 'Gradle Properties' file of 'Gradle Scripts' in left pane/side.

(iii) Must use                                 implements PopupMenu.OnMenuItemClickListener         in        public class MainActivity extends  AppCompatActivity  as  

public class MainActivity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener

(iv) Popup Menu is a menu appear when clicking on a Button or TextView etc.


activity_main.xml
-----------------------

<?xml version="1.0" encoding="utf-8"?>
<linearlayout 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" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity">

    <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pop Up Menu" android:textstyle="bold" android:onclick="showPopup"></button>
        
</linearlayout>


pop_up_menu.xml
-----------------------

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/option1" android:title="Option1"></item>
    <item android:id="@+id/option2" android:title="Option2"></item>
    <item android:id="@+id/option3" android:title="Option3"></item>
        
</menu>


MainActivity.java
---------------------

package com.rkm.myapplication;


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void showPopup(View v)
    {
        PopupMenu popup= new PopupMenu(this, v);
        popup.setOnMenuItemClickListener(this);
        popup.inflate(R.menu.pop_up_menu);
        popup.show();
    }

    @Override
    public boolean onMenuItemClick(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.option1:
                Toast.makeText(this,"Option1 Clicked",Toast.LENGTH_SHORT).show();
                return true;
            case R.id.option2:
                Toast.makeText(this,"Option2 Clicked",Toast.LENGTH_SHORT).show();
                return true;
            case R.id.option3:
                Toast.makeText(this,"Option3 Clicked",Toast.LENGTH_SHORT).show();
                return true;
            default:
                return false;
        }
    }


}

				
			
Categories: Android

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.