You are on page 1of 44

Android Application Development Training Tutorial

For more info visit http://www.zybotech.in

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Menu
android.view.Menu Known Indirect Subclasses ContextMenu, SubMenu ContextMenu Extension of Menu for context menus providing functionality to modify the header of the context menu. SubMenu Subclass of Menu for sub menus.

Class Overview
Interface for managing the items in a menu. By default, every Activity supports an options menu of actions or options. You can add items to this menu and handle clicks on your additions. The easiest way of adding menu items is inflating an XML file into the Menu via MenuInflater. The easiest way of attaching code to clicks is via onOptionsItemSelected(MenuItem) and onContextItemSelected(MenuItem). Different menu types support different features:
1. Context menus: Do not support item shortcuts and item icons. 2. Options menus: The icon menus do not support item check marks and only show the item's condensed title. The expanded menus (only available if six or more menu items are visible, reached via the 'More' item in the icon menu) do not show item icons, and item check marks are discouraged. 3. Sub menus: Do not support item icons, or nested sub menus.

Summary
Constants Category code for the order integer for items/groups that are alternative actions on the data that is currently displayed -- or/add this with your base value. Category code for the order integer for items/groups that are part of a container -- or/add this with your base value. Category code for the order integer for items/groups that are user-supplied secondary (infrequently used) options -- or/add this with your base value. Category code for the order integer for items/groups that are provided by the system -- or/add this with your base value.

int CATEGORY_ALTERNATIVE

int CATEGORY_CONTAINER

int CATEGORY_SECONDARY

int CATEGORY_SYSTEM

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

int FIRST int FLAG_ALWAYS_PERFORM_CLOSE

First value for group and item identifier integers. Flag for performShortcut(int, KeyEvent, int): if set, always close the menu after executing the shortcut. Flag for addIntentOptions(int, int, int, ComponentName, Intent[], Intent, int, MenuItem[]): if set, do not automatically remove any existing menu items in the same group. Flag for performShortcut(int, KeyEvent, int): if set, do not close the menu after executing the shortcut. Value to use for group and item identifier integers when you don't care about them.

int FLAG_APPEND_TO_GROUP

int FLAG_PERFORM_NO_CLOSE

int NONE

Public Methods
abstract add(int titleRes) MenuItem Add a new item to the menu. abstract add(int groupId, int itemId, int order, CharSequence title) MenuItem Add a new item to the menu. add(int groupId, int itemId, int order, int titleRes) abstract MenuItem Variation on add(int, int, int, CharSequence) that takes a string resource identifier instead of the string itself. abstract add(CharSequence title) MenuItem Add a new item to the menu. addIntentOptions(int groupId, int itemId, int order, ComponentName caller, Intent[] specifics, Intent intent, int flags, MenuItem[] outSpecificItems) Add a group of menu items corresponding to actions that can be performed for a particular Intent. abstract addSubMenu(int titleRes) SubMenu Add a new sub-menu to the menu. addSubMenu(int groupId, int itemId, int order, int titleRes) abstract SubMenu Variation on addSubMenu(int, int, int, CharSequence) that takes a string resource identifier for the title instead of the string itself.

abstract int

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

abstract addSubMenu(CharSequence title) SubMenu Add a new sub-menu to the menu. abstract addSubMenu(int groupId, int itemId, int order, CharSequence title) SubMenu Add a new sub-menu to the menu. abstract void abstract void clear() Remove all existing items from the menu, leaving it empty as if it had just been created. close() Closes the menu, if open.

abstract findItem(int id) MenuItem Return the menu item with a particular identifier. abstract getItem(int index) MenuItem Gets the menu item at the given index. abstract boolean abstract boolean abstract boolean abstract boolean abstract void abstract void abstract void hasVisibleItems() Return whether the menu currently has item items that are visible. isShortcutKey(int keyCode, KeyEvent event) Is a keypress one of the defined shortcut keys for this window. performIdentifierAction(int id, int flags) Execute the menu item action associated with the given menu identifier. performShortcut(int keyCode, KeyEvent event, int flags) Execute the menu item action associated with the given shortcut character. removeGroup(int groupId) Remove all items in the given group. removeItem(int id) Remove the item with the given identifier. setGroupCheckable(int group, boolean checkable, boolean exclusive) Control whether a particular group of items can show a check mark. A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

abstract void abstract void

setGroupEnabled(int group, boolean enabled) Enable or disable all menu items that are in the given group. setGroupVisible(int group, boolean visible) Show or hide all menu items that are in the given group. setQwertyMode(boolean isQwerty)

abstract void

Control whether the menu should be running in qwerty mode (alphabetic shortcuts) or 12-key mode (numeric shortcuts). size() Get the number of items in the menu.

abstract int

Constants
public static final int CATEGORY_ALTERNATIVE

Category code for the order integer for items/groups that are alternative actions on the data that is currently displayed -- or/add this with your base value.
Constant Value: 262144 (0x00040000) public static final int CATEGORY_CONTAINER

Category code for the order integer for items/groups that are part of a container -- or/add this with your base value.
Constant Value: 65536 (0x00010000) public static final int CATEGORY_SECONDARY

Category code for the order integer for items/groups that are user-supplied secondary (infrequently used) options -- or/add this with your base value.
Constant Value: 196608 (0x00030000) public static final int CATEGORY_SYSTEM

Category code for the order integer for items/groups that are provided by the system -- or/add this with your base value.
Constant Value: 131072 (0x00020000) public static final int FIRST

First value for group and item identifier integers.


A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Constant Value: 1 (0x00000001) public static final int FLAG_ALWAYS_PERFORM_CLOSE

Flag for performShortcut(int, KeyEvent, int): if set, always close the menu after executing the shortcut. Closing the menu also resets the prepared state.
Constant Value: 2 (0x00000002) public static final int FLAG_APPEND_TO_GROUP

Flag for addIntentOptions(int, int, int, ComponentName, Intent[], Intent, int, MenuItem[]) : if set, do not automatically remove any existing menu items in the same group.
Constant Value: 1 (0x00000001) public static final int FLAG_PERFORM_NO_CLOSE

Flag for performShortcut(int, KeyEvent, int): if set, do not close the menu after executing the shortcut.
Constant Value: 1 (0x00000001) public static final int NONE

Value to use for group and item identifier integers when you don't care about them.
Constant Value: 0 (0x00000000)

Public Methods
public abstract MenuItem add (int titleRes)

Add a new item to the menu. This item displays the given title for its label.
Parameters titleRes Resource identifier of title string. Returns

The newly added menu item.

