Read Newer Theme Attributes On Older Platform - android

I am trying to read attribute values from themes and styles which were designed for platforms that are newer than I am running my application on.
Please don't ask why. If you know anything about the libraries I write then you should already know that I like to push the capabilities of the platform :)
I am operating under the presumption that when Android styles are compiled the attribute constants are what is used for the keys and therefore should theoretically be able to be read on any platform somehow. This is what I have observed to be happening with layout XMLs in my other libraries with no trouble.
Here is a base test case which shows the problem. This should be compiled using Android 3.0+.
<resources>
<style name="Theme.BreakMe">
<item name="android:actionBarStyle">#style/Widget.BreakMe</item>
</style>
<style name="Widget.BreakMe" parent="android:Widget">
<item name="android:padding">20dp</item>
</style>
</resources>
The fact that this uses android:actionBarStyle specifically is irreleveant. All that should be understood is that its an attribute which was only available starting with Android 3.0.
Here are the way that I have tried to access these values thus far on platforms prior to Android 3.0.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Break Me"
style="?android:attr/actionBarStyle"
/>
and
<declare-styleable name="Whatever">
<item name="datStyle" format="reference" />
</declare-styleable>
<style name="Theme.BreakMe.Take2">
<item name="datStyle">?android:attr/actionBarSize</item>
</style>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Break Me"
style="?attr/datStyle"
/>
and
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.actionBarStyle, outValue, true);
and
int[] Theme = new int[] { android.R.attr.actionBarSize };
int Theme_actionBarSize = 0;
TypedArray a = context.obtainStyledAttributes(attrs, Theme);
int ref = a.getResourceId(Theme_actionBarSize, 0);
and
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActionBar, android.R.attr.actionBarStyle, 0);
All of them result in this error in LogCat:
E/ResourceType(5618): Style contains key with bad entry: 0x010102ce
The 0x010102ce constant is the attribute value of android.R.attr.actionBarStyle which seems to indicate the platform is rejecting the attribute before I can even get a chance to access its value.
I am looking for any other way to read attributes like this from the Theme. I'm fairly sure that once I've obtained the style reference I won't have trouble reading its attributes.
Is there any possible way to do this?

I am operating under the presumption that when Android styles are compiled the attribute constants are what is used for the keys and therefore should theoretically be able to be read on any platform somehow.
Possibly, though that is not how I am interpreting the C++ source code that raises the error you are seeing. Check out ResTable::Theme::applyStyle() in frameworks/base/libs/utils/ResourceTypes.cpp.
My interpretation is that Android has what amounts to an in-memory table of packages->types->possible entries:
numEntries = curPI->types[t].numEntries;
Your entry index is higher than the highest known entry:
if (e >= numEntries) {
LOGE("Style contains key with bad entry: 0x%08x\n", attrRes);
bag++;
continue;
}
It is possible that they handle this different for android versus other packages -- android uses known values at firmware build time (and your generated entry index is higher, because it is from a newer platform), non-android ones assume anything's valid.
If my guesswork is correct, what you want to do will not work. That being said, my C++ days are seriously in my rear-view mirror, so I may be misinterpreting what I'm seeing.

