chapter 2 lesson-2 styling the action bar

29
By Kalluri Vinay Reddy Android Club www.facebook.com/vitccandroidclub Introduction to Styling the Action Bar

Upload: kalluri-vinay-reddy

Post on 11-Jan-2017

76 views

Category:

Engineering


4 download

TRANSCRIPT

Page 1: Chapter 2 lesson-2 styling the action bar

ByKalluri Vinay Reddy

Android Clubwww.facebook.com/vitccandroidclub

Introduction to

Styling the Action Bar

Page 2: Chapter 2 lesson-2 styling the action bar

The action bar provides your users a familiar and predictable way to perform actions and navigate your app, but that doesn't mean it needs to look exactly the same as it does in other apps.

Android includes a few built-in activity themes that include "dark" or "light" action bar styles. You can also extend these themes to further customize the look for your action bar.

Page 3: Chapter 2 lesson-2 styling the action bar

Note: If you are using the Support Library APIs for the action bar, then you must use (or override) the Theme.AppCompat family of styles (rather than the Theme.Holo family, available in API level 11 and higher). In doing so, each style property that you declare must be declared twice: once using the platform's style properties (the android: properties) and once using the style properties included in the Support Library.(the appcompat.R.attr properties—the context for these properties is actually your app).

Page 4: Chapter 2 lesson-2 styling the action bar
Page 5: Chapter 2 lesson-2 styling the action bar

Android includes two baseline activity themes that dictate the color for the action bar:

•Theme.Holo for a "dark" theme.•Theme.Holo.Light for a "light" theme.You can apply these themes to your entire app or to individual activities by declaring them in your manifest file with the android:theme attribute for the <application> element or individual <activity>elements.

Page 6: Chapter 2 lesson-2 styling the action bar

Customize the Background

Page 7: Chapter 2 lesson-2 styling the action bar

To change the action bar background, create a custom themefor your activity that overrides the actionBarStyle property.This property points to another style in which you can override the background property to specify a drawable resource for the action bar background.If your app uses navigation tabs or the split action bar, then you can also specify the background for these bars using the backgroundStacked and backgroundSplit properties, respectively.

Caution: It's important that you declare an appropriate parent theme from which your custom theme and style inherit their styles. Without a parent style, your action bar will be without many style properties unless you explicitly declare them yourself.

Page 8: Chapter 2 lesson-2 styling the action bar

• When supporting Android 3.0 and higher only, you can define the action bar's background like this:• res/values/themes.xml

For Android 3.0 and higher only

<?xml version="1.0" encoding="utf-8"?><resources>    <!-- the theme applied to the application or activity -->    <style name="CustomActionBarTheme"           parent="@android:style/Theme.Holo.Light.DarkActionBar">        <item name="android:actionBarStyle">@style/MyActionBar</item>    </style>

    <!-- ActionBar styles -->    <style name="MyActionBar"           parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">        <item name="android:background">@drawable/actionbar_background</item>    </style>

</resources>

Page 9: Chapter 2 lesson-2 styling the action bar

Then apply your theme to your entire app or individual activities:<application android:theme="@style/CustomActionBarTheme" ... />

Page 10: Chapter 2 lesson-2 styling the action bar

For Android 2.1 and higherWhen using the Support Library, the same theme as above must instead look like this:res/values/themes.xml<?xml version="1.0" encoding="utf-8"?><resources>    <!-- the theme applied to the application or activity -->    <style name="CustomActionBarTheme"           parent="@style/Theme.AppCompat.Light.DarkActionBar">        <item name="android:actionBarStyle">@style/MyActionBar</item>

        <!-- Support library compatibility -->        <item name="actionBarStyle">@style/MyActionBar</item>    </style>

    <!-- ActionBar styles -->    <style name="MyActionBar"           parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">        <item name="android:background">@drawable/actionbar_background</item>

        <!-- Support library compatibility -->        <item name="background">@drawable/actionbar_background</item>    </style></resources>

Page 11: Chapter 2 lesson-2 styling the action bar

Customize the Text ColorTo modify the color of text in the action bar, you need to override separate properties for each text element:•Action bar title: Create a custom style that specifies the textColor •property and specify that style for the titleTextStyle property in your custom  actionBarStyle.Note: The custom style applied to titleTextStyle should use TextAppearance.Holo.Widget.ActionBar.Title as the parent style.

•Action bar tabs: Override actionBarTabTextStyle in your activity theme.•Action buttons: Override actionMenuTextColor in your activity theme.

Page 12: Chapter 2 lesson-2 styling the action bar