public abstract MenuItem add (int groupId, int itemId, int order, CharSequence title)

Add a new item to the menu. This item displays the given title for its label.
Parameters groupId The group identifier that this item should be part of. This can be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

itemId Unique item ID. Use NONE if you do not need a unique ID. order The order for the item. Use NONE if you do not care about the order. See getOrder(). title Returns

The text to display for the item.

The newly added menu item.

public abstract MenuItem add (int groupId, int itemId, int order, int titleRes)

Variation on add(int, int, int, CharSequence) that takes a string resource identifier instead of the string itself.
Parameters groupId The group identifier that this item should be part of. This can also be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.

itemId Unique item ID. Use NONE if you do not need a unique ID. order The order for the item. Use NONE if you do not care about the order. See getOrder(). titleRes Resource identifier of title string. Returns

The newly added menu item.

public abstract MenuItem add (CharSequence title)

Add a new item to the menu. This item displays the given title for its label.
Parameters title The text to display for the item. Returns

The newly added menu item.

public abstract int addIntentOptions (int groupId, int itemId, int order, ComponentName caller, Intent[] specifics, Intent intent, int flags, MenuItem[] outSpecificItems)

Add a group of menu items corresponding to actions that can be performed for a particular Intent. The Intent is most often configured with a null action, the data that the current activity is working with, and includes either

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

the CATEGORY_ALTERNATIVE or CATEGORY_SELECTED_ALTERNATIVE to find activities that have said they would like to be included as optional action. You can, however, use any Intent you want. See queryIntentActivityOptions(ComponentName, Intent[], Intent, int) for more * details on the caller, specifics, and intent arguments. The list returned by that function is used to populate the resulting menu items. All of the menu items of possible options for the intent will be added with the given group and id. You can use the group to control ordering of the items in relation to other items in the menu. Normally this function will automatically remove any existing items in the menu in the same group and place a divider above and below the added items; this behavior can be modified with the flags parameter. For each of the generated items setIntent(Intent) is called to associate the appropriate Intent with the item; this means the activity will automatically be started for you without having to do anything else.
Parameters groupId itemId order caller specifics intent flags outSpecificItems The group identifier that the items should be part of. This can also be used to define groups of items for batch state changes. Normally use NONE if the items should not be in a group. Unique item ID. Use NONE if you do not need a unique ID. The order for the items. Use NONE if you do not care about the order. See getOrder(). The current activity component name as defined by queryIntentActivityOptions(). Specific items to place first as defined by queryIntentActivityOptions(). Intent describing the kinds of items to populate in the list as defined by queryIntentActivityOptions(). Additional options controlling how the items are added. Optional array in which to place the menu items that were generated for each of the specifics that were requested. Entries may be null if no activity was found for that specific action.

Returns

The number of menu items that were added.

public abstract SubMenu addSubMenu (int titleRes)

Add a new sub-menu to the menu. This item displays the given title for its label. To modify other attributes on the submenu's menu item, use getItem().
Parameters titleRes Resource identifier of title string.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Returns

The newly added sub-menu

public abstract SubMenu addSubMenu (int groupId, int itemId, int order, int titleRes)

Variation on addSubMenu(int, int, int, CharSequence) that takes a string resource identifier for the title instead of the string itself.
Parameters groupId The group identifier that this item should be part of. This can also be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.

itemId Unique item ID. Use NONE if you do not need a unique ID. order The order for the item. Use NONE if you do not care about the order. See getOrder(). titleRes Resource identifier of title string. Returns

The newly added sub-menu

public abstract SubMenu addSubMenu (CharSequence title)

Add a new sub-menu to the menu. This item displays the given title for its label. To modify other attributes on the submenu's menu item, use getItem().
Parameters title The text to display for the item. Returns

The newly added sub-menu

public abstract SubMenu addSubMenu (int groupId, int itemId, int order, CharSequence title)

Add a new sub-menu to the menu. This item displays the given title for its label. To modify other attributes on the submenu's menu item, use getItem(). Note that you can only have one level of sub-menus, i.e. you cannnot add a subMenu to a subMenu: An UnsupportedOperationException will be thrown if you try.
Parameters groupId The group identifier that this item should be part of. This can also be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

itemId Unique item ID. Use NONE if you do not need a unique ID. order The order for the item. Use NONE if you do not care about the order. See getOrder(). title Returns

The text to display for the item.

The newly added sub-menu

public abstract void clear ()

Remove all existing items from the menu, leaving it empty as if it had just been created.
public abstract void close ()

Closes the menu, if open.


public abstract MenuItem findItem (int id)

Return the menu item with a particular identifier.


Parameters id The identifier to find. Returns

The menu item object, or null if there is no item with this identifier.

public abstract MenuItem getItem (int index)

Gets the menu item at the given index.


Parameters index The index of the menu item to return. Returns

The menu item.

Throws IndexOutOfBoundsException when index < 0 || >= size() public abstract boolean hasVisibleItems ()

Return whether the menu currently has item items that are visible.
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Returns

True if there is one or more item visible, else false.

public abstract boolean isShortcutKey (int keyCode, KeyEvent event)

Is a keypress one of the defined shortcut keys for this window.


Parameters keyCode the key code from KeyEvent to check. event the KeyEvent to use to help check. public abstract boolean performIdentifierAction (int id, int flags)

Execute the menu item action associated with the given menu identifier.
Parameters id Identifier associated with the menu item. flags Additional option flags or 0. Returns

If the given identifier exists and is shown, returns true; else returns false.

public abstract boolean performShortcut (int keyCode, KeyEvent event, int flags)

Execute the menu item action associated with the given shortcut character.
Parameters keyCode The keycode of the shortcut key. event Key event message. flags Returns

Additional option flags or 0.

If the given shortcut exists and is shown, returns true; else returns false.

public abstract void removeGroup (int groupId)

Remove all items in the given group.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Parameters groupId The group to be removed. If there are no items in this group, nothing happens. public abstract void removeItem (int id)

Remove the item with the given identifier.


Parameters id The item to be removed. If there is no item with this identifier, nothing happens. public abstract void setGroupCheckable (int group, boolean checkable, boolean exclusive)

Control whether a particular group of items can show a check mark. This is similar to calling setCheckable(boolean) on all of the menu items with the given group identifier, but in addition you can control whether this group contains a mutually-exclusive set items. This should be called after the items of the group have been added to the menu.
Parameters group The group of items to operate on.

checkable Set to true to allow a check mark, false to disallow. The default is false. exclusive If set to true, only one item in this group can be checked at a time; checking an item will automatically uncheck all others in the group. If set to false, each item can be checked independently of the others.

