Thursday, 15 September 2016

Context Menu Example in android studio | How to apply context menu in android application

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

Required classes and xml layouts:-
1.MainActivity.java | activity_main.xml

Create a new project and add following code to your project:-

Step1:- activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="supriya.com.contextmenu.MainActivity"
    android:background="#DF013A">
  <ListView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:id="@+id/list1"></ListView>
</RelativeLayout>
Step2:-MainActivity.java
package supriya.com.contextmenu;

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

public class MainActivity extends AppCompatActivity {
    ListView listView;
    String[] list = {"Somya", "Supriya", "Pankaj", "Ankit", "Rajwari", "Honey", "Vikash", "Sunita", "Dipika", "Suman", "Akash", "Saurabh", "Mohan", "Ram"};

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.list1);
        ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adp);
        registerForContextMenu(listView);


    }

    @Override    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Select Option");
        menu.add(0, v.getId(), 0, "Call");
        menu.add(0,v.getId(),0,"Message");
        menu.add(0,v.getId(),0,"Mail");
    }

    @Override    public boolean onContextItemSelected(MenuItem item) {
        if(item.getTitle()=="Call"){
            Toast.makeText(getApplicationContext(),"Call:Selected",Toast.LENGTH_LONG).show();
        }
        else if (item.getTitle()=="Message"){
            Toast.makeText(getApplicationContext(),"Message:Selected",Toast.LENGTH_LONG).show();
        }
        else if(item.getTitle()=="Mail"){
            Toast.makeText(getApplicationContext(),"Mail:Selected",Toast.LENGTH_LONG).show();
        }
        return super.onContextItemSelected(item);
    }
}
Output:-
         

12 comments:

  1. Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.Android Training in velachery | Android Training in chennai | Android Training in chennai with placement

    ReplyDelete
  2. 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.Android Training in chennai | Best Android Training in chennai|Android Training in chennai with placement | Android Training in velachery

    ReplyDelete
  3. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    online Python certification course
    python training in OMR
    python training course in chennai

    ReplyDelete
  4. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    Devops training in marathahalli
    Devops training in rajajinagar

    ReplyDelete
  5. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful.Best Android Training Institute

    ReplyDelete