In this tutorial, we show you how to make nested context menu... A context menu is a menu in a graphical user interface that appears upon user interaction..
Open “res/layout/activity_main.xml” file, add a button.
File : res/layout/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=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:layout_marginTop="26dp"
android:text="Context Menu" />
</RelativeLayout>
Open “res/menu/main.xml” file, add following items.
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/one"
android:title="One">
<menu>
<item android:id="@+id/item1" android:title="Apple"/>
<item android:id="@+id/item2" android:title="iphone"/>
</menu>
</item>
<item
android:id="@+id/two"
android:title="Two"/>
<item
android:id="@+id/three"
android:title="Three"/>
</menu>
Code::
open MainActivity.java file and paste following code..
Make onContextItemSelected method and get all items id..& don't forget to add registerForContextMenu for button through which we get context menu..
package com.example.contextmenuforbutton;
import android.os.Bundle;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.button1);
registerForContextMenu(b);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.main, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId())
{
case R.id.one:
Toast.makeText(getApplicationContext(), "One selected", 20).show();
return true;
case R.id.two:
Toast.makeText(getApplicationContext(), "Two selected", 20).show();
return true;
case R.id.three:
Toast.makeText(getApplicationContext(), "Three selected", 20).show();
return true;
case R.id.item1:
Toast.makeText(getApplicationContext(), "Apple selected", 20).show();
return true;
case R.id.item2:
Toast.makeText(getApplicationContext(), "Iphone selected", 20).show();
return true;
}
return true;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
//SubMenu s = menu.addSubMenu(R.id.one);
//s.add(0,R.id.item1, 0, "Apple");
//s.add(0,R.id.item2, 0, "Iphone");
return true;
}
}
Note:: This is my first blog.. if any problem occurs, please specify in comment..



No comments:
Post a Comment