Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Android Background & Persistence Menu Bar - Using attribute on older versions causes crash - Is there a theme /pattern approach?

In Android 3.0, the concept of "checked" can be rendered using an "activated" background. This gives you the persistent bar you see when you tap on a list fragment, providing context for the fragment to the list's right (e.g., tapping on a folder in Gmail highlights that folder and opens up another list fragment to show the conversations in that folder).

For example, the fragment samples show stuff like:

setListAdapter(new ArrayAdapter<String>(getActivity(),                     android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES)); 

That resource (android.R.layout.simple_list_item_activated_1) is new to Android 3.0. What makes it "activated" is:

android:background="?android:attr/activatedBackgroundIndicator" 

That attribute value is new to Android 3.0 and will cause you to crash if you try using it on earlier versions of Android, from what I can tell. I want to set the background to this magic value for the 3.0/large/landscape combination, and skip it otherwise.

I can accomplish this by having two separate versions of the layout, one in a -v11 resource set, one in a regular resource set. This is a bit less DRY than I'd like, though, since the bulk of the layout is the same, with only this one attribute either being included or being skipped.

I took a stab at trying to use drawable resource aliases, so the android:background could refer to the alias and the alias would handle the -v11 differentiation, but <bitmap> drawables don't seem to like android:src="@null".

I suspect there's a styles-and-themes approach to this problem, but since I've never fully wrapped my head around those (one of my more embarrassing Android knowledge gaps), I'm not completely sure what to do.

Has anyone worked out a pattern for using "activated" on 3.0 and skipping it on pre-3.0, beyond separate layouts?

Thanks!

like image 754
CommonsWare Avatar asked Mar 11 '11 15:03

CommonsWare


People also ask

What is Android background app?

Foreground refers to the active apps which consume data and are currently running on the mobile. Background refers to the data used when the app is doing some activity in the background, which is not active right now.


1 Answers

Styles are your friend....

Have two values directories, one is values-v11, the other the default values.

Each values directory contains a styles.xml, the difference being that the default values one contains;

<style name="listViewActivatedStyle"/> 

The values-v11 contains;

<style name="listViewActivatedStyle">    <item name="android:background">?android:attr/activatedBackgroundIndicator</item> </style> 

Then you can have a single layout which uses;

style="@style/listViewActivatedStyle" 

and the appropriate one is selected.

like image 194
Al Sutton Avatar answered Oct 18 '22 17:10

Al Sutton