Worldgineer
Worldgineer

Reputation: 33

Floating Context Menu in Android

I am trying to create a floating Context Menu when long pressing a button. I have read all the answer but still, I am driving myself crazy with this. Here it is my code:

R.menu.menu.xml

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

        <item android:id="@+id/MnuOpc1" android:title="Opcion1"
               android:icon="@drawable/ic_launcher"></item>
        <item android:id="@+id/MnuOpc2" android:title="Opcion2"
              android:icon="@drawable/ic_launcher"></item>
        <item android:id="@+id/MnuOpc3" android:title="Opcion3"
              android:icon="@drawable/ic_launcher"></item>

    </menu>

OnCreateContextMenu(...)

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

onContextItemSelected(...)

@Override
public boolean onContextItemSelected(MenuItem item) {
    Log.v("Hello...","I got the switch");  

    switch (item.getItemId()) {
    case R.id.MnuOpc1:
        Log.v("Hello...","Option 1");   
        return true;
    case R.id.MnuOpc2:
        Log.v("Hello...","Option 2"); 
        return true;
    default:
        return super.onContextItemSelected(item);
    }

I think all the code above is right but I am missing something when calling at registerForContextMenu:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    linear = (LinearLayout) findViewById(R.layout.main);
    registerForContextMenu(linear);

I tried with registerForContextMenu(getListView()); but it does not work, I am getting an error from Eclipse. What am I doing wrong?

Upvotes: 2

Views: 4730

Answers (1)

Nate
Nate

Reputation: 401

You said you're trying to create a context menu for a button, but in your onCreate() you register the context menu to a LinearLayout. Can't tell if that's it though without knowing what error you are actually getting.

Upvotes: 2

Related Questions