Langsung ke konten utama

Customizing the Action Bar

[This post is by Nick Butcher, an Android engineer who notices small imperfections, and they annoy him. — Tim Bray]

Since the introduction of the Action Bar design pattern, many applications have adopted it as a way to provide easy access to common actions. In Android 3.0 (or Honeycomb to its friends) this pattern has been baked in as the default navigation paradigm and extended to take advantage of the extra real-estate available on tablets. By using the Action Bar in your Honeycomb-targeted apps, you'll give your users a familiar way to interact with your application. Also, your app will be better prepared to scale across the range of Android devices that will be arriving starting in the Honeycomb era.

Just because Action Bars are familiar, doesn’t mean that they have to be identical! The following code samples and accompanying project demonstrate how to style the Action Bar to match your application’s branding. I’ll demonstrate how to take Honeycomb’s Holo.Light theme and customise it to match this blog’s colour scheme.

Icon

This step is easy; I’ll use the wonderful Android Asset Studio to create an icon in my chosen colour scheme. For extra credit, I’ll use this image as a starting point to create a more branded logo.

Navigation

Next up, the navigation section of the Action Bar operates in three different modes; I’ll tackle each of these in turn.

Standard

The Action Bar’s standard navigation mode simply displays the title of the activity. This doesn’t require any styling... next!

List

To the left, a standard list drop-down; to the right, the effect we want to achieve.

The default styling in list navigation mode has a blue colour scheme. This is evident when touching the collapsed control in both the top bar, and the selection highlight in the expanded list. We can theme this element by overriding android:actionDropDownStyle with a custom style to implement our colour scheme:

 

The above uses a combination of state list drawables and 9 patch images to style the collapsed spinner, the top bar of the expanded list and sets the highlight colour when picking an item.

Tabs

Here are the before-and-after shots on the tab navigation control:

The tab navigation control uses the standard blue colouring. We can apply a custom style to android:actionBarTabStyle to set our own custom drawable that uses our desired palette:

 

Actions

Before-and-after on the individual items in the Action Bar:

The individual action items inherit the default blue background when selected. We can customise this behaviour by overridingandroid:selectableItemBackground and setting a shape drawable with our desired colouring:

@drawable/ad_selectable_background

The overflow menu also needs some attention as when expanded it shows a blue bar at the top of the list. We can override android:popupMenuStyle and set a custom drawable (in fact the very same drawable we previously used for list navigation) for the top of the overflow menu:

 

Selecting items within the overflow menu also show the default selection colour. We can set our customised selection colour by overridingandroid:dropDownListViewStyle:

 

These changes gets us most of the way there but it’s attention to detail that makes an app. Check boxes and radio buttons within menu items in the overflow section are still using the default assets which have a blue highlight. Let’s override them to fit in with our theme:

@drawable/ad_btn_check_holo_light @drawable/ad_btn_radio_holo_light

Background

I’ve left the background transparent as inheriting form Holo.Light works well for our desired palette. If you’d like to customise it you easily override theandroid:background item on the android:actionBarStyle style:

Bringing it all together

Putting all of these components together we can create a custom style:

We can then apply this style to either an individual activity or to the entire application:

 

Note that some of the system styles that we have overridden in this example will affect much more than the Action Bar. For example overriding android:selectableItemBackground will effect many widgets which support a selectable state. This is useful for styling your entire application but be sure to test that your customisations are applied consistently throughout.

Familiar but styled

Customising the action bar is a great way to extend your application’s branding to the standard control components. With this power, as they say, comes great responsibility. When customising the user interface you must take great care to ensure that your application remains legible and navigable. In particular, watch out for highlight colours which contrast poorly with text and provide drawables for all relevant states. Explore this demo application which exercises the functionality offered by the Action Bar and demonstrates how to theme it.



Source android-developers

Komentar

Postingan populer dari blog ini

Motorola Devour specs: 3.1-inch screen, Android 1.6, MotorBLUR

Posted on Tuesday, Feb 2, 2010 by Phil Nickinson We brought you the promotional packaging for the Motorola Devour . But how would you like some specs? Here we go with the phone formerly known as the Calgary, which we're expecting on Verizon anytime now. (Yes, that's a Droid in the picture above. We're told that's just a placeholder.) The biggies: Android 1.6 ( Donut ) with MotoBLUR . A 3.1-inch touchscreen (320x480). 3MP camera. Verizon is requiring a data plan, and the $350 early termination fee is in effect. It comes with an 8GB microSD card. Still no word on price or when it will go on sale, but we're told it could be anytime now. There's a better breakdown and more screenies after the break. 3.1-inch touchscreen at 320x480. Dimensions: 4.55x2.4x0.61 inches. Weight: 6.35 ounces Talk time: Up to 340 minutes Standby time: Up to 440 hours Horizontal slider keyboard. 3-megapixel camera. EVDO Rev. A. WiFi b/g aGPS Audio formats: AAC, AAC+, ...

Andy Rubin Shows Off Android 3.0 On Prototype Motorola Tablet

Devin Coldewey 8 hours ago Andy Rubin is, as I type this, in the middle of a demo of Android 3.0 (AKA Honeycomb) at Dive Into Mobile. The rest of his talk I’ve got written up elsewhere, but this was too cool to pass up. The software is running on a prototype MOTOPAD that looks really slick, though it was only out for a moment and there are but few pictures at the moment. I’ll be updating this post as more information breaks. First thing he showed off was a new lock screen and then what appeared to be a brand new “desktop” with widgets and buttons in the corners. Different UI aesthetic than earlier versions of Android. He then launched the new, 3D-and-vector-based version of Google Maps that should be coming to your phone in just a couple days. Honeycomb will be out “next year,” which is a pretty vague deadline, but if I remember correctly, we’ve heard Q2 for the Moto tablet. Another improvement he notes is the ability to spli...

Huawei's Low-cost IDEOS Android Phone Coming

Huawei may be a name not too popular with many, but the company has its presence in devices that you may be using. Like your Tata Photon+ or a similar USB 3G dongle is mostly made by them. But now, the Chinese giant is waving its flag big-time with three Android phone launches in the country one after the other. The first one will be launched this month in the under Rs. 10k bracket - the IDEOS U8150; as Indian Android enthusiast blog Androidos.in reports . It is expected to be priced at under Rs. 8,000 but does not miss out on key features that we really care for - like a 2.8 inch (that's not it), QVGA (nope, no excitement there), "Capacitive" (YES, that's the one) touch-screen. The second sixer the IDEOS U8150 strikes is Android 2.2 (Froyo) at launch, while competing models like the Samsung Galaxy 5 i5500 or even the Micromax Andro still run Android 2.1 with no confidence that they'll ever get updated to 2.2. A worrisome part about this phone is t...