Skip to main content

Android Context Menu

Context menu is  a floating menu which appears when user press long-click on an element.
Let's see an example demonstrating context menu where context menu appears on performing long click on an item of list view.
1. Create a new project.
2. Open activity_main.xml and add a list view.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical"
    tools:context="com.gunjan.andriodcodesnippets.MainActivity">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/lv_names"
            />
</LinearLayout>
3. Add main.xml in res/menu folder and add the following menu items.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/item_delete"
        app:showAsAction="ifRoom|withText"
        android:title="Delete"
        android:titleCondensed="Delete"
   />
    <item
        android:id="@+id/item_edit"
        app:showAsAction="ifRoom|withText"
        android:title="Edit"
        android:titleCondensed="Edit"/>
</menu>
4. Open MainActivity.xml and type the following code-
public class MainActivity extends AppCompatActivity {

    ListView mListview;
    String[] names={"Amit","Aalok","Marmee","Anna","Andrew"};
    ArrayAdapter<String> mAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListview=(ListView)findViewById(R.id.lv_names);
        mAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names);
        mListview.setAdapter(mAdapter);

        registerForContextMenu(mListview);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        if(v.getId()==R.id.lv_names){
            MenuInflater inflater=getMenuInflater();
            inflater.inflate(R.menu.main,menu);
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info=(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
        int position=info.position;
        switch (item.getItemId()){
            case R.id.item_delete:
                //Your code
                return true;
            case R.id.item_edit:
                //Your code
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }
}
5. Run the project.


Comments

  1. Hi there! I know this is kinda off topic but I was wondering
    if you knew where I could locate a captcha plugin for my comment form?
    I'm using the same blog platform as yours and I'm having
    trouble finding one? Thanks a lot!

    ReplyDelete
    Replies
    1. If you are using Blogspot, then login to your account on Blogspot, click on "Settings" tab, you will a section for "Comments ". Click on it. Then scroll down a little bit, there will be a "Show word verification?" heading, select YES for it and Save settings.

      Delete

Post a Comment

Popular Posts