public abstract void setGroupEnabled (int group, boolean enabled)

Enable or disable all menu items that are in the given group.
Parameters group The group of items to operate on. enabled If true the items will be enabled, else they will be disabled. public abstract void setGroupVisible (int group, boolean visible)

Show or hide all menu items that are in the given group.
Parameters group The group of items to operate on. visible If true the items are visible, else they are hidden.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

public abstract void setQwertyMode (boolean isQwerty)

Control whether the menu should be running in qwerty mode (alphabetic shortcuts) or 12-key mode (numeric shortcuts).
Parameters isQwerty If true the menu will use alphabetic shortcuts; else it will use numeric shortcuts. public abstract int size ()

Get the number of items in the menu. Note that this will change any times items are added or removed from the menu.
Returns

The item count.

MenuInflater
extends Object java.lang.Object android.view.MenuInflater

Class Overview
This class is used to instantiate menu XML files into Menu objects. For performance reasons, menu inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use MenuInflater with an XmlPullParser over a plain XML file at runtime; it only works with an XmlPullParser returned from a compiled resource (R. something file.)

Summary
Public Constructors MenuInflater(Context context) Constructs a menu inflater. Public Methods inflate(int menuRes, Menu menu) void Inflate a menu hierarchy from the specified XML resource.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

[Expand] Inherited Methods From class java.lang.Object clone() Object Creates and returns a copy of this Object. equals(Object o) boolean Compares this instance with the specified object and indicates if they are equal. finalize() void Is called before the object's memory is being reclaimed by the VM. getClass() final Class<? extends Object> Returns the unique instance of Class which represents this object's class. hashCode() int Returns an integer hash code for this object. notify() final void Causes a thread which is waiting on this object's monitor (by means of calling one of the wait() methods) to be woken up. notifyAll() final void Causes all threads which are waiting on this object's monitor (by means of calling one of the wait() methods) to be woken up. toString() String Returns a string containing a concise, human-readable description of this object. wait(long millis, int nanos) final void Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires. wait(long millis) final void Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

wait() final void Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.

Public Constructors
public MenuInflater (Context context)

Constructs a menu inflater.


See Also
getMenuInflater()

Public Methods
public void inflate (int menuRes, Menu menu)

Inflate a menu hierarchy from the specified XML resource. Throws InflateException if there is an error.
Parameters menuRes Resource ID for an XML layout resource to load (e.g., R.menu.main_activity) menu The Menu to inflate into. The items and submenus will be added to this Menu.

MenuItem
android.view.MenuItem

Class Overview
Interface for direct access to a previously created menu item. An Item is returned by calling one of the add(int) methods.

Summary
Nested Classes interface MenuItem.OnMenuItemClickListener Interface definition for a callback to be invoked when a menu item is clicked. Public Methods A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

getAlphabeticShortcut() abstract char Return the char for this menu item's alphabetic shortcut. getGroupId() abstract int Return the group identifier that this menu item is part of. getIcon() abstract Drawable Returns the icon for this item as a Drawable (getting it from resources if it hasn't been loaded before). getIntent() abstract Intent Return the Intent associated with this item. getItemId() abstract int Return the identifier for this menu item. getMenuInfo() abstract ContextMenu.ContextMenuInfo Gets the extra information linked to this menu item. getNumericShortcut() abstract char Return the char for this menu item's numeric (12-key) shortcut. getOrder() abstract int Return the category and order within the category of this item. getSubMenu() abstract SubMenu Get the sub-menu to be invoked when this item is selected, if it has one. getTitle() abstract CharSequence Retrieve the current title of the item. getTitleCondensed() abstract CharSequence Retrieve the current condensed title of the item. hasSubMenu() abstract boolean Check whether this item has an associated sub-menu. abstract boolean isCheckable()

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Return whether the item can currently display a check mark. isChecked() abstract boolean Return whether the item is currently displaying a check mark. isEnabled() abstract boolean Return the enabled state of the menu item. isVisible() abstract boolean Return the visibility of the menu item. setAlphabeticShortcut(char alphaChar) abstract MenuItem Change the alphabetic shortcut associated with this item. setCheckable(boolean checkable) abstract MenuItem Control whether this item can display a check mark. setChecked(boolean checked) abstract MenuItem Control whether this item is shown with a check mark. setEnabled(boolean enabled) abstract MenuItem Sets whether the menu item is enabled. setIcon(Drawable icon) abstract MenuItem Change the icon associated with this item. setIcon(int iconRes) abstract MenuItem Change the icon associated with this item. setIntent(Intent intent) abstract MenuItem Change the Intent associated with this item. setNumericShortcut(char numericChar) abstract MenuItem Change the numeric shortcut associated with this item. setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener menuItemClickListener) Set a custom listener for invocation of this menu item.

abstract MenuItem

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

setShortcut(char numericChar, char alphaChar) abstract MenuItem Change both the numeric and alphabetic shortcut associated with this item. setTitle(int title) abstract MenuItem Change the title associated with this item. setTitle(CharSequence title) abstract MenuItem Change the title associated with this item. setTitleCondensed(CharSequence title) abstract MenuItem Change the condensed title associated with this item. setVisible(boolean visible) abstract MenuItem Sets the visibility of the menu item.

Public Methods
public abstract char getAlphabeticShortcut ()

Return the char for this menu item's alphabetic shortcut.


Returns

Alphabetic character to use as a shortcut.

public abstract int getGroupId ()

Return the group identifier that this menu item is part of. The group identifier can not be changed after the menu is created.
Returns

The menu item's group identifier.

public abstract Drawable getIcon ()

