49

I'm looking to get the pointing reference from an attribute via code. In my xml layouts I can easily get the referenced drawable like this:

android:background="?attr/listItemBackground"

The attribute reference is set by my Theme. I'm looking to see if it's possible to get that referenced drawable via code.

I can work around this by creating style attr and reading the value inside a custom view but in this case I want to figure out if this is possible without doing all that. I would think it would be possible but I haven't found ways to get that attribute reference.

Thanks!

Jona
  • 13,325
  • 15
  • 86
  • 129

2 Answers2

114

This is how you do it:

// Create an array of the attributes we want to resolve
// using values from a theme
int[] attrs = new int[] { R.attr.listItemBackground /* index 0 */};

// Obtain the styled attributes. 'themedContext' is a context with a
// theme, typically the current Activity (i.e. 'this')
TypedArray ta = themedContext.obtainStyledAttributes(attrs);

// To get the value of the 'listItemBackground' attribute that was
// set in the theme used in 'themedContext'. The parameter is the index
// of the attribute in the 'attrs' array. The returned Drawable
// is what you are after
Drawable drawableFromTheme = ta.getDrawable(0 /* index */);

// Finally, free the resources used by TypedArray
ta.recycle();
TouchBoarder
  • 6,422
  • 2
  • 52
  • 60
Martin Nordholts
  • 10,338
  • 2
  • 39
  • 43
0

Shouldn't you use:

android:background="@drawable/listItemBackground"

And then:

myImageButton.getBackgroundDrawable()

Or maybe I didn't understand ...

user_4685247
  • 2,878
  • 2
  • 17
  • 43
Mathieu de Brito
  • 2,536
  • 2
  • 26
  • 30
  • Well the idea is that I want to access the referenced resource from the attr/listItemBackground which has the proper reference set via theme.xml Getting an already set background from a view is not the issue :) – Jona Feb 22 '12 at 16:35
  • Ok got it ! I think it's possible by trying to get directly the "?attr/listItemBackground" from context.getResources(). But I also think there are better solutions than referencing drawables. May we discuss about the main idea ? ( or not if you think that this solution is the best ) – Mathieu de Brito Feb 22 '12 at 16:40
  • Well, I have a complex yet organized themes for my app. But this one case I wanted to see if it was possible and how it would be possible to be used. You would think res.getDrawable(R.attr.listItemBackground) would do it since the Resource is already properly set with the current set Theme and pointing to the correct Resource. But it's a bit more complex than that it seems... – Jona Feb 22 '12 at 16:45
  • Yeah, res.getDrawable(R.attr.listItemBackground) was what I thought ... You are using references so that when you change the theme, everything is configured in your attr resource ? For this, I would create a custom View/Drawable which select the value from the current Theme. Something like : `android:background="myCustomView"` I don't remember how to write it but the idea is here – Mathieu de Brito Feb 22 '12 at 17:10