A Context menu is a skimming menu that shows up when the client plays out a long-tap on a
component. It gives activities that influence the chose substance or setting outline.
Example :-
@override
public boolean onContextItemSelected(MenuItem item)
{
AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
Switch (item.getItemId())
{
case R.id.contacts:
contacts(info.id);
return true;
case R.id.settings:
settings(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
Code for ContextMenu in android below shown .
activity_main.xml :-
MainActivity.java :-
Output :-
Select the Run option we will get the result of the context menu app and it will show the output on emulator or on mobile phone.
component. It gives activities that influence the chose substance or setting outline.
Example :-
@override
public boolean onContextItemSelected(MenuItem item)
{
AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
Switch (item.getItemId())
{
case R.id.contacts:
contacts(info.id);
return true;
case R.id.settings:
settings(info.id);
return true;
default:
return super.onContextItemSelected(item);
}
Code for ContextMenu in android below shown .
activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
tools:context="com.kiranapp.MainActivity"> <ListView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal" /> </LinearLayout>
MainActivity.java :-
package com.kiranapp; import android.os.Bundle; import android.support.v7.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.ArrayAdapter;
import android.view.AdapterView; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends Activity { ListView lv; String Courses[]={"ANDROID","IOS","PHONEGAP","BLACKBERRY","WINDOWS","MICROSOFT"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv=(ListView)findViewById(R.id.listView); ArrayAdapter<String> adapter=new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,Courses); lv.setAdapter(adapter); registerForContextMenu(lv); } @Overridepublic void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("Select The movement"); menu.add(0,v.getId(),0,"view"); menu.add(0,v.getId(),0,"send sms"); } @Override public boolean onContextItemSelected(MenuItem item) { if (item.getTitle()=="view") { Toast.makeText(getApplicationContext(),"view code",Toast.LENGTH_LONG).show(); } else if (item.getTitle()=="send sms"){ Toast.makeText(getApplicationContext(),"sendingsmscode",Toast.LENGTH_LONG).show(); } else { return false; } return true; } }
Output :-
Select the Run option we will get the result of the context menu app and it will show the output on emulator or on mobile phone.
No comments:
Post a Comment