Returns the icon for this item as a Drawable (getting it from resources if it hasn't been loaded before).
Returns

The icon as a Drawable.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

public abstract Intent getIntent ()

Return the Intent associated with this item. This returns a reference to the Intent which you can change as desired to modify what the Item is holding.
Returns

Returns the last value supplied to setIntent(Intent), or null.

public abstract int getItemId ()

Return the identifier for this menu item. The identifier can not be changed after the menu is created.
Returns

The menu item's identifier.

public abstract ContextMenu.ContextMenuInfo getMenuInfo ()

Gets the extra information linked to this menu item. This extra information is set by the View that added this menu item to the menu.
Returns

The extra information linked to the View that added this menu item to the menu. This can be null.

public abstract char getNumericShortcut ()

Return the char for this menu item's numeric (12-key) shortcut.
Returns

Numeric character to use as a shortcut.

public abstract int getOrder ()

Return the category and order within the category of this item. This item will be shown before all items (within its category) that have order greater than this value. An order integer contains the item's category (the upper bits of the integer; set by or/add the category with the order within the category) and the ordering of the item within that category (the lower bits). Example categories are CATEGORY_SYSTEM, CATEGORY_SECONDARY, CATEGORY_ALTERNATIVE, CATEGORY_CONTAINER. See Menu for a full list.
Returns

The order of this item.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

public abstract SubMenu getSubMenu ()

Get the sub-menu to be invoked when this item is selected, if it has one. See hasSubMenu().
Returns

The associated menu if there is one, else null

public abstract CharSequence getTitle ()

Retrieve the current title of the item.


Returns

The title.

public abstract CharSequence getTitleCondensed ()

Retrieve the current condensed title of the item. If a condensed title was never set, it will return the normal title.
Returns

The condensed title, if it exists. Otherwise the normal title.

public abstract boolean hasSubMenu ()

Check whether this item has an associated sub-menu. I.e. it is a sub-menu of another menu.
Returns

If true this item has a menu; else it is a normal item.

public abstract boolean isCheckable ()

Return whether the item can currently display a check mark.


Returns

If a check mark can be displayed, returns true.

public abstract boolean isChecked ()

Return whether the item is currently displaying a check mark.


Returns

If a check mark is displayed, returns true.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

public abstract boolean isEnabled ()

Return the enabled state of the menu item.


Returns

If true the item is enabled and hence invokable; else it is not.

public abstract boolean isVisible ()

Return the visibility of the menu item.


Returns

If true the item is visible; else it is hidden.

public abstract MenuItem setAlphabeticShortcut (char alphaChar)

Change the alphabetic shortcut associated with this item. The shortcut will be triggered when the key that generates the given character is pressed alone or along with with the alt key. Case is not significant and shortcut characters will be displayed in lower case. Note that menu items with the characters '\b' or '\n' as shortcuts will get triggered by the Delete key or Carriage Return key, respectively. See Menu for the menu types that support shortcuts.
Parameters alphaChar The alphabetic shortcut key. This is the shortcut when using a keyboard with alphabetic keys. Returns

This Item so additional setters can be called.

public abstract MenuItem setCheckable (boolean checkable)

Control whether this item can display a check mark. Setting this does not actually display a check mark (see setChecked(boolean) for that); rather, it ensures there is room in the item in which to display a check mark. See Menu for the menu types that support check marks.
Parameters checkable Set to true to allow a check mark, false to disallow. The default is false. Returns

This Item so additional setters can be called.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

public abstract MenuItem setChecked (boolean checked)

Control whether this item is shown with a check mark. Note that you must first have enabled checking with setCheckable(boolean) or else the check mark will not appear. If this item is a member of a group that contains mutually-exclusive items (set via setGroupCheckable(int, boolean, boolean), the other items in the group will be unchecked.
Parameters checked Set to true to display a check mark, false to hide it. The default value is false. Returns

This Item so additional setters can be called.

public abstract MenuItem setEnabled (boolean enabled)

Sets whether the menu item is enabled. Disabling a menu item will not allow it to be invoked via its shortcut. The menu item will still be visible.
Parameters enabled If true then the item will be invokable; if false it is won't be invokable. Returns

This Item so additional setters can be called.

public abstract MenuItem setIcon (Drawable icon)

Change the icon associated with this item. This icon will not always be shown, so the title should be sufficient in describing this item. See Menu for the menu types that support icons.
Parameters icon The new icon (as a Drawable) to be displayed. Returns

This Item so additional setters can be called.

public abstract MenuItem setIcon (int iconRes)

Change the icon associated with this item. This icon will not always be shown, so the title should be sufficient in describing this item. See Menu for the menu types that support icons. This method will set the resource ID of the icon which will be used to lazily get the Drawable when this item is being shown.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Parameters iconRes The new icon (as a resource ID) to be displayed. Returns

This Item so additional setters can be called.

public abstract MenuItem setIntent (Intent intent)

Change the Intent associated with this item. By default there is no Intent associated with a menu item. If you set one, and nothing else handles the item, then the default behavior will be to call startActivity(Intent) with the given Intent. Note that setIntent() can not be used with the versions of add(int) that take a Runnable, because run() does not return a value so there is no way to tell if it handled the item. In this case it is assumed that the Runnable always handles the item, and the intent will never be started.
Parameters intent The Intent to associated with the item. This Intent object is not copied, so be careful not to modify it later. Returns

This Item so additional setters can be called.

public abstract MenuItem setNumericShortcut (char numericChar)

Change the numeric shortcut associated with this item.


Parameters numericChar The numeric shortcut key. This is the shortcut when using a 12-key (numeric) keyboard. Returns

This Item so additional setters can be called.

public abstract MenuItem setOnMenuItemClickListener (MenuItem.OnMenuItemClickListener menuItemClickListener)

Set a custom listener for invocation of this menu item. In most situations, it is more efficient and easier to use onOptionsItemSelected(MenuItem) or onContextItemSelected(MenuItem).
Parameters menuItemClickListener The object to receive invokations.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Returns

This Item so additional setters can be called.

public abstract MenuItem setShortcut (char numericChar, char alphaChar)

Change both the numeric and alphabetic shortcut associated with this item. Note that the shortcut will be triggered when the key that generates the given character is pressed alone or along with with the alt key. Also note that case is not significant and that alphabetic shortcut characters will be displayed in lower case.
Parameters numericChar The numeric shortcut key. This is the shortcut when using a numeric (e.g., 12-key) keyboard. alphaChar The alphabetic shortcut key. This is the shortcut when using a keyboard with alphabetic keys. Returns

This Item so additional setters can be called.

public abstract MenuItem setTitle (int title)

Change the title associated with this item. Some menu types do not sufficient space to show the full title, and instead a condensed title is preferred. See Menu for more information.
Parameters title The resource id of the new text to be displayed. Returns

This Item so additional setters can be called.

public abstract MenuItem setTitle (CharSequence title)

Change the title associated with this item.


Parameters title The new text to be displayed. Returns

This Item so additional setters can be called.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

public abstract MenuItem setTitleCondensed (CharSequence title)

Change the condensed title associated with this item. The condensed title is used in situations where the normal title may be too long to be displayed.
Parameters title The new text to be displayed as the condensed title. Returns

This Item so additional setters can be called.

public abstract MenuItem setVisible (boolean visible)

Sets the visibility of the menu item. Even if a menu item is not visible, it may still be invoked via its shortcut (to completely disable an item, set it to invisible and disabled).
Parameters visible If true then the item will be visible; if false it is hidden. Returns

This Item so additional setters can be called.

ContextMenu
implements Menu android.view.ContextMenu

Class Overview
Extension of Menu for context menus providing functionality to modify the header of the context menu. Context menus do not support item shortcuts and item icons. To show a context menu on long click, most clients will want to call registerForContextMenu(View) and override onCreateContextMenu(ContextMenu, View, ContextMenu.ContextMenuInfo).

Summary
Nested Classes interface ContextMenu.ContextMenuInfo Additional information regarding the creation of the context menu. [Expand]

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Inherited Constants From interface android.view.Menu int CATEGORY_ALTERNATIVE Category code for the order integer for items/groups that are alternative actions on the data that is currently displayed -- or/add this with your base value. Category code for the order integer for items/groups that are part of a container -- or/add this with your base value. Category code for the order integer for items/groups that are user-supplied secondary (infrequently used) options -- or/add this with your base value. Category code for the order integer for items/groups that are provided by the system -- or/add this with your base value. First value for group and item identifier integers. Flag for performShortcut(int, KeyEvent, int): if set, always close the menu after executing the shortcut. Flag for addIntentOptions(int, int, int, ComponentName, Intent[], Intent, int, MenuItem[]): if set, do not automatically remove any existing menu items in the same group. Flag for performShortcut(int, KeyEvent, int): if set, do not close the menu after executing the shortcut. Value to use for group and item identifier integers when you don't care about them. Public Methods clearHeader() abstract void Clears the header of the context menu. setHeaderIcon(Drawable icon) abstract ContextMenu Sets the context menu header's icon to the icon given in icon Drawable. setHeaderIcon(int iconRes) abstract ContextMenu Sets the context menu header's icon to the icon given in iconRes resource id. setHeaderTitle(int titleRes) abstract ContextMenu Sets the context menu header's title to the title given in titleRes resource identifier. A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

int CATEGORY_CONTAINER

int CATEGORY_SECONDARY

int CATEGORY_SYSTEM int FIRST int FLAG_ALWAYS_PERFORM_CLOSE

int FLAG_APPEND_TO_GROUP

int FLAG_PERFORM_NO_CLOSE

int NONE

setHeaderTitle(CharSequence title) abstract ContextMenu Sets the context menu header's title to the title given in title. setHeaderView(View view) abstract ContextMenu Sets the header of the context menu to the View given in view. [Expand] Inherited Methods From interface android.view.Menu abstract add(int titleRes) MenuItem Add a new item to the menu. abstract add(int groupId, int itemId, int order, CharSequence title) MenuItem Add a new item to the menu. add(int groupId, int itemId, int order, int titleRes) abstract MenuItem Variation on add(int, int, int, CharSequence) that takes a string resource identifier instead of the string itself. abstract add(CharSequence title) MenuItem Add a new item to the menu. addIntentOptions(int groupId, int itemId, int order, ComponentName caller, Intent[] specifics, Intent intent, int flags, MenuItem[] outSpecificItems) Add a group of menu items corresponding to actions that can be performed for a particular Intent. abstract addSubMenu(int titleRes) SubMenu Add a new sub-menu to the menu. addSubMenu(int groupId, int itemId, int order, int titleRes) abstract SubMenu Variation on addSubMenu(int, int, int, CharSequence) that takes a string resource identifier for the title instead of the string itself. abstract addSubMenu(CharSequence title) SubMenu Add a new sub-menu to the menu. abstract addSubMenu(int groupId, int itemId, int order, CharSequence title) A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

abstract int

SubMenu Add a new sub-menu to the menu. abstract void abstract void clear() Remove all existing items from the menu, leaving it empty as if it had just been created. close() Closes the menu, if open.

abstract findItem(int id) MenuItem Return the menu item with a particular identifier. abstract getItem(int index) MenuItem Gets the menu item at the given index. abstract boolean abstract boolean abstract boolean abstract boolean abstract void abstract void abstract void abstract void hasVisibleItems() Return whether the menu currently has item items that are visible. isShortcutKey(int keyCode, KeyEvent event) Is a keypress one of the defined shortcut keys for this window. performIdentifierAction(int id, int flags) Execute the menu item action associated with the given menu identifier. performShortcut(int keyCode, KeyEvent event, int flags) Execute the menu item action associated with the given shortcut character. removeGroup(int groupId) Remove all items in the given group. removeItem(int id) Remove the item with the given identifier. setGroupCheckable(int group, boolean checkable, boolean exclusive) Control whether a particular group of items can show a check mark. setGroupEnabled(int group, boolean enabled) Enable or disable all menu items that are in the given group.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

abstract void

setGroupVisible(int group, boolean visible) Show or hide all menu items that are in the given group. setQwertyMode(boolean isQwerty)

abstract void

Control whether the menu should be running in qwerty mode (alphabetic shortcuts) or 12-key mode (numeric shortcuts). size() Get the number of items in the menu.

abstract int

Public Methods
public abstract void clearHeader ()

Clears the header of the context menu.


public abstract ContextMenu setHeaderIcon (Drawable icon)

Sets the context menu header's icon to the icon given in icon Drawable.
Parameters icon The Drawable used for the icon. Returns

This ContextMenu so additional setters can be called.

public abstract ContextMenu setHeaderIcon (int iconRes)

Sets the context menu header's icon to the icon given in iconRes resource id.
Parameters iconRes The resource identifier used for the icon. Returns

This ContextMenu so additional setters can be called.

public abstract ContextMenu setHeaderTitle (int titleRes)

Sets the context menu header's title to the title given in titleRes resource identifier.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Parameters titleRes The string resource identifier used for the title. Returns

This ContextMenu so additional setters can be called.

public abstract ContextMenu setHeaderTitle (CharSequence title)

Sets the context menu header's title to the title given in title.
Parameters title The character sequence used for the title. Returns

This ContextMenu so additional setters can be called.

public abstract ContextMenu setHeaderView (View view)

Sets the header of the context menu to the View given in view. This replaces the header title and icon (and those replace this).
Parameters view The View used for the header. Returns

This ContextMenu so additional setters can be called.

SubMenu
implements Menu android.view.SubMenu

Class Overview
Subclass of Menu for sub menus. Sub menus do not support item icons, or nested sub menus.

Summary
[Expand]

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Inherited Constants From interface android.view.Menu int CATEGORY_ALTERNATIVE Category code for the order integer for items/groups that are alternative actions on the data that is currently displayed -- or/add this with your base value. Category code for the order integer for items/groups that are part of a container -- or/add this with your base value. Category code for the order integer for items/groups that are user-supplied secondary (infrequently used) options -- or/add this with your base value. Category code for the order integer for items/groups that are provided by the system -- or/add this with your base value. First value for group and item identifier integers. Flag for performShortcut(int, KeyEvent, int): if set, always close the menu after executing the shortcut. Flag for addIntentOptions(int, int, int, ComponentName, Intent[], Intent, int, MenuItem[]): if set, do not automatically remove any existing menu items in the same group. Flag for performShortcut(int, KeyEvent, int): if set, do not close the menu after executing the shortcut. Value to use for group and item identifier integers when you don't care about them. Public Methods abstract void clearHeader() Clears the header of the submenu.

int CATEGORY_CONTAINER

int CATEGORY_SECONDARY

int CATEGORY_SYSTEM int FIRST int FLAG_ALWAYS_PERFORM_CLOSE

int FLAG_APPEND_TO_GROUP

int FLAG_PERFORM_NO_CLOSE

int NONE

abstract getItem() MenuItem Gets the MenuItem that represents this submenu in the parent menu. abstract setHeaderIcon(Drawable icon) SubMenu Sets the submenu header's icon to the icon given in icon Drawable. abstract setHeaderIcon(int iconRes) SubMenu Sets the submenu header's icon to the icon given in iconRes resource id. A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

abstract setHeaderTitle(int titleRes) SubMenu Sets the submenu header's title to the title given in titleRes resource identifier. abstract setHeaderTitle(CharSequence title) SubMenu Sets the submenu header's title to the title given in title. abstract setHeaderView(View view) SubMenu Sets the header of the submenu to the View given in view. abstract setIcon(Drawable icon) SubMenu Change the icon associated with this submenu's item in its parent menu. abstract setIcon(int iconRes) SubMenu Change the icon associated with this submenu's item in its parent menu. [Expand] Inherited Methods From interface android.view.Menu abstract add(int titleRes) MenuItem Add a new item to the menu. abstract add(int groupId, int itemId, int order, CharSequence title) MenuItem Add a new item to the menu. add(int groupId, int itemId, int order, int titleRes) abstract MenuItem Variation on add(int, int, int, CharSequence) that takes a string resource identifier instead of the string itself. abstract add(CharSequence title) MenuItem Add a new item to the menu. addIntentOptions(int groupId, int itemId, int order, ComponentName caller, Intent[] specifics, Intent intent, int flags, MenuItem[] outSpecificItems) Add a group of menu items corresponding to actions that can be performed for a particular Intent. abstract addSubMenu(int titleRes) SubMenu A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

abstract int

Add a new sub-menu to the menu. addSubMenu(int groupId, int itemId, int order, int titleRes) abstract SubMenu Variation on addSubMenu(int, int, int, CharSequence) that takes a string resource identifier for the title instead of the string itself. abstract addSubMenu(CharSequence title) SubMenu Add a new sub-menu to the menu. abstract addSubMenu(int groupId, int itemId, int order, CharSequence title) SubMenu Add a new sub-menu to the menu. abstract void abstract void clear() Remove all existing items from the menu, leaving it empty as if it had just been created. close() Closes the menu, if open.

abstract findItem(int id) MenuItem Return the menu item with a particular identifier. abstract getItem(int index) MenuItem Gets the menu item at the given index. abstract boolean abstract boolean abstract boolean abstract boolean abstract void hasVisibleItems() Return whether the menu currently has item items that are visible. isShortcutKey(int keyCode, KeyEvent event) Is a keypress one of the defined shortcut keys for this window. performIdentifierAction(int id, int flags) Execute the menu item action associated with the given menu identifier. performShortcut(int keyCode, KeyEvent event, int flags) Execute the menu item action associated with the given shortcut character. removeGroup(int groupId) Remove all items in the given group.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

abstract void abstract void abstract void abstract void

removeItem(int id) Remove the item with the given identifier. setGroupCheckable(int group, boolean checkable, boolean exclusive) Control whether a particular group of items can show a check mark. setGroupEnabled(int group, boolean enabled) Enable or disable all menu items that are in the given group. setGroupVisible(int group, boolean visible) Show or hide all menu items that are in the given group. setQwertyMode(boolean isQwerty)

abstract void

Control whether the menu should be running in qwerty mode (alphabetic shortcuts) or 12-key mode (numeric shortcuts). size() Get the number of items in the menu.

abstract int

Public Methods
public abstract void clearHeader ()

Clears the header of the submenu.


public abstract MenuItem getItem ()

Gets the MenuItem that represents this submenu in the parent menu. Use this for setting additional item attributes.
Returns

The MenuItem that launches the submenu when invoked.

public abstract SubMenu setHeaderIcon (Drawable icon)

Sets the submenu header's icon to the icon given in icon Drawable.
Parameters icon The Drawable used for the icon.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Returns

This SubMenu so additional setters can be called.

public abstract SubMenu setHeaderIcon (int iconRes)

Sets the submenu header's icon to the icon given in iconRes resource id.
Parameters iconRes The resource identifier used for the icon. Returns

This SubMenu so additional setters can be called.

public abstract SubMenu setHeaderTitle (int titleRes)

Sets the submenu header's title to the title given in titleRes resource identifier.
Parameters titleRes The string resource identifier used for the title. Returns

This SubMenu so additional setters can be called.

public abstract SubMenu setHeaderTitle (CharSequence title)

Sets the submenu header's title to the title given in title.


Parameters title The character sequence used for the title. Returns

This SubMenu so additional setters can be called.

public abstract SubMenu setHeaderView (View view)

Sets the header of the submenu to the View given in view. This replaces the header title and icon (and those replace this).
Parameters view The View used for the header.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Returns

This SubMenu so additional setters can be called.

public abstract SubMenu setIcon (Drawable icon)

Change the icon associated with this submenu's item in its parent menu.
Parameters icon The new icon (as a Drawable) to be displayed. Returns

This SubMenu so additional setters can be called.

See Also
setIcon(Drawable)

public abstract SubMenu setIcon (int iconRes)

Change the icon associated with this submenu's item in its parent menu.
Parameters iconRes The new icon (as a resource ID) to be displayed. Returns

This SubMenu so additional setters can be called.

See Also
setIcon(int)

Menu Resource
1. Creating Menus A menu resource defines an application menu (Options Menu, Context Menu, or Sub Menu) that can be inflated with MenuInflater. file location:
res/menu/filename.xml

The filename will be used as the resource ID. compiled resource datatype:
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Resource pointer to a Menu (or subclass) resource. resource reference: In Java: R.menu.filename In XML: @[package:]menu.filename syntax:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@[+][package:]id/resource_name" android:menuCategory=["container" | "system" | "secondary" | "alternative"] android:orderInCategory="integer" android:title="string" android:titleCondensed="string" android:icon="@[package:]drawable/drawable_resource_name" android:alphabeticShortcut="string" android:numericShortcut="string" android:checkable=["true" | "false"] android:visible=["visible" | "invisible" | "gone"] android:enabled=["enabled" | "disabled"] /> <group android:id="@[+][package:]id/resource name" android:menuCategory=["container" | "system" | "secondary" | "alternative"] android:orderInCategory="integer" android:checkableBehavior=["none" | "all" | "single"] android:visible=["visible" | "invisible" | "gone"] android:enabled=["enabled" | "disabled"] > <item /> </group> <item > <menu> <item /> </menu> </item> </menu>

elements:
<menu>

Required. This must be the root node. Contains <item> and/or <group> elements. attributes:
xmlns:android

String. Required. Defines the XML namespace, which must be "http://schemas.android.com/apk/res/android".


<group>

A menu group (to create a collection of items that share traits, such as whether they are visible, enabled, or checkable). Contains one or more <item> elements. Must be a child of a <menu> element. attributes:
android:id

Resource ID. A unique resource ID. To create a new resource ID for this item, use the form: "@+id/name". The plus symbol indicates that this should be created as a new ID.
android:menuCategory

Keyword. Value corresponding to Menu CATEGORY_* constants, which define the group's priority. Valid values: Value Description
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

container

For groups that are part of a container. system For groups that are provided by the system. secondary For groups that are user-supplied secondary (infrequently used) options. alternative For groups that are alternative actions on the data that is currently displayed.
android:orderInCategory

Integer. The default order of the items within the category.


android:checkableBehavior

Keyword. The type of checkable behavior for the group. Valid values: Value Description none Not checkable all All items can be checked (use checkboxes) single Only one item can be checked (use radio buttons)
android:visible

Boolean. "true" if the group is visible.


android:enabled

Boolean. "true" if the group is enabled.


<item>

A menu item. May contain a <menu> element (for a Sub Menu). Must be a child of a <menu> or <group> element. attributes:
android:id

Resource ID. A unique resource ID. To create a new resource ID for this item, use the form: "@+id/name". The plus symbol indicates that this should be created as a new ID.
android:menuCategory

Keyword. Value corresponding to Menu CATEGORY_* constants, which define the item's priority. Valid values: Value Description container For items that are part of a container. system For items that are provided by the system. secondary For items that are user-supplied secondary (infrequently used) options. alternative For items that are alternative actions on the data that is currently displayed.
android:orderInCategory

Integer. The order of "importance" of the item, within a group.


android:title

String. The menu title.


android:titleCondensed

String. A condensed title, for situations in which the normal title is too long.
android:icon

Drawable resource. An image to be used as the menu item icon.


android:alphabeticShortcut

Char. A character for the alphabetic shortcut key.


android:numericShortcut

Integer. A number for the numeric shortcut key.


android:checkable

Boolean. "true" if the item is checkable.


android:checked

Boolean. "true" if the item is checked by default.


android:visible

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Boolean. "true" if the item is visible by default.


android:enabled

Boolean. "true" if the item is enabled by default. example: XML file saved at res/menu/example_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/item1" android:title="@string/item1" android:icon="@drawable/group_item1_icon" /> <group android:id="@+id/group"> <item android:id="@+id/group_item1" android:title="@string/group_item1" android:icon="@drawable/group_item1_icon" /> <item android:id="@+id/group_item2" android:title="G@string/group_item2" android:icon="@drawable/group_item2_icon" /> </group> <item android:id="@+id/submenu" android:title="@string/submenu_title" > <menu> <item android:id="@+id/submenu_item1" android:title="@string/submenu_item1" /> </menu> </item> </menu>

This application code will inflate the menu from the onCreateOptionsMenu(Menu) callback:
public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.example_menu, menu); return true; }

