colors.xml resource does not work - java

I created a colors.xml file in my Android app under /res/values/colors.xml. The contents are...
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Green">#00ff00</color>
</resources>
I try to update the background of my a TableRow using...
TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundColor(R.color.Green);
This does not set it as green, it is gray instead. No matter what values I add to the colors.xml file, it is always the same gray color. However this does work...
TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundColor(android.graphics.Color.GREEN);
Is something wrong with my colors.xml?

You should use this instead:
TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundColor(getResources().getColor(R.color.Green));
Its unfortunate that resource ID and color have same type: int. You should get color value from resources via getColor() and use that valu as color. While you are using resource ID as color.

Try instead using the command setBackgroundResource, ie
TableRow test = (TableRow)findViewById(R.id.tableRow2);
test.setBackgroundResource(R.color.Green);

Related

How to change a value color at runtime?

I am developing an app using java, android studio.
I created a "myColor" style (value-> color) (eg blue) and set it in various places of my app (several activits, button, labels)...how to represent the visual identity of a particular brand, a color predominant.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="myColor">#e8660c</color>
</resources>
My intention is to allow the user to select a color and save that color (base64, hex ...) in a txt on external storage ... and when the user opens the application again, all parts (buttons, activities) will be of the selected color.
Remembering that I know how to set the color of components in the ".setBackground" type of execution ... that's not what I want ... I want to load a color style.
Could you change a value of R.color?
You can save the colors in SharedPreferences
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putInt("Color", R.color.mycolor);
editor.apply();
And retreive like this
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
int color = prefs.getInt("Color", R.color.default_color);
Hope this will work.
Save Colour code in Internal storage or SharedPreferences and Use them by
Color.parseColor("#112233");

Two xml with colors

I have two xml in folder values with colors:
colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow">#23238E</color>
</resources>
second_colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow">#DBDB7O</color>
</resources>
and I would like get this in TextView:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/yellow"
android:text="test"
/>
this working ok, but if i do:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#my_color/yellow"
android:text="test2"
/>
then I have error: No resource found that matches the given name (at 'textColor' with value "#android:my_colors/test")
i know - i can make:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow">#DBDB7O</color>
<color name="yellow_second">#23238E</color>
</resources>
but I would like to have it in separate files.
Is this possible?
No you dont need to create a second xml file, that android only specify/use one xml for using color on different values folder. Creating a new xml asides from color.xml in the values will not register it as a color
You can go and read it from here
And states:
They contain things such as application source code and resource files.
Some are generated for you by default, while others should be created if required
The list of xml specified in that websites are the only xml that you can create if it is required by you.
You can't define two resources of the same type with the same name with the same qualifiers.
Setting a color in XML using #color/yellow is really just saying "find a color resource called 'yellow'". #color doesn't define a file name- it is declaring the resource type. Similarly, trying to use #second_color/yellow won't tell Android to look for a color named yellow in a file called second_color.xml.
When the build tools generate R, they will combine all of the <color> entries for a given configuration together. If you have a color named "yellow" defined in two separate files that still both reside in your values/ directory, you will have a problem because there can only be one color resource named "yellow."
You can define two yellow colors if they are in values/ folders with different qualifiers. For example, you could put one yellow in values-fr/ and a different one in values-en, and the first will be used for French speaking countries while the latter will be used for English speaking countries. If you do this, you still just reference the color with #color/yellow, but the system takes care of selecting the correct one for you.
If you just want two different shades of yellow for the same application in the same configuration, you should give them different names.
What do you want ? I don't understand :/
You can set color in Java :
TextView t = findViewById(R.id.yourText);
t.setTextColor(getResources().getColor(R.color.colors));
EDIT :
You have to name your colors differently. Although your colors are in differents files, they are in your unique R file so if name is the same there will be an error.
Try this :
colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow1">#23238E</color>
</resources>
second_colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="yellow2">#DBDB7O</color>
</resources>
and in xml layout :
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/yellow1"
android:text="test2"/>
or
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/yellow2"
android:text="test2"/>

Custom color not available

I am trying to add a custom Color to my project, but for some reason it is not appearing in R.color. I have added a colors.xml file as follows
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="bronze">#ffc125</color>
</resources>
and now when I try to use the new colour as follows
paint.setColor(getResources().getColor(R.color.bronze));
I get a compile error as bronze does not appear in the list. I've tried moving the definition to styles.xml but it still doesn't appear. I've also tried cleaning the project to force R to rebuild but it didn't make any difference either. Can anyone see what I'm doing wrong here?
Thanks
Remove the import of android.R and then build again.
getColor() returns int so set to the view you need to use the color
resource xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="bronze">#8C7853</color>
</resources>
setColor to the view,here i used in textview
textView.setTextColor(getResources().getColor(R.color.errorColor));

Android how can i get the image button's id programmatically?

I created image button and I set that's image programmatically with this code:
myButton1[0][0].setImageResource(R.drawable.mayin2);
And I want to get that image's id or name from another method. How can i call image button's image's id or name?
You can't do that directly, it's just a reference to a drawable resource that gets retrieved, but the integer value isn't retained. One thing you could do is store the reference as a tag on the View:
myButton1[0][0].setImageResource(R.drawable.mayin2);
myButton1[0][0].setTag(R.drawable.mayin2);
Then you could retrieve it with:
Integer resourceID = (Integer) myButton1[0][0].getTag("resource_id");
EDIT: Alternately, if you need to store multiple tags, define an ID in your values folder (create some XML file, maybe ids.xml) like so:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="resource" type="id" />
</resources>
Then set a tag with that ID:
myButton1[0][0].setImageResource(R.drawable.mayin2);
myButton1[0][0].setTag(R.id.resource, R.drawable.mayin2);
Then retrieve by that same ID:
Integer resourceID = (Integer) myButton1[0][0].getTag(R.id.resource);

Android. Change the background color of a FrameLayout from code

I'm trying to change the background color of a FramyLayout.
The color is changing, but to the wrong one.
However it is working fine if I do it through the XML.
Here is my res/values/colors.xml code
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="grey">#888888</color>
<color name="white">#FFFFFF</color>
<color name="red">#ffff3448</color>
<color name="green">#ff408c3a</color>
</resources>
Here is how it looks like if I make changes in the XML
And that's what is happening if I try to do it with the code
FrameLayout frameLayoutBalance = (FrameLayout)view.findViewById(R.id.frameLayoutBalance);
frameLayoutBalance.setBackgroundColor(R.color.green);
You should not use
frameLayoutBalance.setBackgroundColor(R.color.green);
setBackgroundColor required a Color (i.e. its value as describe by Chirag Raval) not a color resources.
use this frameLayoutBalance.setBackgroundColor(getResources().getColor(R.color.green));
Use this code
frameLayoutBalance.setBackgroundColor(Color.parseColor("#00aacc"));

Categories