Technical Knowledge

technical knowledge is that type of blogger in which you can find all the soluctions of your problems which you cannot solve you can see the video given on this blog and solve your problem by yourself.

Like us on Facebook

Technical Knowledge

Sunday, March 29, 2015

options menu, context menu, and sub menu: android tutorial

Program description:

This program will show how to design, create, and handle menus in android.

There are 3 types menus:

1. Options menu - displayed when user clicks hard menu key on the keyboard
2. Sub menu - menu with in a menu
3. Context menu - press and hold on a view, to show context menu.
                                just like when you right click on some item in your desktop.

This will show how to create options menu, how to handle option menu items &

How to register context menu for a button, and how to handle context menu items.

Note: From android version 3.0 on wards there is no support for hard key menu, so using options menu is discouraged. In place of options menu we will be using action bars.


First Activity
package com.techpalle.b15_menus;

import android.os.Bundle;
import android.app.Activity;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button1);
registerForContextMenu(b1);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
if(v.getId() == R.id.button1){

}
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.contextmenu, menu);
menu.setHeaderIcon(R.drawable.ic_launcher);
menu.setHeaderTitle("Contact Us!");
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.item1:
Toast.makeText(getApplicationContext(),
"Contact by SMS", 0).show();
break;
case R.id.item2:
Toast.makeText(getApplicationContext(),
"Contact by Call", 0).show();
break;
case R.id.item3:
Toast.makeText(getApplicationContext(),
"Contact by Map location", 0).show();
break;
}
return super.onContextItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mi = getMenuInflater();
mi.inflate(R.menu.optionsmenu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.getItem(0).setTitle("My home");
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.item1:
Toast.makeText(getApplicationContext(), "Home", 0).show();
break;
case R.id.item2:
Toast.makeText(getApplicationContext(), "AboutUs", 0).show();
break;
case R.id.item3:
item.getSubMenu().setHeaderIcon(R.drawable.ic_launcher);
item.getSubMenu().setHeaderTitle("Techpalle Trainings");
Toast.makeText(getApplicationContext(), "Trainings", 0).show();
break;
case R.id.item4:
Toast.makeText(getApplicationContext(), item.getTitle(),
0).show();
break;
case R.id.item5:
item.setChecked(!item.isChecked());
Toast.makeText(getApplicationContext(), "Android", 0).show();
break;
case R.id.item6:
Toast.makeText(getApplicationContext(), "DotNet", 0).show();
break;
}
return super.onOptionsItemSelected(item);
}

}




xml layout file for First Activity 
File name: activity_main.xml
  xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >


options menu file in res -> menu folder
optionsmenu.xml (in res-menu folder)
  




android:checkable="true" android:checked="true"/>
android:checkable="true"/>










context menu file in res -> menu folder
contextmenu.xml (in res-menu folder)
  





Download complete code : Click to download

Similar programs:
android Dialogs tutorial

Tags: android, tutorial, examples, menus, options menu, sub menu, context menu

No comments:

Post a Comment