Perhaps I'm missing the end goal here, but I put together the following example that was able to read out all the attributes without issue on any 2.x device. The example was compiled against a 3.0 targetSdk.
styles.xml (Declare the styles and themes)
<resources>
<style name="Theme.NewFeatures" parent="android:Theme">
<item name="android:actionBarStyle">#style/Widget.MyActionBar</item>
</style>
<style name="Widget.MyActionBar" parent="android:Widget">
<item name="android:padding">20dp</item>
</style>
</resources>
attrs.xml (Declare the attribute groups you wish to obtain at runtime)
<resources>
<declare-styleable name="ActionBarNewFeatures">
<attr name="android:actionBarStyle" />
</declare-styleable>
<declare-styleable name="MyWidgetNewFeatures">
<attr name="android:padding" />
</declare-styleable>
</resources>
AndroidManifest.xml (Apply the custom theme)
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.NewFeatures" >
<activity
android:name=".SomeActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
SomeActivity.java (Go digging for attributes)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TypedArray a = obtainStyledAttributes(R.styleable.ActionBarNewFeatures);
//Get the style ID for the widget
int resid = a.getResourceId(R.styleable.ActionBarNewFeatures_android_actionBarStyle, -1);
a.recycle();
a = obtainStyledAttributes(resid, R.styleable.MyWidgetNewFeatures);
int padding = a.getDimensionPixelSize(R.styleable.MyWidgetNewFeatures_android_padding, -1);
a.recycle();
TextView tv = new TextView(this);
tv.setText(String.format("Padding will be %d px", padding));
setContentView(tv);
}
As long as I compile the example against 3.0 so it can resolved all the attribute names; on every 2.X device/emulator I have this will correctly read into the theme and then into the widget style to get the scaled padding dimension I had set.
Hope I didn't miss something big.

Probably, You must define a few themes. For old devices use folder res/values-v11/themes.xml. See section "Using Holo while supporting Android 2.x" in the http://android-developers.blogspot.com/2012/01/holo-everywhere.html

Related

How to read a TextAppearance attribute programmatically [duplicate]