For Android 3.0 and higher onlyWhen supporting Android 3.0 and higher only, your style XML file might look like this:res/values/themes.xml<?xml version="1.0" encoding="utf-8"?><resources>    <!-- the theme applied to the application or activity -->    <style name="CustomActionBarTheme"           parent="@style/Theme.Holo">        <item name="android:actionBarStyle">@style/MyActionBar</item>        <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>        <item name="android:actionMenuTextColor">@color/actionbar_text</item>    </style>

    <!-- ActionBar styles -->    <style name="MyActionBar"           parent="@style/Widget.Holo.ActionBar">        <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>    </style>

    <!-- ActionBar title text -->    <style name="MyActionBarTitleText"           parent="@style/TextAppearance.Holo.Widget.ActionBar.Title">        <item name="android:textColor">@color/actionbar_text</item>    </style>

    <!-- ActionBar tabs text styles -->    <style name="MyActionBarTabText"           parent="@style/Widget.Holo.ActionBar.TabText">        <item name="android:textColor">@color/actionbar_text</item>    </style></resources>

Page 13: Chapter 2 lesson-2 styling the action bar

•When using the Support Library, your style XML file might look like this:res/values/themes.xml

For Android 2.1 and higher

Page 14: Chapter 2 lesson-2 styling the action bar

<?xml version="1.0" encoding="utf-8"?><resources>    <!-- the theme applied to the application or activity -->    <style name="CustomActionBarTheme"           parent="@style/Theme.AppCompat">        <item name="android:actionBarStyle">@style/MyActionBar</item>        <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>        <item name="android:actionMenuTextColor">@color/actionbar_text</item>

        <!-- Support library compatibility -->        <item name="actionBarStyle">@style/MyActionBar</item>        <item name="actionBarTabTextStyle">@style/MyActionBarTabText</item>        <item name="actionMenuTextColor">@color/actionbar_text</item>    </style>

    <!-- ActionBar styles -->    <style name="MyActionBar"           parent="@style/Widget.AppCompat.ActionBar">        <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>

        <!-- Support library compatibility -->        <item name="titleTextStyle">@style/MyActionBarTitleText</item>    </style>

 

Page 15: Chapter 2 lesson-2 styling the action bar

•   <!-- ActionBar title text -->    <style name="MyActionBarTitleText"           parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">        <item name="android:textColor">@color/actionbar_text</item>        <!-- The textColor property is backward compatible with the Support Library -->    </style>

    <!-- ActionBar tabs text -->    <style name="MyActionBarTabText"           parent="@style/Widget.AppCompat.ActionBar.TabText">        <item name="android:textColor">@color/actionbar_text</item>        <!-- The textColor property is backward compatible with the Support Library -->    </style></resources>

Page 16: Chapter 2 lesson-2 styling the action bar

Customize the Tab Indicator

Page 17: Chapter 2 lesson-2 styling the action bar

To change the indicator used for the navigation tabs, create an activity theme that overrides the actionBarTabStyle property. This property points to another style resource in which you override the background property that should specify a state-list drawable.

Note: A state-list drawable is important so that the tab currently selected indicates its state with a background different than the other tabs. For more information about how to create a drawable resource that handles multiple button states, read the State List documentation.

Page 18: Chapter 2 lesson-2 styling the action bar

res/drawable/actionbar_tab_indicator.xml<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- STATES WHEN BUTTON IS NOT PRESSED -->

    <!-- Non focused states -->    <item android:state_focused="false" android:state_selected="false"          android:state_pressed="false"          android:drawable="@drawable/tab_unselected" />    <item android:state_focused="false" android:state_selected="true"          android:state_pressed="false"          android:drawable="@drawable/tab_selected" />

    <!-- Focused states (such as when focused with a d-pad or mouse hover) -->    <item android:state_focused="true" android:state_selected="false"          android:state_pressed="false"          android:drawable="@drawable/tab_unselected_focused" />    <item android:state_focused="true" android:state_selected="true"          android:state_pressed="false"          android:drawable="@drawable/tab_selected_focused" />

Page 19: Chapter 2 lesson-2 styling the action bar

•<!-- STATES WHEN BUTTON IS PRESSED -->

    <!-- Non focused states -->    <item android:state_focused="false" android:state_selected="false"          android:state_pressed="true"          android:drawable="@drawable/tab_unselected_pressed" />    <item android:state_focused="false" android:state_selected="true"        android:state_pressed="true"        android:drawable="@drawable/tab_selected_pressed" />

    <!-- Focused states (such as when focused with a d-pad or mouse hover) -->    <item android:state_focused="true" android:state_selected="false"          android:state_pressed="true"          android:drawable="@drawable/tab_unselected_pressed" />    <item android:state_focused="true" android:state_selected="true"          android:state_pressed="true"          android:drawable="@drawable/tab_selected_pressed" /></selector>

