Options Menu in Android.
activity name MenusActivity.java.)
Step 2: Open menu.xml (located under res/menu folder) file and paste below code into it.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item Set id, icon and Title for each menu item -->
<item android:id="@+id/menu_save"
android:title="Save" />
<item android:id="@+id/menu_share"
android:title="Share" />
<item android:id="@+id/menu_delete"
android:title="Delete" />
<item android:id="@+id/menu_preferences"
android:title="Preferences" />
</menu>
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.Menu;
import
android.view.MenuInflater;
import
android.view.MenuItem;
import android.widget.Toast;
public class AndroidMenusActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
//
Initiating Menu XML file (menu.xml)
@Override
public
boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater
menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.menu,
menu);
return
true;
}
@Override
public
boolean onOptionsItemSelected(MenuItem item)
{
switch
(item.getItemId())
{
case
R.id.menu_save:
Toast.makeText(AndroidMenusActivity.this,
"Save is Selected", Toast.LENGTH_SHORT).show();
return true;
case
R.id.menu_share:
Toast.makeText(AndroidMenusActivity.this,
"Share is Selected", Toast.LENGTH_SHORT).show();
return true;
case
R.id.menu_delete:
Toast.makeText(AndroidMenusActivity.this,
"Delete is Selected", Toast.LENGTH_SHORT).show();
return
true;
case R.id.menu_preferences:
Toast.makeText(AndroidMenusActivity.this,
"Preferences is Selected", Toast.LENGTH_SHORT).show();
return true;
default:
return
super.onOptionsItemSelected(item);
}
}
}
Yes this looks easy
ReplyDeleteSimple and well explained... 👍
ReplyDeleteSimple Way To Learn And Great Explained 👌🙌
ReplyDeleteVery well explained and very much thank you for providing screen shots, it helped me a lot!!!
ReplyDeleteEasy to understand
ReplyDeleteEasy to understand.. 👍
ReplyDeleteNice n simple way to explained
ReplyDeleteEasy way explain..
ReplyDeleteVery interesting and informative
ReplyDeleteVery well explained..Very nice
ReplyDeleteVery nice
ReplyDeleteStep by step explanation has given... So nice 👍
ReplyDeleteVery nice...
ReplyDeleteAwesomely explained....... Each step was broken into simpler one
ReplyDeleteWell explained ����
ReplyDeleteWell explained and easy to understand
ReplyDeleteGreat��
ReplyDeleteExplained in simple language. Great Going
ReplyDeleteWell done
ReplyDeleteGrt work
ReplyDeleteNice explanation
ReplyDeleteEasiest way to learn..very well explained..✌️✌️
ReplyDeleteGood...👍 Easy to understand and in that way anyone can do it 👍👍👍
ReplyDeleteExplained in a simple way ��
ReplyDeleteNice and simple. I am definitely going to try this.
ReplyDeleteTruly good for beginners.
ReplyDeleteThank you mam
This comment has been removed by the author.
ReplyDelete