Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for Retrieving Value from Nested Style Resource?

Since DrawerLayout is part of the chrome of an Android activity, a likely background color for the drawer would seem to be the background color of the action bar, so they match. Hence, I'd like to set up the ListView that is the drawer contents to have the same background color as the action bar, and I'd like to do that in the layout XML that defines the ListView.

And here I get lost in the maze of twisty little passages that is the Android style system...

I know that ?android:attr/ syntax allows you to refer, by reference, to a value defined in the theme being used by the activity (e.g., ?android:attr/activatedBackgroundIndicator as the background for a list item row to work with the "activated" state).

I know that android:actionBarStyle is where a theme points to the style to be used to style the action bar itself, and on that nested(?) style, android:background is the background used for the action bar.

What I don't know is how to craft an ?android:attr/, to be applied to a ListView, that pulls the background from the action bar's defined style.

Is this possible? If so, what's the syntax?

My guess is that this is not possible, which is why the official DrawerLayout sample hard-codes the background color...

Thanks!

like image 252
CommonsWare Avatar asked May 30 '13 10:05

CommonsWare


1 Answers

It's not possible unfortunately via XML.

You could do the following in code (untested, but should work):

// Need to manually create android.styleable.ActionBar.
// If you need other attributes, add them
int[] android_styleable_ActionBar = { android.R.attr.background };

// Need to get resource id of style pointed to from actionBarStyle
TypedValue outValue = new TypedValue();
getTheme().resolveAttribute(android.R.attr.actionBarStyle, outValue, true);

// Now get action bar style values...
TypedArray abStyle = getTheme().obtainStyledAttributes(outValue.resourceId,
    android_styleable_ActionBar);
// background is the first attr in the array above so it's index is 0.
Drawable bg = abStyle.getDrawable(0);
abStyle.recycle();
like image 118
Chris Banes Avatar answered Sep 22 '22 19:09

Chris Banes