vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Wednesday, July 4, 2012

Showing Context menu in single click on ListView in android application


Here now we shall look out about context menu used in ListView in android
In ListView if we want to show the context menu on selected items,by default context menu will display only long time press on the selected items.
 But in some case we need to show the context menu on single click on the list view, this type of approach we going to see here …
For ListView I used separate layout for listview
Below layout xml file for ListView
Rowlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingTop="20dp">          
<ImageView
        android:id="@+id/icon"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_marginLeft="4dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="4dp"
        android:src="@drawable/man" >
    </ImageView>
    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="20dp"
        android:textColor="#000000" >
    </TextView> 
</LinearLayout>

Below is the main layout file
List.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     android:background="@drawable/bgcolor" >    
 <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    </ListView>
</LinearLayout>

Below is the Activity java file code
List.java
public void onCreate(Bundle savedInstanceState) {

              super.onCreate(savedInstanceState);
              setContentView(R.layout.benelist1);
              str = new String[] { "Arul", "sairam", "raju", "rahul", "Salim",
                           "Kumar" };
              lst = (ListView) findViewById(R.id.listView1);
              ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                           R.layout.rowlayout, R.id.label, str);
              lst.setAdapter(adapter);
               registerForContextMenu(lst);
               
               //Here we set the on item click open the context menu
               //*****www.dotnetcode.in******
                      lst.setOnItemClickListener(new OnItemClickListener() {
               
                   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

                       arg1.showContextMenu();
                   }
               });
              // TODO Auto-generated method stub
       }
       @Override
       public void onCreateContextMenu(ContextMenu menu, View v,
                     ContextMenuInfo menuInfo) {

              AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
              menu.setHeaderTitle(str[info.position]);
              menuItems = new String[] { "Edit", "Delete","Send" };
              menu.add(Menu.NONE, 0, 0, menuItems[0]);
              menu.add(Menu.NONE, 1, 1, menuItems[1]);
              menu.add(Menu.NONE, 2, 2, menuItems[2]);
       }

       public boolean onContextItemSelected(MenuItem item) {

              AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
                           .getMenuInfo();
              int menuItemIndex = item.getItemId();
                     menuItems = new String[] { "Edit", "Delete","Send" };
              String menuItemName = menuItems[menuItemIndex];
               Toast.makeText(this, "You have selected: " + " " + menuItemName.toString(), Toast.LENGTH_LONG).show();
              return true;
       }

Coding part is over now you can run the application and click the listview item,om single click the context menu will open
Happy coding ………Keep reading

0 comments:

Post a Comment