How to create Context Menu explain :-
The Context menu is a
floating menu, the user perform long-click on an element.
The context menu
Developing in Android 2.3 an higher version.the Context menu implement the best
UI context view is ,ListView and GridView. example
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="com.androidbeginner.testcode.MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
Main_Activity.java
package
com.androidbeginner.testcode;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.ContextMenu;
import
android.view.ContextMenu.ContextMenuInfo;
import
android.view.MenuItem;
import
android.view.View;
import android.widget.ArrayAdapter;
import
android.widget.ListView;
import
android.widget.Toast;
public class
MainActivity extends Activity {
ListView listView1;
String
contacts[]={"Mr.varun","Mr.Sachin","Mr.rahul","
Mr.Tarun","Mr.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;
}
}
0 comments