Hey friends, In this post I'm gonna explain you how to create List view ?, how to perform specific task on each item selection?, How to create Context Menu and how to perform specific task on selection of context Item.... So lots of things are there in this post.... So, use it n Enjoy!!
|
List View On Single Touch |
|
Context Menu On Long press |
/*The Code Goes Here!!*/
<!--
--------------------------------------------------------- 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"
android:id="@+id/lay" >
<ListView
android:id="@+id/listView2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
<!--
--------------------------------------- End of XML File -------------------------------------------
-->
/*============================== Java File===========================*/
public class Context1Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView lv = (ListView) findViewById(R.id.listView2);
String[] list = new String[] {"Home","Office","Office2"};
BaseAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
lv.setAdapter(adapter);
/*Registering for context Menu */
registerForContextMenu(lv);
/*Setting onItemClick listener so that on clicking, it will work accordingly "single click" */
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
if(lv.getItemAtPosition(arg2)=="Home")
home();
if(lv.getItemAtPosition(arg2)=="Office")
office();
if(lv.getItemAtPosition(arg2)=="Office2"){
office2();
}
}
});
/* set onClickListner listener on the whole layout(Liner layout) so that whenever user will click on
screen it will provide option for adding location*/
LinearLayout ll = (LinearLayout)findViewById(R.id.lay);
//registerForContextMenu(ll); //this can be use for open a context menu by clicking on screen
ll.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(Context1Activity.this, "linerlayout click", Toast.LENGTH_LONG).show();
}
});
}
/*-------------------------Function body for each item in list -------------------------------------*/
public void home(){
Toast.makeText(Context1Activity.this,"home single click", Toast.LENGTH_LONG).show();
}
public void office(){
Toast.makeText(Context1Activity.this,"office single click", Toast.LENGTH_LONG).show();
}
public void office2(){
Toast.makeText(Context1Activity.this,"office2 single click", Toast.LENGTH_LONG).show();
}
/*--------------------------------------------------------------------------------------------------*/
/* For implementing context menu, two methods should be implement,
* one is onCreateContextMenu for creating context menu which will add menu
* other one is OnContextItemSelected for performing action on selecting the items
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu); //select layout which should pop up in context menu
menu.add("Add Location!");
menu.add("Delete");
menu.add("Rename");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onContextItemSelected(item);
if(item.getTitle()=="Add Location!"){
Addloc();
}
if(item.getTitle()=="Delete"){
Delete();
}
if(item.getTitle()=="Rename"){
Rename();
}
return true;
}
/*----------------------------function body for context menu----------------------------------*/
public void Addloc(){
Toast.makeText(this,"Add location", Toast.LENGTH_LONG).show();
}
public void Delete(){
Toast.makeText(this,"Delete Message", Toast.LENGTH_LONG).show();
}
public void Rename(){
Toast.makeText(this,"Rename message", Toast.LENGTH_LONG).show();
}
/*------------------------------------------------------------------------------------------------*/
}
/*=========================End of Java File===========================*/