0

hiii

I an trying to create a context menu which consists of list of items.while clicking on any one of the item a popup menu should come.the problem is when i am clicking on the item,it is remaining on the same page(i.e no pop up is displayed)...Please suggest where i went wrong?

code is as follows:-

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:paddingBottom="@dimen/activity_vertical_margin"  
    android:paddingLeft="@dimen/activity_horizontal_margin"  
    android:paddingRight="@dimen/activity_horizontal_margin"  
    android:paddingTop="@dimen/activity_vertical_margin"  
    tools:context=".MainActivity" >  

    <ListView  
        android:id="@+id/listView1"  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentTop="true"  
        android:layout_marginLeft="66dp"  
        android:layout_marginTop="53dp" >  
    </ListView>  

</RelativeLayout>  

MainActivity.java

package com.javatpoint.contextmenu;  
import android.os.Bundle;  
import android.app.Activity;  
import android.view.ContextMenu;  
import android.view.ContextMenu.ContextMenuInfo;  
import android.view.Menu;  
import android.view.MenuItem;  
import android.view.View;  
import android.widget.AdapterView;  
import android.widget.ArrayAdapter;  
import android.widget.ListView;  
import android.widget.Toast;  

public class MainActivity extends Activity {  
    ListView listView1;  
    String contacts[]={"Ajay","Sachin","Sumit","Tarun","Yogesh"};  

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

        listView1=(ListView)findViewById(R.id.listView1);  

        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contacts);  
        listView1.setAdapter(adapter);  

        // Register the ListView  for Context menu  
        registerForContextMenu(listView1);  
    }  

    @Override   
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)  
    {  
            super.onCreateContextMenu(menu, v, menuInfo);  
            menu.setHeaderTitle("Select The Action");    
            menu.add(0, v.getId(), 0, "Call");//groupId, itemId, order, title   
            menu.add(0, v.getId(), 0, "SMS");   
    }   

    @Override    
    public boolean onContextItemSelected(MenuItem item){    
            if(item.getTitle()=="Call"){  
                Toast.makeText(getApplicationContext(),"calling code",Toast.LENGTH_LONG).show();  
            }    
            else if(item.getTitle()=="SMS"){  
                Toast.makeText(getApplicationContext(),"sending sms code",Toast.LENGTH_LONG).show();  
            }else{  
               return false;  
            }    
          return true;    
      }    
    }  
user3736518
  • 75
  • 1
  • 2
  • 12
  • A context menu setup as you have it is created on a long-press. Is that your problem? – Mike M. Aug 04 '14 at 05:19
  • As already stated by Mike M., your approach by default will be handled by long click. If you want to do it on short click instead, you must call `openContextMenu(listView1)` on your list's click listener. – Andrew T. Aug 04 '14 at 05:30
  • @AndrewT.hii i have tried with openContextMenu(listView1).But its not working.Can you please suggest some other way for this short click. – user3736518 Aug 04 '14 at 05:46

1 Answers1

0

Use .equals instead of == to compare string:

change this:

if(item.getTitle()=="Call"){  
                Toast.makeText(getApplicationContext(),"calling code",Toast.LENGTH_LONG).show();  
            }    
            else if(item.getTitle()=="SMS"){  
                Toast.makeText(getApplicationContext(),"sending sms code",Toast.LENGTH_LONG).show();  
            }

to this:

if(item.getTitle().equals("Call"){  
                Toast.makeText(getApplicationContext(),"calling code",Toast.LENGTH_LONG).show();  
            }    
            else if(item.getTitle().equals("SMS"){  
                Toast.makeText(getApplicationContext(),"sending sms code",Toast.LENGTH_LONG).show();  
            }

For more info see this

Community
  • 1
  • 1
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138