Currently I'm using either a WebView or a TextView to show some dynamic data coming from a webservice in one of my apps.
If the data contains pure text, it uses the TextView and applies a style from styles.xml.
If the data contains HTML (mostly text and images) it uses the WebView.
However, this WebView is unstyled. Therefor it looks a lot different from the usual TextView.
I've read that it's possible to style the text in a WebView simply by inserting some HTML directly into the data. This sounds easy enough, but I would like to use the data from my Styles.xml as the values required in this HTML so I won't need to change the colors et cetera on two locations if I change my styles.
So, how would I be able to do this? I've done some extensive searching but I have found no way of actually retrieving the different style attributes from your styles.xml. Am I missing something here or is it really not possible to retrieve these values?
The style I'm trying to get the data from is the following:
<style name="font4">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#E3691B</item>
<item name="android:paddingLeft">5dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:textStyle">bold</item>
</style>
I'm mainly interested in the textSize and textColor.
It is possible to retrieve custom styles from styles.xml programmatically.
Define some arbitrary style in styles.xml:
<style name="MyCustomStyle">
<item name="android:textColor">#efefef</item>
<item name="android:background">#ffffff</item>
<item name="android:text">This is my text</item>
</style>
Now, retrieve the styles like this
// The attributes you want retrieved
int[] attrs = {android.R.attr.textColor, android.R.attr.background, android.R.attr.text};
// Parse MyCustomStyle, using Context.obtainStyledAttributes()
TypedArray ta = obtainStyledAttributes(R.style.MyCustomStyle, attrs);
// Fetch the text from your style like this.
String text = ta.getString(2);
// Fetching the colors defined in your style
int textColor = ta.getColor(0, Color.BLACK);
int backgroundColor = ta.getColor(1, Color.BLACK);
// Do some logging to see if we have retrieved correct values
Log.i("Retrieved text:", text);
Log.i("Retrieved textColor as hex:", Integer.toHexString(textColor));
Log.i("Retrieved background as hex:", Integer.toHexString(backgroundColor));
// OH, and don't forget to recycle the TypedArray
ta.recycle()
The answer #Ole has given seems to break when using certain attributes, such as shadowColor, shadowDx, shadowDy, shadowRadius (these are only a few I found, there might be more)
I have no idea as to why this issue occurs, which I am asking about here, but #AntoineMarques coding style seems to solve the issue.
To make this work with any attribute it would be something like this
First, define a stylable to contain the resource ids like so
attrs.xml
<resources>
<declare-styleable name="MyStyle" >
<attr name="android:textColor" />
<attr name="android:background" />
<attr name="android:text" />
</declare-styleable>
</resources>
Then in code you would do this to get the text.
TypedArray ta = obtainStyledAttributes(R.style.MyCustomStyle, R.styleable.MyStyle);
String text = ta.getString(R.styleable.MyStyle_android_text);
The advantage of using this method is, you are retrieving the value by name and not an index.
The answers from Ole and PrivatMamtora didn't work well for me, but this did.
Let's say I wanted to read this style programmatically:
<style name="Footnote">
<item name="android:fontFamily">#font/some_font</item>
<item name="android:textSize">14sp</item>
<item name="android:textColor">#color/black</item>
</style>
I could do it like this:
fun getTextColorSizeAndFontFromStyle(
context: Context,
textAppearanceResource: Int // Can be any style in styles.xml like R.style.Footnote
) {
val typedArray = context.obtainStyledAttributes(
textAppearanceResource,
R.styleable.TextAppearance // These are added to your project automatically.
)
val textColor = typedArray.getColorStateList(
R.styleable.TextAppearance_android_textColor
)
val textSize = typedArray.getDimensionPixelSize(
R.styleable.TextAppearance_android_textSize
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val typeface = typedArray.getFont(R.styleable.TextAppearance_android_fontFamily)
// Do something with the typeface...
} else {
val fontFamily = typedArray.getString(R.styleable.TextAppearance_fontFamily)
?: when (typedArray.getInt(R.styleable.TextAppearance_android_typeface, 0)) {
1 -> "sans"
2 -> "serif"
3 -> "monospace"
else -> null
}
// Do something with the fontFamily...
}
typedArray.recycle()
}
I took some inspiration from Android's TextAppearanceSpan class, you can find it here: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/text/style/TextAppearanceSpan.java
I was not able to get the earlier solutions to work.
My style is:
<style name="Widget.TextView.NumPadKey.Klondike" parent="Widget.TextView.NumPadKey">
<item name="android:textSize">12sp</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textColor">?attr/wallpaperTextColorSecondary</item>
<item name="android:paddingBottom">0dp</item>
</style>
The obtainStyledAttributes() for android.R.attr.textSize gives String results of "12sp" which I then have to parse. For android.R.attr.textColor it gave a resource file XML name. This was much too cumbersome.
Instead, I found an easy way using ContextThemeWrapper.
TextView sample = new TextView(new ContextThemeWrapper(getContext(), R.style.Widget_TextView_NumPadKey_Klondike), null, 0);
This gave me a fully-styled TextView to query for anything I want. For example:
float textSize = sample.getTextSize();
With Kotlin, if you include the androidx.core:core-ktx library in your app/library...
implementation("androidx.core:core-ktx:1.6.0") // Note the -ktx
...you can have either of the following (no need for you to recycle the TypedArray):
// Your desired attribute IDs
val attributes = intArrayOf(R.attr.myAttr1, R.attr.myAttr2, android.R.attr.text)
context.withStyledAttributes(R.style.MyStyle, attributes) {
val intExample = getInt(R.styleable.MyIntAttrName, 0)
val floatExample = getFloat(R.styleable.MyFloatAttrName, 0f)
val enumExample = R.styleable.MyEnumAttrName, MyEnum.ENUM_1 // See Note 1 below
// Similarly, getColor(), getBoolean(), etc.
}
context.withStyledAttributes(R.style.MyStyle, R.styleable.MyStyleable) {
// Like above
}
// attributeSet is provided to you like in the constructor of a custom view
context.withStyledAttributes(attributeSet, R.styleable.MyStyleable) {
// Like above
}
Note 1 (thanks to this answer)
For getting an enum value you can define this extension function:
internal inline fun <reified T : Enum<T>> TypedArray.getEnum(index: Int, default: T) =
getInt(index, -1).let { if (it >= 0) enumValues<T>()[it] else default }
Note 2
The difference between -ktx dependencies like androidx.core:core and androidx.core:core-ktx is that the -ktx variant includes useful extension functions for Kotlin.
Otherwise, they are the same.
Also, thanks to the answer by Ole.
If accepted solution not working for try to rename attr.xml to attrs.xml (worked for me)

cannot combine custom titles with other title features

I am getting this error when calling the setContentView() after
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.maintitlebar);
The code is in the onCreate() for my class which extends ListActivity.
My manifest XML file shows the default AppTheme for the application:
android:theme="#style/AppTheme"
I have updated styles.xml to be:
<resources>
<style name="AppTheme" parent="android:Theme.Light" >
<item name="android:windowNoTitle">true</item>
</style>
</resources>
This seems to be in accordance with the main posts on this error message. I have also cleaned the build, yet I am still getting the above error message. Has anybody any idea what is causing the clash?
I had a similar problem that drove me insane: I have 2 versions of the same app using a shared library project as their common code (over 95% of each app is made of that shared library project): One runs fine, without any problem whatsoever. The other crashes upon start with the same error message & symptoms you describe:
You cannot combine custom titles with other title features
The layout XML files are common as well! So, I couldn't find any explanation for this weird problem.
After much brainstorming between me, myself and I, I discovered that the only difference between the two apps is that the one that runs fine has this in its AndroidManifest.xml:
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="7" />
And the one that crashes has this in its AndroidManifest.xml:
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="13" />
So, you see, the Android UI is touted as being a 4th-generation UI framework, in which the UI is declarative and independently themed, but at the end of the day it's all the same st: Developing in C (for example) for Microsoft Windows is no more time consuming than developing in Java for the Android because the Android development framework is full of landmines like this in which the compiler won't tell you anything at compile time, and the thrown exception won't tell you either. Instead, you have to rely on **luck finding that little snippet of documentation (that may or may not exist), providing you with a hint as to where to look for the root cause of the problem.
I hope that this information will be helpful to many who experience the same problem.
You should use
<item name="android:windowNoTitle">false</item>
in your custom theme
I have the same problem and here is what it worked for me:
AndroidManifest.xml
< application
...
...
android:theme="#style/CustomTheme" >
Styles.xml
< style name="CustomTheme" parent="android:Theme">
< /style>
MainActivity.java
1) super.onCreate(savedInstanceState);
2) requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
3) setContentView(R.layout.activity_main);
4) getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title1);
The order of the codes is important as shown above.
If you have:
1) requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
2) setContentView(R.layout.activity_main);
3) super.onCreate(savedInstanceState);
4) getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title1);
You will get this exception:
android.view.InflateException: Binary XML file line #37: Error inflating class fragment
It matters which parent theme you are using:
If I use parent="android:Theme", then android:windowNoTitle="false" works (as per #Vladimir's answer).
However, if I use parent="android:Theme.Holo.Light", then I need to use android:windowActionBar="false" (as per #Jasper's comment). Here's my working xml using the Holo Light theme:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.ScheduleTimes" parent="android:Theme.Holo.Light">
<item name="android:windowActionBar">false</item>
</style>
</resources>
Maybe this question up to this time is already solved, but i post this answer for those who are having this error...i had a very similar problem, and changing the manifest file didn't work at all, but what it worked for me was go to the gradle Scripts and go to the build.gradle(Module:app) in this file, i changed the minSdkVersion and the targetSdkVersion with the same in both of them in this case for example 7, and the app runs correctly!
I had the same problem. I downloaded an app named BlueToothChat.
So, I add the following code to the string.mxl file within the value folder:
<resources>
<style name="CustomTheme" parent="#android:Theme">
<item name="android:windowNoTitle">false</item>
</style>
<string name="app_name">Bluetooth Chat</string>
...
and then add: {Theme = "#style/CustomTheme"} to the home page of the activity page (BluetoothChat.cs):
namespace BluetoothChat
{
/// <summary>
/// This is the main Activity that displays the current chat session.
/// </summary>
[Activity (Label = "#string/app_name", MainLauncher = true,
Theme = "#style/CustomTheme", ConfigurationChanges
Android.Content.PM.ConfigChanges.KeyboardHidden |
Android.Content.PM.ConfigChanges.Orientation)]
public class BluetoothChat : Activity
However, I did not test variations of this procedure or define any real CustomTheme attributes.

Theme change doesn't work on <4.0 as it should

I have some difficulties with setting up a "theme switcher" programmatically.
I would like to switch themes from app (between White (Theme.Light.NoTitleBar) and Dark (Theme.Black.NoTitleBar)) and what I do is:
I set a SharedPreference:
final String PREFS_NAME = "MyPrefsFile";
final SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
final SharedPreferences.Editor editor = settings.edit();
and than I have a two buttons to switch themes (second one is almost identical)
Button ThemeWhite = (Button) findViewById(R.id.ThemeWhite);
ThemeWhite.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
editor.putBoolean("Theme", false);
editor.commit();
System.exit(2);
}
});
and in begging of each activity I check SharedPreference
boolean theme = settings.getBoolean("Theme", false);
if(theme){
this.setTheme(R.style.Theme_NoBarBlack);
} else{
this.setTheme(R.style.Theme_NoBar);
}
setContentView(R.layout.aplikacja);
I define themes in file styles.xml in folder values:
<resources>
<style name="Theme.NoBar" parent="#android:style/Theme.Light.NoTitleBar" />
<style name="Theme.NoBarBlack" parent="#android:style/Theme.NoTitleBar" />
in values-v11:
<resources>
<style name="Theme.NoBar" parent="#android:style/Theme.Holo.Light.NoActionBar" />
<style name="Theme.NoBarBlack" parent="#android:style/Theme.Holo.NoActionBar" />
in values-v14:
<resources>
<style name="Theme.NoBar" parent="#android:style/Theme.DeviceDefault.Light.NoActionBar" />
<style name="Theme.NoBarBlack" parent="#android:style/Theme.DeviceDefault.NoActionBar" />
manifest file:
<application
...
android:theme="#style/Theme.NoBar" >
Everything is working excellent on android >4.0 but when I use 2.2 it doesn't change theme - just font is getting white as it should be but there is no dark background.
I tried checking if it at least works and changed Theme.NoBarBlack in values (for android <3.0) and its value the same as Theme.NoBar and then when I pressed button font wasn't changed -as it should do.
EDIT (PROBLEM FOUND):
So after trying it turn out that on Android 2.2 Manifest file set Background and rest, and programmatically changing theme affect only text color. any idea why that happens?
ANSWER (as I don't have 10 reputation):
On android <3.0 it matters if
super.onCreate(savedInstanceState);
Is before setting up theme or not.
So it should be:
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_NoBar);
super.onCreate(savedInstanceState);
setContentView(R.layout.lista);
}
Sorry guys for making problem out of nothing but as it was working on >3.0 I was confused. Spend half of day on it but working. :)
There's a mistake in your styles.xml:
for Theme.NoBarBlack, you set the parent to #android:style/Theme.NoActionBar, but it doesn't exist.
I think you mean #android:style:Theme.NoTitleBar.

Resources$NotFoundException in Graphical Layout ADT preview (but app actually Works)

My problem is that loading an array of strings defined in XML works in the app but will result in an error in the ADT Graphical Layout preview.
Now I can't see any graphics in the Graphical Layout because of this error, and it's difficult to work with other graphics.
But the view is loading and displaying the strings fine if I build and run my app.
So I suppose my code is correct but either:
I am missing some limitations of the Graphical Layout preview and some workaround
or perhaps I'm missing something obvious and doing things wrong even if it seems to work in the app
I have a custom view where I get an array defined by me in an array.xml file.
public class ScoreTable extends View {
[...]
#Override
protected void onDraw(Canvas canvas) {
[...]
int score_vals[] = getResources().getIntArray(R.array.score_vals);
[...]
}
[...]
}
My array is defined in res/values/array.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="score_vals">
<item >10</item>
<item >20</item>
<item >50</item>
</array>
</resources>
Graphical Layout is blank and says:
Int array resource ID #0x7f050000
Exception details are logged in Window > Show View > Error Log
But of course I have "public static final int score_vals=0x7f050000;" in R.java!
The details of this error are in a 50-deep stack, but resumes to this:
android.content.res.Resources$NotFoundException: Int array resource ID #0x7f050000
at android.content.res.Resources.getIntArray(Resources.java:405)
at com.threecats.poker.ScoreTable.onDraw(ScoreTable.java:53)
at android.view.View.draw(View.java:6740)
[...]
So, should getResources().getXXXArray() work in the context of a ADT Graphical Layout preview?
I would like to mention that I tried with both "array" and "array-integer" in the XML, and both work in the app but not in the preview.
Also I tried to save the Context from the constructor of the view in a private Context member... didn't help either.
Your code is alright but unfortunately there are still some bugs in ADT plugin and there is one of them. Layout Editor has troubles with rendering custom views. I had the same issue and the only workout I have found is checking View.isInEditMode and initializing int array in some other way but not from resources. So your code will look like this:
int score_vals[];
if (isInEditMode()) {
score_vals = { 10, 20, 50 };
} else {
score_vals = getResources().getIntArray(R.array.score_vals);
}
And by the way don't create or load any resources in your onDraw methods. I suppose getResources().getIntArray uses some sort of caching but anyway your perfomance may suffer.
I found a kind of a workaround whereby you have to hijack android's own attributes to get access to resources in the designer.
The following should provide the idea, but you would have to find a native android property of type int[]
This custom view XML should render in the graphical layout preview while using resources
<!-- Could override individual attributes here too rather than using a style -->
<com.github.espiandev.showcaseview.ShowcaseView
style="#style/ShowcaseView"/>
styles.xml - Style specifying some of the resources to use
<style name="ShowcaseView" parent="match_fill">
<!--# Cling drawable -->
<item name="android:src">#drawable/cling</item>
<!--# Title #-->
<item name="android:contentDescription">#string/showcase_title</item>
<!--# Description #-->
<item name="android:description">#string/showcase_description</item>
<!--# Button Text #-->
<item name="android:text">#string/ok</item>
<item name="sv_titleTextColor">#33B5E5</item>
<item name="sv_detailTextColor">#FFFFFF</item>
<item name="sv_backgroundColor">#3333B5E5</item>
<item name="sv_buttonBackgroundColor">#3333B5E5</item>
<item name="sv_buttonForegroundColor">#33B5E5</item>
</style>
attrs.xml - Custom attribute definition compatible with design-time preview
<!-- The android attrs assume the corresponding android format / data type -->
<declare-styleable name="ShowcaseView">
<!--# Cling drawable -->
<attr name="android:src"/>
<!--# Title #-->
<attr name="android:contentDescription"/>
<!--# Description #-->
<attr name="android:description"/>
<!--# Button Text #-->
<attr name="android:text"/>
<attr name="sv_backgroundColor" format="color|reference" />
<attr name="sv_detailTextColor" format="color|reference" />
<attr name="sv_titleTextColor" format="color|reference" />
<attr name="sv_buttonBackgroundColor" format="color|reference" />
<attr name="sv_buttonForegroundColor" format="color|reference" />
</declare-styleable>
ShowcaseView.java - Using the custom attributes in the custom view
public ShowcaseView(Context context) {
this(context, null, R.styleable.CustomTheme_showcaseViewStyle);
}
public ShowcaseView(Context context, AttributeSet attrs) {
this(context, attrs, R.styleable.CustomTheme_showcaseViewStyle);
}
public ShowcaseView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// Get the attributes for the ShowcaseView
final TypedArray styled = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ShowcaseView, 0, 0);
showcase = styled.getDrawable(R.styleable.ShowcaseView_android_src);
titleText = styled.getString(R.styleable.ShowcaseView_android_contentDescription);
subText = styled.getString(R.styleable.ShowcaseView_android_description);
buttonText = styled.getString(R.styleable.ShowcaseView_android_text);
backColor = styled.getInt(R.styleable.ShowcaseView_sv_backgroundColor, Color.argb(128, 80, 80, 80));
detailTextColor = styled.getColor(R.styleable.ShowcaseView_sv_detailTextColor, Color.WHITE);
titleTextColor = styled.getColor(R.styleable.ShowcaseView_sv_titleTextColor, Color.parseColor("#49C0EC"));
styled.recycle();
// Now make use of the fields / do further initialization ..
}

Access resource defined in theme and attrs.xml android

I have a scenario in which I want to set a Drawable depending upon the theme defined.
To explain this further, Here is what I have in code:
\res\values\attrs.xml
<resources>
<declare-styleable name="AppTheme">
<attr name="homeIcon" format="reference" />
</declare-styleable>
</resources>
res\values\styles.xml
<resources>
<style name="AppTheme" parent="android:style/Theme">
<item name="attr/homeIcon">#drawable/ic_home</item>
</style>
</resources>
AndroidManifest.xml
<application android:label="#string/app_name"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
So as you have noticed I am defining a custom attr homeIcon and setting the attribute value in AppTheme.
When I define this attribute in a layout XML and try to access it it works smoothly
<ImageView android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="?attr/homeIcon" />
and renders the Drawable ic_home in an ImageView.
But I am not able to figure out how to access the Drawable programmatically.
I tried to do this with a work around, by defining a holder LayerList Drawable, which results in resource not found exception:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="?attr/homeIcon" />
</layer-list>
Summary
I want to access the Drawable defined in a custom defined Theme programmatically.
I think you can get the Drawable with this code:
TypedArray a = getTheme().obtainStyledAttributes(R.style.AppTheme, new int[] {R.attr.homeIcon});
int attributeResourceId = a.getResourceId(0, 0);
Drawable drawable = getResources().getDrawable(attributeResourceId);
a.recycle();
Another possible way to do it:
public static int getResIdFromAttribute(final Activity activity,final int attr) {
if(attr==0)
return 0;
final TypedValue typedvalueattr=new TypedValue();
activity.getTheme().resolveAttribute(attr,typedvalueattr,true);
return typedvalueattr.resourceId;
}
Or in Kotlin:
#JvmStatic
fun getResIdFromAttribute(activity: Activity, attr: Int): Int {
if (attr == 0)
return 0
val typedValue = TypedValue()
activity.theme.resolveAttribute(attr, typedValue, true)
return typedValue.resourceId
}
no need to recycle anything here...
usage:
int drawableResId=getResIdFromAttribute(this,R.attr.homeIcon);
Drawable drawable = getResources().getDrawable(drawableResId);
I used below method to get resource id form style attribute. Then it can be used for drawable, string, dimension so on.
TypedArray typedArray = context.getTheme().obtainStyledAttributes(new int[] { R.attr.attrName });
int resourceId = typedArray.getResourceId(0, defaultResourceId);
typedArray.recycle();
cheers :-)
If you are using support / design library easier way to get drawables now is -
Context.getDrawable(int)
or
ContextCompat.getDrawable(Context, int)
reference - https://plus.google.com/+BenjaminWeiss/posts/M1dYFaobyBM
Here are the results of my investigation, regarding this topic.
If we have declare-stylable then we can override those values in themes.
So far the best way that I found how to get them is the following.
TypedArray a = context.getTheme().obtainStyledAttributes(R.styleable.AppTheme);
a.getDrawable(R.styleable.AppTheme_homeIcon);
By using R.styleable.AppTheme_homeIcon we are referencing exactly that attribute that we want. For example if we would have few more attributes, then we can reference them as follows:
a.getColor(R.styleable.AppTheme_color,defaultValue);
a.getDimension(R.styleable.AppTheme_width,defaultWidth);
And if in current theme those attributes were not defined you will get default values and no Exceptions.

Categories

Resources