About
Home > Tutorials Home > Android > SubMenu Creation in Android

SubMenu Creation in Android

Published on October 17, 2019 8:09:31 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 "SubMenu 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.submenuexample;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
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)
	{
		SubMenu sb=menu.addSubMenu("SubMenu");
		sb.add(0,1,1,"one");
		sb.add(0,2,2,"two");
		sb.add(0,3,3,"three");

		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item)
	{
		int id=item.getItemId();

		if(id==1)
		{
			Toast.makeText(this,"Id is:-"+item.getTitle(),1000).show();
		}
		else
		{
			Toast.makeText(this,"Id 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