Applying Styles and Themes


In this document
1. Defining Styles 1. Inheritance 2. Style Properties 2. Applying Styles and Themes to the UI 1. Apply a style to a View 2. Apply a theme to an Activity or application 3. Using Platform Styles and Themes

See also
1. Style and Theme Resources 2. R.style for Android styles and themes 3. R.attr for all style attributes A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

A style is a collection of properties that specify the look and format for a View or window. A style can specify properties such as height, padding, font color, font size, background color, and much more. A style is defined in an XML resource that is separate from the XML that specifies the layout. Styles in Android share a similar philosophy to cascading stylesheets in web designthey allow you to separate the design from the content. For example, by using a style, you can take this layout XML:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#00FF00" android:typeface="monospace" android:text="@string/hello" />

And turn it into this:


<TextView style="@style/CodeFont" android:text="@string/hello" />

All of the attributes related to style have been removed from the layout XML and put into a style definition called CodeFont, which is then applied with the style attribute. You'll see the definition for this style in the following section. A theme is a style applied to an entire Activity or application, rather than an individual View (as in the example above). When a style is applied as a theme, every View in the Activity or application will apply each style property that it supports. For example, you can apply the same CodeFont style as a theme for an Activity and then all text inside that Activity will have green monospace font.

Defining Styles
To create a set of styles, save an XML file in the res/values/ directory of your project. The name of the XML file is arbitrary, but it must use the .xml extension and be saved in the res/values/ folder. The root node of the XML file must be <resources>. For each style you want to create, add a <style> element to the file with a name that uniquely identifies the style (this attribute is required). Then add an <item> element for each property of that style, with a name that declares the style property and a value to go with it (this attribute is required). The value for the <item> can be a keyword string, a hex color, a reference to another resource type, or other value depending on the style property. Here's an example file with a single style:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="CodeFont" parent="@android:style/TextAppearance.Medium"> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:textColor">#00FF00</item> <item name="android:typeface">monospace</item> </style> </resources>

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Each child of the <resources> element is converted into an application resource object at compile-time, which can be referenced by the value in the <style> element's name attribute. This example style can be referenced from an XML layout as @style/CodeFont (as demonstrated in the introduction above). The parent attribute in the <style> element is optional and specifies the resource ID of another style from which this style should inherit properties. You can then override the inherited style properties if you want to. Remember, a style that you want to use as an Activity or application theme is defined in XML exactly the same as a style for a View. A style such as the one defined above can be applied as a style for a single View or as a theme for an entire Activity or application. How to apply a style for a single View or as an application theme is discussed later.