Page 20: Chapter 2 lesson-2 styling the action bar

res/drawable/actionbar_tab_indicator.xml<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">

<!-- STATES WHEN BUTTON IS NOT PRESSED -->

    <!-- Non focused states -->    <item android:state_focused="false" android:state_selected="false"          android:state_pressed="false"          android:drawable="@drawable/tab_unselected" />    <item android:state_focused="false" android:state_selected="true"          android:state_pressed="false"          android:drawable="@drawable/tab_selected" />

    <!-- Focused states (such as when focused with a d-pad or mouse hover) -->    <item android:state_focused="true" android:state_selected="false"          android:state_pressed="false"          android:drawable="@drawable/tab_unselected_focused" />    <item android:state_focused="true" android:state_selected="true"          android:state_pressed="false"          android:drawable="@drawable/tab_selected_focused" />

Page 21: Chapter 2 lesson-2 styling the action bar

• <!-- STATES WHEN BUTTON IS PRESSED -->

    <!-- Non focused states -->    <item android:state_focused="false" android:state_selected="false"          android:state_pressed="true"          android:drawable="@drawable/tab_unselected_pressed" />    <item android:state_focused="false" android:state_selected="true"        android:state_pressed="true"        android:drawable="@drawable/tab_selected_pressed" />

    <!-- Focused states (such as when focused with a d-pad or mouse hover) -->    <item android:state_focused="true" android:state_selected="false"          android:state_pressed="true"          android:drawable="@drawable/tab_unselected_pressed" />    <item android:state_focused="true" android:state_selected="true"          android:state_pressed="true"          android:drawable="@drawable/tab_selected_pressed" /></selector>

Page 22: Chapter 2 lesson-2 styling the action bar

Overlaying the Action BarBy default, the action bar appears at the top of your activity window, slightly reducing the amount of space available for the rest of your activity's layout. If, during the course of user interaction, you want to hideand show the action bar, you can do so by calling  hide()and show() on the ActionBar. However, this causes your activity to recomputed and redraw the layout based on its new size.

Page 23: Chapter 2 lesson-2 styling the action bar

•To avoid resizing your layout when the action bar hides and shows, you can enable overlay mode for the action bar. When in overlay mode, your activity layout uses all the space available as if the action bar is not there and the system draws the action bar in front of your layout. This obscures some of the layout at the top, but now when the action bar hides or appears, the system does not need to resize your layout and the transition is seamless.

Page 24: Chapter 2 lesson-2 styling the action bar

•Tip: If you want your layout to be partially visible behind the action bar, create a custom style for the action bar with a partially transparent background, such as the one shown in figure 1. For information about how to define the action bar background, read Styling the Action Bar.

Page 25: Chapter 2 lesson-2 styling the action bar
Page 26: Chapter 2 lesson-2 styling the action bar

Enable Overlay ModeFor Android 3.0 and higher onlyIf your minSdkVersion is set to 11 or higher, your custom theme should use Theme.Holo theme (or one of its descendants) as your parent theme. For example:<resources>    <!-- the theme applied to the application or activity -->    <style name="CustomActionBarTheme"           parent="@android:style/Theme.Holo">        <item name="android:windowActionBarOverlay">true</item>    </style>

</resources>

Page 27: Chapter 2 lesson-2 styling the action bar

For Android 2.1 and higherIf your app is using the Support Library for compatibility on devices running versions lower than Android 3.0, your custom theme should use Theme.AppCompat theme (or one of its descendants) as your parent theme. For example:<resources>    <!-- the theme applied to the application or activity -->    <style name="CustomActionBarTheme"           parent="@android:style/Theme.AppCompat">        <item name="android:windowActionBarOverlay">true</item>

        <!-- Support library compatibility -->        <item name="windowActionBarOverlay">true</item>    </style>

</resources>

Page 28: Chapter 2 lesson-2 styling the action bar

Specify Layout Top-marginWhen the action bar is in overlay mode, it might obscure some of your layout that should remain visible. To ensure that such items remain below the action bar at all times, add either margin or padding to the top of the view(s) using the height specified by actionBarSize. For example:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingTop="?android:attr/actionBarSize">    ...

</RelativeLayout>

Page 29: Chapter 2 lesson-2 styling the action bar

If you're using the Support Library for the action bar, you need to remove the android: prefix. For example:<!-- Support library compatibility --><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingTop="?attr/actionBarSize">    ...

</RelativeLayout>