0

I want to add a floating context menu to a button in android. How can i create a listener for the same How can i use the item to know which button invoked the menu?

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

@Override
public boolean onContextItemSelected(MenuItem item) {
    if (item.getGroupId() == R.id.button2)
        Toast.makeText(MainActivity.this, "1", Toast.LENGTH_SHORT).show();
    else
    if (item.getGroupId() == R.id.button3)
        Toast.makeText(MainActivity.this, "2", Toast.LENGTH_SHORT).show();
    return true;
}

1 Answers1

1

This might help.

activity_main.xml

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1" />

    <Button
       android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2" />

</LinearLayout>

ActivityMain.java

package com.shyra.contextmenu;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    Button button1, button2;

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

        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);

        registerForContextMenu(button1);
        registerForContextMenu(button2);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);

        if (v.getId() == R.id.button1) {
            menu.setHeaderTitle("Context Menu");
            getMenuInflater().inflate(R.menu.menu_button1, menu);
        } else if (v.getId() == R.id.button2) {
            menu.add(v.getId(), 0, Menu.NONE, "Option 1");
            menu.add(v.getId(), 1, Menu.NONE, "Option 2");
            menu.add(v.getId(), 2, Menu.NONE, "Option 3");
        }
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        if (item.getTitle().equals("Action 1")) {
            Toast.makeText(getApplicationContext(), "Action 1 pressed", Toast.LENGTH_SHORT).show();
        } else if (item.getTitle().equals("Action 2")) {
            Toast.makeText(getApplicationContext(), "Action 2 pressed", Toast.LENGTH_SHORT).show();
        } else if (item.getTitle().equals("Action 3")) {
            Toast.makeText(getApplicationContext(), "Action 3 pressed", Toast.LENGTH_SHORT).show();
        } else if (item.getGroupId() == R.id.button2) {
            if (item.getItemId() == 0) {
                Toast.makeText(getApplicationContext(), "Option 1 pressed", Toast.LENGTH_SHORT).show();
            } else if (item.getItemId() == 1) {
                Toast.makeText(getApplicationContext(), "Option 2 pressed", Toast.LENGTH_SHORT).show();
            } else if (item.getItemId() == 2) {
                Toast.makeText(getApplicationContext(), "Option 3 pressed", Toast.LENGTH_SHORT).show();
            }
        }
        return true;
    }
}

menu_button1.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="com.osahub.rachit.contextmenu.MainActivity">
    <item
        android:id="@+id/action_1"
        android:title="Action 1"
        app:showAsAction="never"/>
    <item
        android:id="@+id/action_2"
        android:title="Action 2"
        app:showAsAction="never"/>
    <item
        android:id="@+id/action_3"
        android:title="Action 3"
        app:showAsAction="never"/>
</menu>
Rachit
  • 3,173
  • 3
  • 28
  • 45
  • But if two buttons can invoke the menu then how can I know which button invoked the menu (If I want to make some changes to that button)? – Aditya Vijayvergia Dec 27 '16 at 10:57
  • Look at my edit. The above Context Menu is for 2 buttons. And the ItemSelected handles that using the GroupID and the ItemID. – Rachit Dec 27 '16 at 11:11
  • I have used created menu in a separate menu file as in my edit. – Aditya Vijayvergia Dec 27 '16 at 11:37
  • @AdityaVijayvergia check the answer now. It's not a very neat solution, but could work. – Rachit Dec 27 '16 at 11:59
  • thanks...it worked. I was thinking about the case when we have 5 views (all registered for context menu) and all of them invoke the same menu. Then is there a way to know which view invoked the menu so that we can perform some action on that view(like remane). – Aditya Vijayvergia Dec 27 '16 at 12:18
  • @AdityaVijayvergia I would recommend creating the Menu from the java code, instead of XML, in that case. – Rachit Jan 04 '17 at 05:31
  • http://stackoverflow.com/a/21959658/3286614 - Check this answer out to understand what I mean – Rachit Jan 04 '17 at 05:32