Wednesday, 23 September 2015

Context Menu in Android

Context Menu:-
Android context menu appears when user press long click on the element. It is also known as floating menu.

It doesn't support item shortcuts and icons.

To show a context menu on long click, most clients will want to call registerForContextMenu(View) and overrideonCreateContextMenu(ContextMenu, View, ContextMenu.ContextMenuInfo).
activity_main.xml
-------------------------
<LinearLayout
   
xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
tools:context=".MainActivity"
   
android:orientation="vertical">


    <
TextView
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:textAppearance="?android:attr/textAppearanceLarge"
       
android:text="Context Menu"
       
android:id="@+id/textView"
       
android:layout_marginTop="52dp"
       
android:layout_gravity="center_horizontal" />
</
LinearLayout>
menu_main.xml
-------------------------
<menu 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" tools:context=".MainActivity">

    <item android:id="@+id/item1" android:title="Context1"></item>

    <item android:id="@+id/item2" android:title="Context2"></item>

    <item android:id="@+id/item3" android:title="Context3"></item>

    <item android:id="@+id/item4" android:title="Context4"></item>

</menu>

MainActivity.Java
-------------------------
package com.anil.android.contextmenus;



import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.ContextMenu;

import android.view.MenuItem;

import android.view.View;

import android.widget.TextView;

import android.widget.Toast;



public class MainActivity extends AppCompatActivity {

  TextView t1;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        t1=(TextView)findViewById(R.id.textView);

        registerForContextMenu(t1);

    }



    @Override

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        getMenuInflater().inflate(R.menu.menu_main,menu);

        super.onCreateContextMenu(menu, v, menuInfo);

    }



    @Override

    public boolean onContextItemSelected(MenuItem item) {

        switch (item.getItemId()){

            case R.id.item1:

                Toast.makeText(MainActivity.this, "Context 1 is Selected", Toast.LENGTH_SHORT).show();

                break;

            case R.id.item2:

                Toast.makeText(MainActivity.this, "Context 2 is Selected", Toast.LENGTH_SHORT).show();

                break;

            case R.id.item3:

                Toast.makeText(MainActivity.this, "Context 3 is Selected", Toast.LENGTH_SHORT).show();

                break;

            case R.id.item4:

                Toast.makeText(MainActivity.this, "Context 4 is Selected", Toast.LENGTH_SHORT).show();

                break;

        }

        return super.onContextItemSelected(item);

    }

}

Output is:
-------------------------







No comments:

Post a Comment