Showing posts with label Context menu in android. Show all posts
Showing posts with label Context menu in android. Show all posts

Saturday, March 24, 2012

ListView with Context menu in android

ListView with Context menu in android

1,Define the xml file:-

   <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
  >
  <ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="0px"
    android:layout_weight="1"
    />
   </LinearLayout>

2, Define the layout for items in the ListView (listitem.xml):

    <?xml version="1.0" encoding="utf-8"?>

       <TextView
       xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="fill_parent" android:layout_height="wrap_content"
       android:textSize="35dip"
       android:padding="8dip"
       android:textColor="#990000"
        />
3,  Define the array in string.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Listview_contextmenuActivity!</string>
    <string name="app_name">Listview_contextmenu</string>
    <string-array name="Countries"><item>jisho</item>
    <item>aju</item>
    <item >gino</item>
    <item >binish</item>
    <item>alex</item>
    <item >vishnu</item>
    <item >arun</item>
    </string-array>
 </resources>

4, Retrieve the array element from strings.xml: and Sort the string;

        Resources res = getResources();
        Countries = getResources().getStringArray(R.array.Countries);

         Arrays.sort(Countries);

5, Define the Array adapter and set the register for Context menu.

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem,Countries);
        list.setAdapter(adapter);
        registerForContextMenu(list);


6,  Override the Context menu:-

     @Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo)
{
      if (v.getId()==R.id.list)
{
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        menu.setHeaderTitle(Countries[info.position]);
        String[ ] menuItems = getResources().getStringArray(R.array.Countries);
        for (int i = 0; i<menuItems.length; i++)
 {
          menu.add(Menu.NONE, i, i, menuItems[i]);
  }
  }
  }

--------------------------------------------------------------------
package l.c;

import java.util.Arrays;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Listview_contextmenuActivity extends Activity {
    /** Called when the activity is first created. */
    private String[] Countries;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Resources res = getResources();
        Countries = getResources().getStringArray(R.array.Countries);
        Arrays.sort(Countries);
      
        ListView list = (ListView)findViewById(R.id.list);
        //ImageView icon=(ImageView) findViewById(R.id.icon);
       // ArrayAdapter<String> a=new ArrayAdapter<String>(this, R.layout.main, R.id.offerdetails,name);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem,Countries);
        list.setAdapter(adapter);
        registerForContextMenu(list);
      }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
      if (v.getId()==R.id.list) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
        menu.setHeaderTitle(Countries[info.position]);
        String[] menuItems = getResources().getStringArray(R.array.Countries);
        for (int i = 0; i<menuItems.length; i++) {
          menu.add(Menu.NONE, i, i, menuItems[i]);
        }
      }
    }
       
    }
   
----------------------------------------------------------------------------------   






Friday, March 23, 2012

Context Menu

Context Menu(Long press menu)


We then use registerForContextMenu in the onCreate of the activity to tell android that we want this view to create a menu when it is long pressed. This is not limited to buttons, this will work for other views too. You must register each view that you want to have associated with the context menu.

1, Then register menu and button in onCreate method

        bt=(Button) findViewById(R.id.bt1);
        registerForContextMenu(bt); 


2, Override the onCreateContextMenu method to create the menu:


    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 
    menu.setHeaderTitle("Context Menu"); 
     menu.add(0, v.getId(), 0, "Action 1"); 
     menu.add(0, v.getId(), 0, "Action 2"); 
    }  


4,Then override onContextItemSelected to preform the action when an option is selected from this menu:

     @Override 
           public boolean onContextItemSelected(MenuItem item)
 { 
            if(item.getTitle()=="Action 1")
            {
                function1(item.getItemId());
            } 
             else if(item.getTitle()=="Action 2")
            {
                function2(item.getItemId());
             } 
            else
            {
            return false;
            } 
             return true; 
    }
    private void function1(int itemId)
   {
                Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show(); 
    }
    private void function2(int itemId)
   {
                Toast.makeText(this, "function 2 called", Toast.LENGTH_SHORT).show(); 
    } 
}