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
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
<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"
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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="27dp"
android:layout_marginTop="18dp"
android:text="Press and hold to Contact" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:text="Go to Next screen" />
</RelativeLayout>
options menu file in res -> menu folder
optionsmenu.xml (in res-menu folder)
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item1" android:title="Home"></item>
<item android:id="@+id/item2" android:title="AboutUs"></item>
<item android:id="@+id/item3" android:title="Trainings">
<menu>
<item android:id="@+id/item5" android:title="Android"
android:checkable="true" android:checked="true"/>
<item android:id="@+id/item6" android:title="DotNet"
android:checkable="true"/>
</menu>
</item>
<item android:id="@+id/item4" android:title="ContactUs"></item>
<group android:id="@+id/group1" android:checkableBehavior="single">
<item android:id="@+id/item7" android:title="item7"></item>
<item android:id="@+id/item8" android:title="item8"></item>
</group>
</menu>
context menu file in res -> menu folder
contextmenu.xml (in res-menu folder)
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item1" android:title="Conact by SMS"></item>
<item android:id="@+id/item2" android:title="Contact by Call"></item>
<item android:id="@+id/item3" android:title="Contact by Map"></item>
</menu>
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