Question

iv tied between clicking on imagebutton to open a context menu but the menu doesnt open. here is the xml:

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My Movies"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_weight="1"
        android:gravity="center" />

    <ImageButton
        android:id="@+id/btn_settings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_menu_settings_holo_light" />

</LinearLayout>

here is the connection to the clicking event:

private ImageButton btn_settings;


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

    btn_settings = (ImageButton)findViewById(R.id.btn_settings);

    registerForContextMenu(btn_settings);

and here is the context menu itself:

@Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()){
    case R.id.Item1:
        handler.deleteAllMovies();
        break;

    case R.id.Item2:
        finish();
    }
    return super.onContextItemSelected(item);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflate = getMenuInflater();
    inflate.inflate(R.menu.context, menu);
}

the overides are outside the oncreate section and btn_settings imagebutton is global. the context menu doesnt open at all, what do i do wrong?

Was it helpful?

Solution

For a ContextMenu, make sure you are long pressing on the image button. Otherwise, a simple tap on the button will not have the context menu shown.

From the documentation of ContextMenu:

To show a context menu on long click, most clients will want to call registerForContextMenu(View) and override onCreateContextMenu(ContextMenu, View, ContextMenu.ContextMenuInfo).

If you want the context menu to be shown on a single click, you can manually show it:

btn_settings = (ImageButton) findViewById(R.id.btn_settings);
registerForContextMenu(btn_settings);
btn_settings.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        view.showContextMenu();
    }
});

OTHER TIPS

Please modify following code and try again:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
 // TODO Auto-generated method stub 
 MenuInflater inflate = getMenuInflater();
 inflate.inflate(R.menu.context, menu);
 super.onCreateContextMenu(menu, v, menuInfo);
}

I hope it will work :)

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