Inheritance
The parent attribute in the <style> element lets you specify a style from which your style should inherit properties. You can use this to inherit properties from an existing style and then define only the properties that you want to change or add. You can inherit from styles that you've created yourself or from styles that are built into the platform. (See Using Platform Styles and Themes, below, for information about inheriting from styles defined by the Android platform.) For example, you can inherit the Android platform's default text appearance and then modify it:
<style name="GreenText" parent="@android:style/TextAppearance"> <item name="android:textColor">#00FF00</item> </style>

If you want to inherit from styles that you've defined yourself, you do not have to use the parent attribute. Instead, just prefix the name of the style you want to inherit to the name of your new style, separated by a period. For example, to create a new style that inherits the CodeFont style defined above, but make the color red, you can author the new style like this:
<style name="CodeFont.Red"> <item name="android:textColor">#FF0000</item> </style>

Notice that there is no parent attribute in the <style> tag, but because the name attribute begins with the CodeFont style name (which is a style that you have created), this style inherits all style properties from that style. This style then overrides the android:textColor property to make the text red. You can reference this new style as @style/CodeFont.Red. You can continue inheriting like this as many times as you'd like, by chaining names with periods. For example, you can extend CodeFont.Red to be bigger, with:
<style name="CodeFont.Red.Big"> <item name="android:textSize">30sp</item> </style>

