About
Home > Tutorials Home > Android > Menu Creation Example With Toast

Menu Creation Example With Toast

Published on October 17, 2019 8:07:11 PM

We are an online tutoring company that has laid its foundation in utilizing Multiple Intelligence methodology to understand specific needs and learning styles of a student.So that we patch a particular student to a tutor having a compatible teaching style. We have devised a smart online tutoring system to give you measurable results.Schools, cramped with students, fail to give your child the edge, the motivation and the right teacher guidance to help him find the true potential. We at Eonlinetutors.com make sure that your child gets the perfect learning environment to outperform.




Step 1 : Open the Eclipse and create a new Android project using Blank Activity application template. Give the application name "Menu Example in Android".

<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">
</RelativeLayout>

Step 2 : Copy the Following Code into your .Java File.

package com.example.menuexample2;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		menu.add(0,1,2,"One");
		menu.add(0,2,3,"Two");
		menu.add(0,3,1,"Three");
		menu.add(0,4,4,"Four");
		return true;
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item)
	{
		int id=item.getItemId();
		if(id==1)
		{
			Toast.makeText(this,"Selected Menu is:-"+item.getTitle(),1000).show();
		}
		else
		{
			Toast.makeText(this,"Selected Menu is:-"+item.getTitle(),1000).show();
		}
		return true;
	}
}
ALL COMMENTS (2)
Abhishek Bedre 2017-03-28 17:25:15
I have implemented my own "jarvis"....thanks to this example.        
2019-01-03 13:22:19
It is working fine in desktop but not working in android mobile