Monday, 23 September 2019

Android Programming MENU Concept

D:\androidoptionmenuchan\app\src\main\res\layout\activity_main.xml

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

   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:orientation="vertical"
   
>
  

    <
TextView
       
android:id="@+id/txt1"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="CONTEXT MENU  FOR BIRDS LIST"
       
android:paddingBottom="30dp"
       
android:textAllCaps="true"
       
android:textSize="20sp"
       
/>
    <
Button
       
android:id="@+id/button"
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:layout_alignParentStart="true"
       
android:layout_alignParentTop="true"
       
android:layout_marginTop="181dp"
       
android:onClick="popup1"
       
android:text=" POPUP MENU FOR BIRDS LIST "
       
android:textAllCaps="true" />

   


</
RelativeLayout>


_________________________________________________________
D:\androidoptionmenuchan\app\src\main\res\menu\menu_file.xml

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

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

    <item android:id="@+id/birds"

        android:title="Click to see sub items"

        android:icon="@drawable/allbirds" >



        <!-- "item" submenu -->

        <menu>

            <item android:id="@+id/peacock"

                android:title="Peacock"

                android:icon="@drawable/peacock"/>

            <item android:id="@+id/parrot"

                android:title="Parrot"

                android:icon="@drawable/parrot" />

            <item android:id="@+id/duck"

                android:title="Duck"

                android:icon="@drawable/duck" />



        </menu>

    </item>

</menu>

D:\androidoptionmenuchan\app\src\main\res\menu\menu_file1.xml

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

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

    <item android:id="@+id/gforest"

        android:title=" Gir Forest"

        android:icon="@drawable/allbirds" >



                   <item android:id="@+id/sforest"

                android:title="Sundarban Forest "

                />

            <item android:id="@+id/vforest"

                android:title="Vandalur Forest"

                 />

            <item android:id="@+id/kforest"

                android:title="Kaziranga Forest "

                 />





    </item>

</menu>

D:\androidoptionmenuchan\app\src\main\java\com\ldc\androidoptionmenuchan\MainActivity.java

package com.ldc.androidoptionmenuchan;



import android.app.Activity;

import android.os.Bundle;

import android.view.ContextMenu;

import android.view.Menu;

import android.view.MenuInflater;

import android.view.MenuItem;

import android.view.View;

import android.widget.ImageButton;

import android.widget.ImageView;

import android.widget.PopupMenu;



import android.widget.TextView;

import android.widget.Toast;





public class MainActivity extends Activity implements PopupMenu.OnMenuItemClickListener {

ImageView imgv1;

ImageButton imgb1;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        //imgb1=(ImageButton) findViewById(R.id);

        //registerForContextMenu((ImageView)findViewById(R.id.imageView));

       // imgv1=(ImageView) findViewById(R.id.imgvw1);





        //Registering for Context Menu

        registerForContextMenu((TextView)findViewById(R.id.txt1));



    }



    @Override

    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfor) {

        super.onCreateContextMenu(menu, view, menuInfor);



        MenuInflater menin = getMenuInflater();

        menin.inflate(R.menu.menu_file,menu);

    }





    @Override

    public boolean onContextItemSelected(MenuItem m_item) {

        switch (m_item.getItemId()){

            case R.id.birds:

                Toast.makeText(this,"BIRDS LIST CONTEXT MENU",Toast.LENGTH_SHORT).show();

                break;

            case R.id.peacock:

                Toast.makeText(this,"I Love Dancing During  Raining ",Toast.LENGTH_SHORT).show();

                break;

            case R.id.parrot:

                Toast.makeText(this,"I Love Eating of Nuts",Toast.LENGTH_SHORT).show();

                break;

            case R.id.duck:

                Toast.makeText(this,"I Love Swimming ",Toast.LENGTH_SHORT).show();

                break;

        }

        return true;



    }



    @Override

    public boolean onCreateOptionsMenu(Menu menu) {



        MenuInflater menin1 = getMenuInflater();

        menin1.inflate(R.menu.menu_file, menu);

        return true;

    }





    @Override

    public boolean onOptionsItemSelected(MenuItem m_item) {



        switch (m_item.getItemId()) {

            case R.id.birds :  Toast.makeText(this, "Cicked Main Menu - Birds", Toast.LENGTH_SHORT).show();

                 break;

            case R.id.peacock:

                Toast.makeText(this, "   Beautiful Peackcock", Toast.LENGTH_SHORT).show();

                break;

            case R.id.parrot:

                Toast.makeText(this, " Gracyful  Parrot ", Toast.LENGTH_SHORT).show();

                break;

            case R.id.duck:

                Toast.makeText(this, "Lovely Duck", Toast.LENGTH_SHORT).show();

                break;

        }

        return true;

    }





    public void popup1(View view1) {

        PopupMenu popup = new PopupMenu(this, view1);

        popup.setOnMenuItemClickListener(this);

        MenuInflater menuin2 = getMenuInflater();

        menuin2.inflate(R.menu.menu_file1, popup.getMenu());

        popup.show();



    }





    @Override

    public boolean onMenuItemClick(MenuItem m_item) {

        switch (m_item.getItemId()) {

            case R.id.gforest:

                Toast.makeText(this, "Gir Forest  in Gujarat", Toast.LENGTH_SHORT).show();



                break;

            case R.id.sforest:

                Toast.makeText(this, "Sundarban Forest in West Bangal ", Toast.LENGTH_SHORT).show();

                break;

            case R.id.kforest:

                Toast.makeText(this, "Kaziranga Forest  in Chennai", Toast.LENGTH_SHORT).show();

                break;

            case R.id.vforest:

                Toast.makeText(this, "Vandalure Forest in Assam", Toast.LENGTH_SHORT).show();

                break;

        }

        return true;

    }

}

__________________________________________________________________________