Question

I'd like to show a context_menu when a user click on a Button but I'm encountering some problems. For some reason the menu is not appearing, maybe there's something I'm missing. Can you help me please? That's my code:

public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn = (Button) findViewById(R.id.btn);
    btn.setOnCreateContextMenuListener(this);
    registerForContextMenu(btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            v.showContextMenu();
        }
    });

}
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    }
@Override
public boolean onContextItemSelected(MenuItem item) {
    int i = item.getItemId();
    if (i == R.id.share) {
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, "Leggi");
        sendIntent.setType("text/plain");
        startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.chooser_title)));
        return true;
    } else {
        return onContextItemSelected(item);
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    return true;
}

and this is my context_menu in R.menu:

<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/share"
    android:title="Condividi"
    android:orderInCategory="100"
    app:showAsAction="never" />
<item android:id="@+id/web"
    android:title="Mostra articolo"
    android:orderInCategory="100"
    app:showAsAction="never" />
<item android:id="@+id/add"
    android:title="Aggiungi nei Preferiti"
    android:orderInCategory="100"
    app:showAsAction="never" />

Was it helpful?

Solution

Change the app:showAsAction="never" to android:showAsAction="never" perhaps ? (or just remove it).

OTHER TIPS

You can use openContextMenu instead of showContextMenu.

registerForContextMenu(btn);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        openContextMenu(v);
    }
});

you can try this code code:

public class MainActivity extends Activity {

    Button btn;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn=(Button) findViewById(R.id.Button1);
        registerForContextMenu(btn);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            btn.showContextMenu();

        }
    }); 

    }


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

        menu.add(0,1,0,"copy");
        menu.add(0,2,0,"paste");
        super.onCreateContextMenu(menu, v, menuInfo);
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top