This inherits from both CodeFont and CodeFont.Red styles, then adds the android:textSize property. Note: This technique for inheritance by chaining together names only works for styles defined by your own resources. You can't inherit Android built-in styles this way. To reference a built-in style, such as TextAppearance, you must use the parent attribute.
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

Style Properties
Now that you understand how a style is defined, you need to learn what kind of style propertiesdefined by the <item> elementare available. You're probably familiar with some already, such as layout_width and textColor. Of course, there are many more style properties you can use. The best place to find properties that apply to a specific View is the corresponding class reference, which lists all of the supported XML attributes. For example, all of the attributes listed in the table of TextView XML attributes can be used in a style definition for a TextView element (or one of its subclasses). One of the attributes listed in the reference is android:inputType, so where you might normally place the android:inputType attribute in an <EditText> element, like this:
<EditText android:inputType="number" ... />

You can instead create a style for the EditText element that includes this property:
<style name="Numbers"> <item name="android:inputType">number</item> ... </style>

So your XML for the layout can now implement this style:
<EditText style="@style/Numbers" ... />

This simple example may look like more work, but when you add more style properties and factor-in the ability to re-use the style in various places, the pay-off can be huge. For a reference of all available style properties, see the R.attr reference. Keep in mind that all View objects don't accept all the same style attributes, so you should normally refer to the specific View class for supported style properties. However, if you apply a style to a View that does not support all of the style properties, the View will apply only those properties that are supported and simply ignore the others. Some style properties, however, are not supported by any View element and can only be applied as a theme. These style properties apply to the entire window and not to any type of View. For example, style properties for a theme can hide the application title, hide the status bar, or change the window's background. These kind of style properties do not belong to any View object. To discover these theme-only style properties, look at the R.attr reference for attributes that begin with window. For instance, windowNoTitle and windowBackground are style properties that are effective only when the style is applied as a theme to an Activity or application. See the next section for information about applying a style as a theme. Note: Don't forget to prefix the property names in each <item> element with the android: namespace. For example: <item name="android:inputType">.

