I have a color define in colors.xml
<color name="gray">#9e9e9e</color>
I want to set the background color in my MainActivity I stablish a conditional that set the background depens the version of Android that the device has.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
cobro.setBackgroundColor(getResources().getColor(R.color.gray, getApplicationContext().getTheme()));
cobro.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.gray));
}
else
{
cobro.setBackgroundColor(getResources().getColor(R.color.gray));
}
Actually I am testing in Android Lollipop, so the color has to be setting with the if statement, but neither of two forms setting me the background color to my button work, any idea? could anybody tell me what is the correct form to set the background color?
On your Button XML, add an attribute like this
android:background="#color/yourColor"
But before doing that, you need to add yourColor inside your colors.xml file. The default value of colors.xml is below
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
Then just add yourColor there
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="yourColor">#yourColorCode</color>
</resources>
Use ContextCompat rather than deprecated getColor Method
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.L){
cobro.setBackground(R.drawable.btn_selector);
}else{
cobro.setBackground(R.drawable.btn_selector);
}
btn_selector.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/login_selected" /><!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/login_mouse_over" /> <!-- focused -->
<item android:drawable="#drawable/login" /><!-- default -->
Related
I can't get my colors in my app to line up, even though they supposedly are the same hex code. What I did is I went into Sketch (where I exported and made the buttons) and used the program to get hex code, but as you can see from the picture (below that is not the same color.
https://imgur.com/a/pfcLgOm
Here is my code from my colors file and styles file:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name= "color1"> #5AD7DB</color>
<color name= "color2"> #D53D96</color>
<color name= "color3"> #F27E33</color>
</resources>
<resources>
Here is my styles file
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/color1</item>
<item name="colorPrimaryDark">#color/color1</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
<style name="MyCustomHelpshiftTheme"
parent="Helpshift.Theme.Light.DarkActionBar">
<item name="colorAccent">#color/color2</item>
<item name="colorPrimary">#color/color1</item>
<item name="colorPrimaryDark">#color/color1</item>
</style>
<style name="Helpshift.Theme.Base" parent="MyCustomHelpshiftTheme"/>
</resources>
Try the below code in your colors.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="color1">#5AD7DB</color>
<color name="color2">#D53D96</color>
<color name="color3">#F27E33</color>
</resources>
Spaces removed where unnecessary..
Trying to set a color which is defined in res/values/colors.xml to an object,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="listViewSelected">#android:color/holo_blue_light</drawable>
<drawable name="listViewPressed">#android:color/holo_green_light</drawable>
<drawable name="pagerTabStrip">#2B3333</drawable>
<!--<drawable name="pagerTabStrip">#353F3E</drawable>-->
<drawable name="tableHead">#FF444444</drawable>
</resources>
I can not figure out why it is not working,
I tried a lot of approaches (getResources(), Color.parseColor(), ...)
How do I set the color "tableHead" e.g. to a TextView?
tv.setBackgroundColor(????);
Color entries should be like this
<color name="tableHead">#FF444444</color>
and use tv.setBackgroundResource(R.color.tableHead);
Use,..
Color.parseColor("#bdbdbd");
like,
mTextView.setTextColor(Color.parseColor("#bdbdbd"));
OR......................
Get a handle to the root layout used, then set the background color on that. The root layout is whatever you called setContentView with.
// Now get a handle to any View contained
// within the main layout you are using
View someView = findViewById(R.id.randomViewInMainLayout);
// Find the root view
View root = someView.getRootView()
// Set the color
root.setBackgroundColor(getResources().getColor(android.R.color.red));
tv.setTextColor(getResources().getColor(R.color.tableHead));
And guess what your colors.xml should be like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="tableHead">#FF444444</color>
</resources>
Your color.xml should look like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="tableHead">#FF444444</color>
</resources>
How you will use this color to set in textview: Like this
tv.setBackgroundColor(getResources().getColor(R.color.tableHead));
Try something like this:
tv.setBackgroundResource(Color.parseColor("#ffffff"));
Firstly modify your color.xml as below
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="listViewSelected">#android:color/holo_blue_light</drawable>
<color name="listViewPressed">#android:color/holo_green_light</drawable>
<color name="pagerTabStrip">#2B3333</drawable>
<!--<color name="pagerTabStrip">#353F3E</drawable>-->
<color name="tableHead">#FF444444</drawable>
</resources>
For setting the textview background color you can do like
tv.setBackgroundColor(R.color.tableHead);
Additionally for setting the textview textcolor you can do like
tv_empty.setTextColor(R.color.tableHead)
if there is a color in color constants like
<color name="error_red_color">#f00</color>
then it can be set as following -
tv.setTextColor(ContextCompat.getColor(context, R.color.error_red_color))
or
tv.setTextColor(getResources().getColor(R.color.error_red_color, null))
Other ways are -
tv.setTextColor(Color.RED);
tv.setTextColor(Color.parseColor("#FFFFFF"));
tv.setTextColor(Color.rgb(100,100,100));
My styles.xml file is refusing to work. What am i doing wrong? It says expected closing tag and needs attribute value...
<!-- Base application theme. -->
<style name="Theme.MainTheme" parent="Theme.AppCompat.Light">
<!-- colorPrimary is used for the default action bar background -->
<item name=”colorPrimary”>#colors/Primary</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name=”colorPrimaryDark”>#colors/PrimaryDark</item>
<!-- colorAccent is used as the default value for colorControlActivated, which is used to tint widgets -->
<item name=”colorAccent”>#colors/Accent</item>
</style>
colors.xml (stored in res\values)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Primary">#03A9F4</color>
<color name="PrimaryDark">#0288D1</color>
<color name="Accent">#1DE9B6</color>
</resources>
You need to say #color instead of #colors. Colors is the name of your colors.xml file, however you are seeking the color resources, which you access using #color
<item name="colorPrimary">#color/Primary</item>
I have an XML file I created in the res/value folder and try to reference it by writing
android:background="#colors/red"
and it does not work.
I also tried placing this file in the drawable folder but to no avail.
Refer to color resources with #color/name, not #colors/name.
For example, in res/values/whatever.xml:
<color name="your_color_name">#12345678</color>
Then in a layout xml:
android:background="#color/your_color_name"
Your color res file has to be under res/values/ (You may add it to a qualifier res/values folder though)
Add the color in this format:
<resources>
<color name="white">#ffffff</color>
</resources>
Refer to it using #color/white.
You can declare a color in your color.xml file like below,
<color name="aqua_blue">#6495ED</color>
And then use it below,
android:background="#color/aqua_blue"
easy way
android:background="#8EE8B5"
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#FFFFFF</color>
<color name="yellow">#FFFF00</color>
<color name="fuchsia">#FF00FF</color>
<color name="red">#FF0000</color>
<color name="silver">#C0C0C0</color>
<color name="gray">#808080</color>
<color name="olive">#808000</color>
<color name="purple">#800080</color>
<color name="maroon">#800000</color>
<color name="aqua">#00FFFF</color>
<color name="lime">#00FF00</color>
<color name="teal">#008080</color>
<color name="green">#008000</color>
<color name="blue">#0000FF</color>
<color name="navy">#000080</color>
<color name="black">#000000</color>
</resources>
make one xml named colors.xml in res and add color names and codes as you want custum
I added a color folder, with this xml file:
<?xml version="1.0" encoding="utf-8"?>
<item
xmlns:android="http://schemas.android.com/apk/res/android">
<color name="orange">#FF9912</color>
</item>
But when I put as the value in the screen_display.xml that i created in the values folder. It gives me a mistake:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme.Background" parent="#android:style/Theme">
<item name="android:windowNoTitle"> true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowBackground">#colors/color/orange</item>
</style>
</resources>
UPDATE
<activity android:name=".EasyLearningActivity"
android:launchMode="singleTask"
android:alwaysRetainTaskState="true"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
android:theme="MyTheme.Background"...shows mistake, saying that Strying type inst allowed :(
>
pls chk out this
in values folder create two xml file first one
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="orange">#FF9912</color>
</resources>
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme.Background" parent="#android:style/Theme">
<item name="android:windowNoTitle"> true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowBackground">#color/orange</item>
</style>
</resources>
In manifest file:
<activity android:name=".EasyLearningActivity"
android:launchMode="singleTask"
android:alwaysRetainTaskState="true"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
android:theme="#style/MyTheme.Background"></activity>
It's #color, not #colors...
and are you setting the android:theme attribute for your application tag in the manifest to use MyTheme.Background?
You can't use directly string for android:theme.
You need to include one of the styles like **#style/**MyTheme.Background.
Thanks for the color.xml , finally after 7 hours of research and lots of frustration I now have a purple action bar like I wanted.
<color name="orange" type="color">#FF9912</color>
<color name="red" type="color">#FF0000</color>
<color name="blue" type="color">#FF33B5E5</color>
<color name="purple" type="color">#FFAA66CC</color>
<color name="green" type="color">#FF99CC00</color>
<color name="darkblue" type="color">#FF0099CC</color>
<color name="darkpurple" type="color">#FF9933CC</color>
<color name="darkgreen" type="color">#FF669900</color>
<color name="darkorange" type="color">#FFFF8800</color>
<color name="darkred" type="color">#FFCC0000</color>
<!--Black #000000 (0,0,0)
White #FFFFFF (255,255,255)
Red #FF0000 (255,0,0)
Lime #00FF00 (0,255,0)
Blue #0000FF (0,0,255)
Yellow #FFFF00 (255,255,0)
Cyan / Aqua #00FFFF (0,255,255)
Magenta / Fuchsia #FF00FF (255,0,255)
Silver #C0C0C0 (192,192,192)
Gray #808080 (128,128,128)
Maroon #800000 (128,0,0)
Olive #808000 (128,128,0)
Green #008000 (0,128,0)
Purple #800080 (128,0,128)
Teal #008080 (0,128,128)
Navy #000080 (0,0,12-->
<!--color name="orange" type="color">#FFFFBB33</color-->
<!--<color name="red" type="color">#FFFF4444</color-->