main_activity.xml
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/AbsoluteLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="268dp"
android:layout_height="wrap_content"
android:layout_x="30dp"
android:layout_y="56dp"
android:text="Context Menu" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="2dp"
android:layout_y="6dp"
android:text="Developed by Shrikant"
android:textAppearance="?android:attr/textAppearanceMedium" />
</AbsoluteLayout>
Under menu folder add following xml files:
contextitems.xml
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/item5" android:title="Option5"></item>
<item android:id="@+id/item6" android:title="Option6"></item>
<item android:id="@+id/item7" android:title="Option7"></item>
<item android:id="@+id/item8" android:title="Option8"></item>
</menu>
items.xml
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/item1" android:icon="@drawable/ic_launcher" android:title="Option1"></item>
<item android:id="@+id/item2" android:icon="@drawable/ic_launcher" android:title="Option2"></item>
<item android:id="@+id/item3" android:icon="@drawable/ic_launcher" android:title="Option3"></item>
<item android:id="@+id/item4" android:icon="@drawable/ic_launcher" android:title="Option4"></item>
</menu>
Mainactivity.java
package com.shrikant.menumyself;import android.os.Bundle;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button contextbtn=(Button)findViewById(R.id.button1);
registerForContextMenu(contextbtn);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.item, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(getApplicationContext(), "option1 selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item2:
Toast.makeText(getApplicationContext(), "option2 selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item3:
Toast.makeText(getApplicationContext(), "option3 selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item4:
Toast.makeText(getApplicationContext(), "option4 selected", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.contextitems, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.item5:
Toast.makeText(getApplicationContext(), "option5 selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item6:
Toast.makeText(getApplicationContext(), "option6 selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item7:
Toast.makeText(getApplicationContext(), "option7 selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item8:
Toast.makeText(getApplicationContext(), "option8 selected", Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.