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));
Related
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"/>
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"/>
I am implementing searchview in my app, for which I copied this options_menu.xml from a separate working project of searchview.
I get this error in res/menu/options_menu.xml
Multiple annotations found at this line:
- error: No resource identifier found for attribute 'actionViewClass' in package
'com.example.indianconstitution'
- error: No resource identifier found for attribute 'showAsAction' in package
'com.example.indianconstitution'
Code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search" // error in this line
android:icon="#drawable/ic_launcher"
android:title="#string/action_search"
yourapp:actionViewClass="android.support.v7.widget.SearchView"
yourapp:showAsAction="always" />
</menu>
Can anyone figure out this problem?
Thanks in advance...
Looks like you copied this from a project that uses appcompat-v7 into one that doesn't.
Either include appcompat-v7, following the instructions, or just replace yourapp:x attributes with android:x attributes. If you go this way, though, you should also replace the action view class with the corresponding one in the framework (i.e. android.widget.SearchView).
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"));
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);