Problem with Context menus


Last Updated:

  1. LoneWolfPR

    LoneWolfPR New Member This Topic's Starter

    Joined:
    Jul 21, 2010
    Messages:
    3
    Likes Received:
    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 (Text):
    1.  
    2. import android.app.Activity;
    3. import android.os.Bundle;
    4. import android.view.ContextMenu;
    5. import android.view.MenuInflater;
    6. import android.view.View;
    7. import android.view.ContextMenu.ContextMenuInfo;
    8.  
    9. public class Menus extends Activity {
    10.     /** Called when the activity is first created. **/
    11.     View.OnCreateContextMenuListener contextListener;
    12.     @Override
    13.     public void onCreate(Bundle savedInstanceState) {
    14.         super.onCreate(savedInstanceState);
    15.         setContentView(R.layout.main);
    16.        
    17.         View myView = getCurrentFocus();
    18.         registerForContextMenu(myView);
    19.        
    20.         contextListener = new View.OnCreateContextMenuListener() {
    21.            
    22.             public void onCreateContextMenu(ContextMenu menu, View v,
    23.                     ContextMenuInfo menuInfo) {
    24.                     MenuInflater inflater = getMenuInflater();
    25.                     inflater.inflate(R.menu.cmenu, menu);
    26.                
    27.             }
    28.         };
    29.        
    30.     }
    31.  
    32.  
    33.    
    34. }
    35.  
     

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    just put this in your Activity class:

    Code (Text):
    1. public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
    2.     {
    3.         super.onCreateContextMenu(menu, v, menuInfo);
    4.        
    5.         if(v.getId() == R.id.whateverYourViewsIdIs)
    6.         {
    7.            
    8.         }
    9.     }
    and then just use
    registerForContext(myView);

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

    You will also want to implement

    Code (Text):
    1.  
    2. public boolean onContextItemSelected(MenuItem item)
    3.     {
    4.         super.onContextItemSelected(item);
    5.        
    6.         switch(item.getItemId())
    7.         {
    8.         case R.id.whateverYourMenuItemIdIs:
    9.                      ...
    10.         }
    11.         return true;
    12.     }
     
    LoneWolfPR likes this.
  3. LoneWolfPR

    LoneWolfPR New Member This Topic's Starter

    Joined:
    Jul 21, 2010
    Messages:
    3
    Likes Received:
    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 (Text):
    1.  
    2. package com.sonarstudios.dev.samplemenus;
    3.  
    4.  
    5. import android.app.Activity;
    6. import android.os.Bundle;
    7. import android.view.ContextMenu;
    8. import android.view.MenuItem;
    9. import android.view.View;
    10. import android.view.ContextMenu.ContextMenuInfo;
    11.  
    12. public class Menus extends Activity {
    13.     /** Called when the activity is first created. **/
    14.     View.OnCreateContextMenuListener contextListener;
    15.     @Override
    16.     public void onCreate(Bundle savedInstanceState) {
    17.         super.onCreate(savedInstanceState);
    18.         setContentView(R.layout.main);
    19.        
    20.         View myView = getCurrentFocus();
    21.         registerForContextMenu(myView);
    22.        
    23.        
    24.     }
    25.    
    26.     @Override
    27.     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
    28.         super.onCreateContextMenu(menu, v, menuInfo);
    29.        
    30.         if(v.getId() == R.id.mainView){
    31.            
    32.         }
    33.     }
    34.    
    35.     @Override
    36.     public boolean onContextItemSelected(MenuItem item){
    37.         super.onContextItemSelected(item);
    38.        
    39.         switch(item.getItemId()){
    40.         case R.id.item1:
    41.             break;
    42.         case R.id.item2:
    43.             break;
    44.         case R.id.item3:
    45.             break;
    46.         case R.id.item4:
    47.             break;
    48.         default:
    49.             break;
    50.         }
    51.         return true;
    52.     }
    53.    
    54. }
    55.  
    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 (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    4.     android:id="@+id/mainView"
    5.     android:orientation="vertical"
    6.     android:layout_width="fill_parent"
    7.     android:layout_height="fill_parent"
    8.     >
    9. </LinearLayout>
    10.  
    11.  
     
  4. LoneWolfPR

    LoneWolfPR New Member This Topic's Starter

    Joined:
    Jul 21, 2010
    Messages:
    3
    Likes Received:
    0
    Actually, nevermind. I found the fix. Instead of this:

    Code (Text):
    1.  
    2.         View myView = getCurrentFocus();
    3.         registerForContextMenu(myView);
    4.  
    I did this:

    Code (Text):
    1.  
    2.         registerForContextMenu(findViewById(R.id.mainView));
    3.  
    That fixed it. After populating the if statement in the onCreateContextMenu with a menuinflater i got it working. Thanks again for the help.
     
  5. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458


    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
     

Share This Page

Loading...