Two xml with colors - java

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"/>

Related

Vector drawables not loading in xml ResourceNotFoundException

I am having trouble with loading vector files in my splash background xml file. The vector has been loaded successfully because I can open the vector file in android studio and I get a small graphic of the vector on the side of the xml file. However, the design view of the xml file shows an error for the vector file, and I get a ResourceNotFoundException when trying to run my app.
Code below:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:opacity="opaque">
<item android:drawable="#color/colorPrimary"/>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/ic_loading_screen_logo"/>
</item>
</layer-list>
splash background.xml
My grade version is classpath 'com.android.tools.build:gradle:4.0.1'
and I have included the following line in my grade app file vectorDrawables.useSupportLibrary = true
It seems that any vector file that I use has this error, so there is some setting of some sort that is missing.
Please assist in anyway.
Thanks,
The problem is wrapping the xml drawable in a <bitmap/>
Try this:
Replace
<item>
<bitmap
android:gravity="center"
android:src="#drawable/ic_loading_screen_logo"/>
</item>
With:
<item android:gravity="center"
android:drawable="#drawable/ic_loading_screen_logo"/>
replace <bitmap to <item
make sure that your vector is in drawable folder not in drawable-24.
check vectory path may be its too long.

Background property on custom compound control

I have a compound control that essentially combines a Button with a ProgressBar. It includes a background property that I have declared in attrs.xml as:
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<declare-styleable name="MyControl">
<attr name="background" format="reference" />
</declare-styleable>
</resources>
In the constructor of my control, I then pull the background out of the TypedArray and apply it.
All this worked fine until recently, when I had to add in a dependency on the v4 support library. Now I get this build error in my attrs.xml file:
Error APT0000: Attribute "background" has already been defined (APT0000)
Why is this? And what can I do as an alternative so that consumers of my compound control can set the background?
It's a conflict with an internal library that also defines the 'background' attribute.
For more details visit : https://groups.google.com/forum/#!topic/actionbarsherlock/_N0hn47zx6w
The attribute "background" has already been defined in the support library. So you dont have to define that again. you just add the attribute without defining it, like this,
If you want to use app:background in the custom view, then
<attr name="background"/> is enough in the attrs file.
If you want to use android:background in the custom view, then use this line in attrs.
<attr name="android:background"/>

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

color-palette or third party plugin in order to set colors in eclipse

Is there easy way to manipulate colors in android ?
In dot.net we have color palette to change the color of
objects(button-background,text-color.. etc)
similarly is there any color palette/or/-any-plugins in object
browser of eclipse IDE at design time
Note:: I am aware of changing the color by adding color codes in styles.xml and referencing it in layout.xml
You can do this in some cases directly,
e.g in TextView you can change the Text Color by applying this statement in
Java code
textView1.setTextColor(Color.Red);
for other objects like buttons etc. you can not change their Color Directly like C#.
You have to create different .xml file and then you will store this .xml file inb drawable folder, in your activity_main(where button is created in your layout file) you have to make reference
e.g
<Button
android:background="#drawable/red_button" />
now the colored button .xml file located in
res/drawable/red_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/red_button_pressed" />
<item android:state_focused="true" android:drawable="#drawable/red_button_focus" />
<item android:drawable="#drawable/red_button_rest" />
</selector>
Check this Link for more Details

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