Applying Styles and Themes to the UI


There are two ways to set a style:
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

To an individual View, by adding the style attribute to a View element in the XML for your layout. Or, to an entire Activity or application, by adding the android:theme attribute to the <activity> or <application> element in the Android manifest.

When you apply a style to a single View in the layout, the properties defined by the style are applied only to that View. If a style is applied to a ViewGroup, the child View elements will not inherit the style properties only the element to which you directly apply the style will apply its properties. However, you can apply a style so that it applies to all View elementsby applying the style as a theme. To apply a style definition as a theme, you must apply the style to an Activity or application in the Android manifest. When you do so, every View within the Activity or application will apply each property that it supports. For example, if you apply the CodeFont style from the previous examples to an Activity, then all View elements that support the text style properties will apply them. Any View that does not support the properties will ignore them. If a View supports only some of the properties, then it will apply only those properties.

Apply a style to a View


Here's how to set a style for a View in the XML layout:
<TextView style="@style/CodeFont" android:text="@string/hello" />

Now this TextView will be styled as defined by the style named CodeFont. (See the sample above, in Defining Styles.) Note: The style attribute does not use the android: namespace prefix.

Apply a theme to an Activity or application


To set a theme for all the activities of your application, open the AndroidManifest.xml file and edit the <application> tag to include the android:theme attribute with the style name. For example:
<application android:theme="@style/CustomTheme">

If you want a theme applied to just one Activity in your application, then add the android:theme attribute to the <activity> tag instead. Just as Android provides other built-in resources, there are many pre-defined themes that you can use, to avoid writing them yourself. For example, you can use the Dialog theme and make your Activity appear like a dialog box:
<activity android:theme="@android:style/Theme.Dialog">

Or if you want the background to be transparent, use the Translucent theme:


<activity android:theme="@android:style/Theme.Translucent">

If you like a theme, but want to tweak it, just add the theme as the parent of your custom theme. For example, you can modify the traditional dialog theme to use your own background image like this:
A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

<style name="CustomDialogTheme" parent="@android:style/Theme.Dialog"> <item name="android:windowBackground">@drawable/custom_dialog_background</item> </style>

Now use CustomDialogTheme instead of Theme.Dialog inside the Android Manifest:


<activity android:theme="@style/CustomDialogTheme">

Using Platform Styles and Themes


The Android platform provides a large collection of styles and themes that you can use in your applications. You can find a reference of all available styles in the R.style class. To use the styles listed here, replace all underscores in the style name with a period. For example, you can apply the Theme_NoTitleBar theme with "@android:style/Theme.NoTitleBar". The R.style reference, however, is not well documented and does not thoroughly describe the styles, so viewing the actual source code for these styles and themes will give you a better understanding of what style properties each one provides. For a better reference to the Android styles and themes, see the following source code:

Android Styles (styles.xml) Android Themes (themes.xml)

These files will help you learn through example. For instance, in the Android themes source code, you'll find a declaration for <style name="Theme.Dialog">. In this definition, you'll see all of the properties that are used to style dialogs that are used by the Android framework. For more information about the syntax used to create styles in XML, see Available Resource Types: Style and Themes. For a reference of available style attributes that you can use to define a style or theme (e.g., "windowBackground" or "textAppearance"), see R.attr or the respective View class for which you are creating a style.

A7, Stephanos Tower, Eachamukku, Kakkanadu,Kochi

You might also like