I want to let users customize various colors of my app. My app palette consists of three colors, I have implemented a color picker so users can choose each of these color values.
My problem is that I don't know how to define which views use the colors. e.g: give a view customColor1 as background and customColor2 as text color on it's layout, nor how to apply the custom color to it.
My initial idea was to a color resource for each color on the palette so I could set the default values and also apply them to views e.g: android:background="#color/customColor1":
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="customColor1">#FFFFFF</color>
<color name="customColor2">#7A7A7A</color>
<color name="customColor3">#0044AA</color>
</resources>
Would it be possible to replace occurrences of #color/customColor1 with it's custom value? if not, how can this be done?
You cannot change that.
Instead you can store the values of the colors to be used in SharedPreferences and every time your app launches you can access these colors and use it any way you like it.
You cannot change the value of a resource at runtime.
I recommend storing the color in a SharedPreference or with some other form of local storage so that you can persist the value and access it again wherever you need it.
Simply you can not. Resources are bundled at compile time in the apk
Related
I am having a problem, i know how to set colors programmatically using the following method
For Example:
imageButton.setColorFilter(ContextCompat.getColor(this,R.color.colorAccent));
This works just fine what about if i don't want to set the colors from colors.xml for example
the following colors in XML
android:background="#android:color/holo_orange_dark"
I want to set the colors above programmatically i don't want to use the colors in colors.xml
Use this
getResources().getColor(android.R.color.holo_orange_dark)
Here's the full code:
imageButton.setColorFilter(getResources().getColor(android.R.color.holo_orange_dark));
Hope this helps. Feel free to ask for clarifications...
Hy,
I am using the next background colour #android:color/transparent for a ImageButton in the layout.xml. Could or should I externalize it in the colors.xml resources file?
Thanks
You could but it depends on your needs.
If you use that color in many different parts of your application it's best to place it in an external xml file. Sometimes you need to change the style of the whole application and it's easier to change the definition of that color instead of changing the color in many different places. If you're only using it in one single place then it is OK to leave it out of the external file.
EDIT:
<color name="my_background_color">#android:color/transparent</color>
I've a normal TextView:
<TextView
android:id="#+id/item"
android:layout_width="wrap_content"
android:layout_heigh="wrap_content"
android:textColor="#color/text_on_background"/>
It references #color/text_on_background. I've a few build flavors of my app, each with different color scheme. So I need to change the color of the text sometimes. But sometimes not, and I want to use default inverse text color from theme.
So i tried these:
<color name="text_on_background">?android:textColorPrimaryInverse</color>
<color name="text_on_background">?android:attr/textColorPrimaryInverse</color>
And it always ended with error about inflating the TextView. And I couldn't find any #color/…inverse… or #android:color/…inverse….
Any clue how to do that?
If you want to inverse the Color means , you can do (MAX - COLOR_VAL) in all (R,G,B)
for example
I Assume F3FAB6 - this is your color code means you can do following way
Color.rgb(255-243, 255-250,255-182);
You can get Inverse Color.
I am using library for date and time pikers(https://github.com/wdullaer/MaterialDateTimePicker).
And there is a saying:
"Alternatively, you can theme the pickers by overwriting the color resources mdtp_accent_color and mdtp_accent_color_dark in your project."
So I override this two colors with my own, but when I run app it still the libraries colors, but not mine.
Maybe some one have the same problem or maybe you have some idea why it happens?
EDIT:
Here is how I override colors:
<color name="mdtp_accent_color">#08395b</color>
<color name="mdtp_accent_color_dark">#062d48</color>
Here's the order in which the library looks for colors:
Color set in java
If on 5.0+: color set in android.R.attr.colorAccent
Color set in R.attr.colorAccent
Color set in R.color.mdtp_accent_color
If R.attr.colorAccent is defined in your application (for example because you are using a recent version of the AppCompat support library), you cannot use R.color.mdtp_accent_color to overwrite it.
If you want to use a different color, I recommend you to set in your java code when instantiating the dialog:
tpd.setAccentColor(myColor);
I have color code #ffa100. I have also define it in values xml as
<color name="orange">#ffa100</color>
I want to find name "orange" based on the color code value "#ffa100" How can I find it? I want to use name of color in program from value where several colors are defined and I get has code from server.
You can set it directly from with the hex code, no need to have it in resources. Hex value has to be a String.
your_image.setBackgroundColor(Color.parseColor("#ffa100"))
Use,
Color.parseColor("#ffa100");
like,
mTextView.setTextColor(Color.parseColor("#ffa100")); //set text color
mTextView.setBackgroundColor(Color.parseColor("#ffa100")); // set background color