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

1

75

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!

Darksome answered 11/3, 2011 at 15:39 Comment(8)
Can't <include /> work here? Or different versions of a theme maybe?Tacet
@alexanderblom: <include> wouldn't help here -- I need the background on the root element of the row layout. "different versions of a theme" is certainly in the realm of possibility, but that's why I'm asking the question. :-)Darksome
yeah at first glance themes separated out -v11 or not seem to be the way to go. Less copy/pasting that doing an entire layout at least :/Amaranthaceous
Also, how about @android:color/transparent ?Lounge
@Roman Nurik: I am assuming you mean that as opposed to @null? That might work. Off the cuff, I would think the styles approach Al outlines would be more "proper", in terms of fidelity with the Android vision -- do you agree? At the end of the day, after all, this question is less about me and more about what I tell my readers, so I want to go the route that Google would like Android developers to use.Darksome
@Darksome definitely more proper to use styles (even better with parent styles), but wanted to close the loop on the @null problem :-)Lounge
@Darksome you should dive into styles already, after you put in the initial time they actually save you tons of time in doing layouts in the long run. I am sure your readers would love some tips ;-)Amaranthaceous
@Darksome just want to chat with you please tell me when and where you will be available? Thanks in advance. Its urgent.Xerxes
Q
57

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.

Quake answered 11/3, 2011 at 18:7 Comment(2)
That seems to work. Of course, now I gotta grok enough about styles and such overall to write a chapter on it, so I can better explain how to use this approach in the book. I am so not looking forward to that. :-( However, thanks for the help!Darksome
Glad to help. Your books have dug me out of enough knowledge holes I'm happy to be able to give something back.Quake

© 2022 - 2024 — McMap. All rights reserved.