• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Problem with Context menus

LoneWolfPR

Lurker
Jul 21, 2010
3
0
I'm fairly new to android app development, and I've been playing around with context menus. The info on the developer site is very straightforward, and I can make it work just fine. After looking at it though I just wanted to make a simple context menu show up from a long press on a blank layout. I've tried all manner of things, but I can't seem to figure it out. Is android limited as to which types of views you can call a context menu from?

Here's my current version, which is DRASTICALLY different than how I started with it. This causes the emulator to crash as soon as the the app starts. I know this should be fairly simple, but I'm at a loss. Any help would be great. Thanks!

Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;

public class Menus extends Activity {
    /** Called when the activity is first created. **/
    View.OnCreateContextMenuListener contextListener;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        View myView = getCurrentFocus();
        registerForContextMenu(myView);
        
        contextListener = new View.OnCreateContextMenuListener() {
            
            public void onCreateContextMenu(ContextMenu menu, View v,
                    ContextMenuInfo menuInfo) {
                    MenuInflater inflater = getMenuInflater();
                    inflater.inflate(R.menu.cmenu, menu);
                
            }
        };
        
    }


    
}
 
just put this in your Activity class:

Code:
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
	{
		super.onCreateContextMenu(menu, v, menuInfo);
		
		if(v.getId() == R.id.whateverYourViewsIdIs)
		{
			
		}
	}

and then just use
registerForContext(myView);

You don't need the whole contextListener = yada yada yada.

You will also want to implement

Code:
public boolean onContextItemSelected(MenuItem item)
	{
		super.onContextItemSelected(item);
		
		switch(item.getItemId())
		{
		case R.id.whateverYourMenuItemIdIs:
                     ...
		}
		return true;
	}
 
  • Like
Reactions: LoneWolfPR
Upvote 0
thanks for the reply. I tried what you said, and it still crashes as soon as it launches. Here's the code implementing what you suggested.

Code:
package com.sonarstudios.dev.samplemenus;


import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;

public class Menus extends Activity {
    /** Called when the activity is first created. **/
    View.OnCreateContextMenuListener contextListener;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        View myView = getCurrentFocus();
        registerForContextMenu(myView);
        
        
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
        super.onCreateContextMenu(menu, v, menuInfo);
        
        if(v.getId() == R.id.mainView){
            
        }
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item){
        super.onContextItemSelected(item);
        
        switch(item.getItemId()){
        case R.id.item1:
            break;
        case R.id.item2:
            break;
        case R.id.item3:
            break;
        case R.id.item4:
            break;
        default:
            break;
        }
        return true;
    }
    
}

Also, it should be noted that there is nothing outside of the base linearlayout call in the xml file for the view. Would this cause a problem?

Here's the xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainView"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
</LinearLayout>
 
Upvote 0
Actually, nevermind. I found the fix. Instead of this:

Code:
        View myView = getCurrentFocus();
        registerForContextMenu(myView);

I did this:

Code:
        registerForContextMenu(findViewById(R.id.mainView));

That fixed it. After populating the if statement in the onCreateContextMenu with a menuinflater i got it working. Thanks again for the help.
 
Upvote 0
Actually, nevermind. I found the fix. Instead of this:

Code:
        View myView = getCurrentFocus();
        registerForContextMenu(myView);

I did this:

Code:
        registerForContextMenu(findViewById(R.id.mainView));

That fixed it. After populating the if

statement in the onCreateContextoMenu with a menuinflater i got it working. Thanks again for the help.



Wow... i need to start reading the code better. thats another obvious mistake i should have caught right off the bat... sry for leading you in the wrong direction
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones