In my main Android application, I have defined an attribute and it points to some color. It looks something like this:
<declare-styleable name="ThemeColors">
<attr name="my_button_background" format="color" />
</declare-styleable>
And then I have my syle
<style name="MyTheme">
<item name="my_button_background">#color/color_blue</item>
</style>
After that, i setup my button like this:
<Button
style="#style/some_style"
android:text="#string/some_text"
android:background="?attr/my_button_background"/>
Now, in my overlay app, if I do something like this, and it works perfectly
<color name="color_blue">#FFFF00</color>
But, if I want to override attribute using this, it does not work.
<color name="my_button_background">#FFFF00</color>
Alternate solution is to wrap color_blue with something like color_button_background, then use that as a pointer for my attribute, and override that in my RRO. But it just brings up a lot of clutter. And I have tons of attributes, which will make this job much more time consuming.
Can someone please help me with this one? Is there a way to overlay attributes? I tried Googling this one out, but I found no solution.
Related
I've got this super simple question I can't find an answer to.
I want to make a variable which I could then use for multiple elements.
Something like this in strings.xml:
<resources>
<string name="textSize">20sp</string>
</resources>
...
<EditText android:textSize="#string/textSize" />
But this does not work.
I was able to accomplish what I wanted the following way in themes.xml:
<style name="textSize">
<item name="android:textSize">20sp</item>
</style>
...
<EditText android:theme="#style/textSize" />
But it seems too complicated for just a single value, is there a better way?
As suggested in the comments, this looks like a dimen property rather than a string.
Just like strings.xml, you can have another file (usually dimen.xml) with dimensions. Your case could look like this:
<resources>
<dimen name="bigText">20sp</string>
</resources>
It also allows you to have different settings for different configurations (for example, screen sizes here: How to define dimens.xml for every different screen size in android?).
You can find the documentation here:
https://developer.android.com/guide/topics/resources/more-resources?hl=en#Dimension
I am trying to create Android library module (API 21+) for my application, which will implement some of the business logic shared across various applications.
The issue is that I want to declare attributes in this library module, for which I will be able to set values in app modules of aforementioned projects.
The funny thing is, that my current solution works on emulator but not on physical devices.
Here is how I have it now (simplified, irrelevant parts omitted):
Library's attrs.xml:
<resources>
<attr name="lib_Background" format="reference"/>
<attr name="lib_Logo" format="reference"/>
</resources>
Applications styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="lib_Background">#drawable/back</item>
<item name="lib_Logo">#drawable/logo</item>
</style>
It does not matter whether I try to access these resources from xml of library's layout like this:
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="?attr/lib_Background"
android:scaleType="centerCrop"/>
or programatically like this:
fun Context.getDrawableFromAttributes(idOfAttribute: Int): Drawable? {
val a = this.theme.obtainStyledAttributes(intArrayOf(idOfAttribute))
val resourceId = a.getResourceId(0, 0)
a.recycle()
return this.resources.getDrawable(resourceId, this.theme)
}
Still I have this image properly loaded on emulator, but "null" on physical device.
Is there some better or correct way how to achieve this?
I have kinda forgot about this question.
The issue was that I have used broken PNG for one particular density which caused it to be null. So it was not and error in implementation at all and this approach is absolutely valid.
I want to offer my user two themes: light and dark. I want the background color of a certain layout to change based on whichever theme has been chosen.
Essentially, I want to have two themes like this:
<style name="Theme.Light">
<item name="myBackgroundColor">#ffffff</item>
</style>
<style name="Theme.Dark">
<item name="myBackgroundColor">#000000</item>
</style>
And then from my Activity, I would reference the variable like this:
<RelativeLayout
...
android:background="?myBackgroundColor"/>
How do I do that?
Ok I've figured out an answer of sorts. If anyone has a better answer, please post it.
You need to declare the attribute "myBackgroundColor" as styleable. E.G:
<declare-styleable name="TestStyleable">
<attr name="myBackgroundColor" format="color|reference" />
</declare-styleable>
I have no idea what the name refers to, it seems you can set it to anything. Once you have declared this block, you can use "myBackgroundColor" as I have done above.
I have
android:button="?android:attr/listChoiceIndicatorSingle"
on the layout for a CheckBox. I pressed F3 to take me to the definition, and it just said
<!-- Drawable to use for single choice indicators. -->
<attr name="listChoiceIndicatorSingle" format="reference" />
in android-sdk/platforms/android-17/data/res/values/attrs.xml. I searched the whole file and that was the only occurrence.
How do I find the drawables that it references?
The value of attributes are defined in theme.xml. Check 'theme.xml' in SDK.
In my case, it defined like this:
<item name="listChoiceIndicatorSingle">#android:drawable/btn_radio_holo_dark</item>
I'm trying to set a ListView background color based on the current theme attribute, but it crash every time the ListView is shown.It seems I'm doing something wrong but I can't see what...
Here's what I'm doing:
First, create the background color:
<resources>
<color name="userlist_background_light">#fff0f0f0</color>
<color name="userlist_background_dark">#ff040404</color>
</resources>
Second, create attributes for my custom themes:
<resources>
<attr name="userlist_background" format="reference|color" />
</resources>
Third, setting this attribute in my themes:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Light" parent="Theme.Sherlock.Light">
<item name="userlist_background">#color/userlist_background_light</item>
</style>
<style name="Dark" parent="Theme.Sherlock">
<item name="userlist_background">#color/userlist_background_dark</item>
</style>
</resources>
And finally, using this attribute in the ListView xml:
<ListView
android:id="#+id/user_bar"
android:layout_width="0dip"
android:layout_height="0dip"
android:background="?attr/userlist_background"
android:cacheColorHint="?userlist_background"
android:visibility="gone" />
Even the Eclipse layout view crash. Of course, it works fine if I use a "#color/" directly in the background attribute. It even work if I use say, "?android:attr/colorBackground".
The message error is:
android.view.InflateException: Binary XML file line #8: Error
inflating class android.view.ListView Caused by:
android.content.res.Resources$NotFoundException: Resource is not a
Drawable (color or path): TypedValue{t=0x2/d=0x7f010068 a=-1}
I'm pretty sure I'm doing something wrong, as it works with android attributes, but I haven't be able to find what during my Google searches.
I hope you'll be able to help me!
Many thanks,
Sébastien.
Ok, I fixed it, and it was due to a mistake!
I have two themes.xml files, one for Honeycomb+, and one for Gingerbread-. I've only added the new attributes to the themes.xml targeting Gingerbread-, and was testing on ICS.
Maybe it'll help others who'll make the same mistake!