Setting Default Colors from android Programmatically - android

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

Related

How to set layout background tint from string programmatically?

I tried this code:
LinearLayout someLayout=(LinearLayout)view.findViewById(R.id.someLayout);
someLayout.setBackgroundTintList(context.getResources().getColorStateList(Color.parseColor("#ff8800")));
But I'm getting an error: android.content.res.Resources$NotFoundException
I'm getting the color hex from external source so I can't embed it in colors.xml.
Also I want to change the tint, not the background so setBackground is not an option.
I figured I can't use getColorStateList() so I searched for another way to do it.
At the end I was able to set color tint using the following code:
LinearLayout someLayout=(LinearLayout)view.findViewById(R.id.someLayout);
someLayout.getBackground().setColorFilter(Color.parseColor("#ff8800"), PorterDuff.Mode.SRC_ATOP);
This worked as if I changed the backgroundTint property in the xml file, so it's perfect for my problem.
I was able to manage using the following line. change it to your circumstances.
myView.getBackground().setTint(currentView.getResources().getColor(R.color.colorAccent));
For Kotlin ,
I modified #Krestek answer :
someLayout.background.setColorFilter(Color.parseColor("#ff8800"), PorterDuff.Mode.SRC_ATOP)
You can't do this like that because getColorStateList method expect int id of resource, and you are passing RGB color int. You should create color state list following this link
and then set it like this:
.getColorStateList(R.color.your_xml_name)

Android: externalize "#android:color/transparent" in the colors.xml resources file

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>

Android - Cannot change textcolor from xml

I'm stuck with a very strange issue.
I can't change the TextColor of some TextViews around the app, neither with the android:textColor="" nor by setting a style. It only works if I change it at runtime. This problem appeared without any change related to the activity in question. The most strange thing is that in the Preview the colors are fine, but when I run it, the colors are always the same.
What might be overriding the textColor value set in the XML ?
Thx Anticipately
P.S:
Along all the App I can only change the color of the textHint
I had to remove all the N preview stuff from my sdk to make things normal again. Don't forget the cache too.
sdk/extras/android/m2repository/com/android/support/design|support-v-13|ect./24~
The syntax of xml line you wrote is wrong
instead of
android:setTextColor=""
use
android:textColor=""
You can change the Text Color in layout XML by using android:textColor
example:
android:textColor="#0E0E9A"
it overrides the style.xml
the only way to override your layout.xml is by code
example:
mEditText.setTextColor(Color.DKGRAY);

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

How to view default android simple_spinner_dropdown_item style

Often i like default android style defined for some widget.
Often i like to just slightly modify this default style, not create my own style from a scratch.
Now, i use android.R.layout.simple_spinner_dropdown_item as dropDownViewResource on my spinner adapter - like this:
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
I can open this layout xml file and copy its content to modify it. But the most important part of that xml file is attribute style, it looks like this: style="?android:attr/spinnerDropDownItemStyle"
Now, my question is: how do i find/view/copy this style spinnerDropDownItemStyle, so i can for example just change background color.
Thanx for your help
here you can find style spinnerDropDownItemStyle:
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml
But, if you looking for change background of Spinner, you'll need to search for 9 patch images:
https://developer.android.com/studio/write/draw9patch.html
And here a good example:
http://www.gersic.com/blog.php?id=57
Ty
My suggestion to you is . you can make a seprate custom layout and put it on your spinner setDropDownResource()
adapter.setDropDownViewResource(android.R.layout.CUSTOM_LAYOUT);
Now you can easily make a style for your layout

Categories

Resources