Can't override android colors - android

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);

Related

Where can I find defined colors in android

I've created the project from default template Bottom Navigation Activity. I found that the background color for BottomNavigationView is #2D2D2D for dark theme. I defined it in colors.xml, but I'm not sure that it's good solution. Is there any pre-defined colors in system(smth like #android:color/theColorThatINeed)? Where can I see all of them?
the best practice is to use colors.xml, and put there all of your colors,
and then call it like this:
android:background="#color/red"
there are also system constants for colors, which contains this constants:
http://developer.android.com/reference/android/R.color.html
this resources are read only and you can not add to them more constants.
Unfortunately there isn't a single place to find all the 'system colours'. You can either find them through documentation, which is usually easy with the material components (which is where BottomNavigationView is from), or by trawling through the source code. For your particular example the colour you're after is ?attr/colorSurface, which is in the documentation and the source code.
Is there any pre-defined colors in system?
Yes, there is but it's limited. These colors are defined in android.R.color. Here are some of them.
background_dark
background_light
black
darker_gray
holo_blue_bright
holo_blue_dark
holo_blue_light
holo_green_dark
holo_green_light
holo_orange_dark
holo_orange_light
holo_purple
holo_red_dark
holo_red_light
transparent
white
widget_edittext_dark
To use them:
android:background="#android:color/background_dark"/>

Setting Default Colors from android Programmatically

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...

Get Android system accent color (Android 10 System color accent)

This question should be simple, but i didn't find an answer. I have an app with selectable accent, and i'm trying to add an option to use the android system accent (apps like Lawnchair have such an option). In the style for the system accent, i tried to get this accent in every way possible:
?android:colorAccent
?android:attr/colorAccent
?attr/colorAccent
And this is the style:
<style name="AppTheme.systemAccent" parent="AppTheme">
<item name="colorAccent">???</item>
</style>
Nothing seems to work and the app crashes, but i'm certain that this is possible. The accent selection, when i use normal colors, works fine. Where am i wrong?
EDIT: Just to be clear, i'm trying to get the system accent color, i.e. the system wide color used in settings, notification panel and so on. This color is now selectable in Android 10 and was selectable before in rom like the Oxygen OS. Let's say i select a red accent in settings->customization on my Android 10 device. I want to get this red accent in my app
You can get it programmatically:
TypedValue typedValue = new TypedValue();
ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(this,
android.R.style.Theme_DeviceDefault);
contextThemeWrapper.getTheme().resolveAttribute(android.R.attr.colorAccent,
typedValue, true);
int color = typedValue.data;
Just to be clear I am referring to the Android Q System color accent:
Okay. I did some intensive research and trial and error. After that I found out, that I could access a private property #*android:color/accent_device_default_light. BUT this is just possible if you change your parent class for the activity from AppCompatActivity to Activity because the AppCompat can't set up the toolbar with this private property. Further it's not recommended to use private properties because they are likely to get deleted or changed in the future.
#color/colorAccent
use this ,it's default color in android
or you can customize your color in the color.xml

Changing BackgroundTint of a MaterialButton

I'm using the lasted support design : 28, alpha3.
I use using the "Theme.MaterialComponents.Light.NoActionBar" as the theme for my application and "MaterialButton" instead of a normal "Button" in my layouts.
I can set the BackgroundTind from the XML as normal but i can't change it via java.
I tried:
deliverSwitch.setBackgroundTintList(getResources().getColorStateList(R.color.colorYellow));
deliverSwitch.setSupportBackgroundTintList(getResources().getColorStateList(R.color.colorYellow));
but none of them worked... I also tried to clear the current tint by leaving the setBackgroundTintList null and it doesn't work either.
I couldn't get it working either. As a workaround I did the following: First you get the current background Drawable, then you tint it with the desired color and set the new background with setBackgroundDrawable for your Material Button.
Drawable background = materialButton.getBackground();
background.setTint(ContextCompat.getColor(getContext(), R.color.bg_button_primary));
materialButton.setBackgroundDrawable(background);
I hope that helps.

Allow users to customize app colors?

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

Categories

Resources