Create a new folder in resource name it my_menu
my_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/item1"
android:title="Setting"/>
<item
android:id="@+id/item2"
android:title="Help"/>
<item
android:id="@+id/item3"
android:title="About"/>
<item
android:id="@+id/item4"
android:title="Exit"/>
</menu>
Then Go to MainActivity.java
package com.kc.optionmenu2;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater men= getMenuInflater();
men.inflate(R.menu.my_menu,menu);
// TODO: Implement this method
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId()==R.id.item1)
{
Toast.makeText(this, "Setting is clicked",
Toast.LENGTH_SHORT).show();
}else
if(item.getItemId()==R.id.item2)
{
Toast.makeText(this, "Help is clicked",
Toast.LENGTH_SHORT).show();
}else
if(item.getItemId()==R.id.item3)
{
Toast.makeText(this, "About is clicked",
Toast.LENGTH_SHORT).show();
}else
if(item.getItemId()==R.id.item4)
{
Toast.makeText(this, "Exit is clicked",
Toast.LENGTH_SHORT).show();
}
// TODO: Implement this method
return true;
}
}
my_menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/item1"
android:title="Setting"/>
<item
android:id="@+id/item2"
android:title="Help"/>
<item
android:id="@+id/item3"
android:title="About"/>
<item
android:id="@+id/item4"
android:title="Exit"/>
</menu>
Then Go to MainActivity.java
package com.kc.optionmenu2;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater men= getMenuInflater();
men.inflate(R.menu.my_menu,menu);
// TODO: Implement this method
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.getItemId()==R.id.item1)
{
Toast.makeText(this, "Setting is clicked",
Toast.LENGTH_SHORT).show();
}else
if(item.getItemId()==R.id.item2)
{
Toast.makeText(this, "Help is clicked",
Toast.LENGTH_SHORT).show();
}else
if(item.getItemId()==R.id.item3)
{
Toast.makeText(this, "About is clicked",
Toast.LENGTH_SHORT).show();
}else
if(item.getItemId()==R.id.item4)
{
Toast.makeText(this, "Exit is clicked",
Toast.LENGTH_SHORT).show();
}
// TODO: Implement this method
return true;
}
}
0 comments:
Post a Comment