Unable to resolve color value - android

I am trying to make my buttons change the color of their text when it is pressed but i ran into a problem that i cant solve. I get an error saying "unable to resolve color value" then it gives me the path to the file. here are my files im using
This one is in a new folder named color under resources and its called button
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#ffff0000" />
<item android:state_focused="true" android:color="#ff0000ff" />
<item android:color="#ff000000" />
</selector>
And
<Button android:text="Main Menu" android:textColor="#color/button"
android:layout_width="200px" android:id="#+id/mainmenu"
android:layout_height="55px" android:layout_x="5dip"
android:layout_y="174dip"
android:textSize="18px">
</Button>
its driving me crazy if anybody could help me.

I successfully did it like this:
Files:
/drawable/button_states.xml
/layout/main.xml
/values/colors.xml
button_states.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#color/red" /> <!-- pressed -->
<item android:state_focused="true"
android:color="#color/blue_background" /> <!-- focused -->
<item android:color="#color/white" /> <!-- default -->
</selector>
colors.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="blue_background">#FF2f74c3</color>
<color name="white">#fefefe</color>
<color name="red">#ff0000</color>
</resources>
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_height="wrap_content"
android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:textColor="#drawable/button_states" />
</LinearLayout>

Try android:background instead.

Related

Change color of a disabled control [duplicate]

It isn't looking nice when using EditText enabled="false". How can I change it
automatically for all of the controls?
I've added this image for reference.
Can someone help me? Thanks.
Create a custom selector in drawables and set it as
<EditText android:textColor="#drawable/edit_text_selector" />
1) in 'colors.xml' define the color for enabled and disabled state:
<color name="enabled_color">#F7FE2E</color>
<color name="disabled_color">#000000</color>
2) in 'drawable/edit_text_selector.xml':
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#color/disabled_color"/>
<item android:color="#color/enabled_color"/>
</selector>
3) in your layout.xml:
<EditText
android:id="#+id/customEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:text="Hello!"
android:textColor="#drawable/edit_text_selector" />
In the color file define your color:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#color/disabled_color" />
<item android:color="#color/normal_color"/>
</selector>
In the layout:
<EditText
android:text="whatever text you want"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#drawable/example" />
</EditText>

How to design up arrow in button in xml

I want to design like the image given below: when I select any button it should appear up arrow with background color on selected button.
Use button selector for it like this. or follow this link http://www.mkyong.com/android/android-imagebutton-selector-example/
button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_pressed_yellow"
android:state_pressed="true" />
<item android:drawable="#drawable/button_focused_orange"
android:state_focused="true" />
<item android:drawable="#drawable/button_normal_green" />
</selector>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/imageButtonSelector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/new_button" />
</LinearLayout>

Why is this ripple effect is not working as expected?

I am using a drawable to change the background and text of textViews inside of my navigation drawer. I would like to keep the background white for the text area, but by testing keeping the background white does not show the ripple effect on the background, instead it does it to the text making the text gray. In the picture below, the middle one is being pressed causing the ripple effect.
Here are my drawable files that are used to make the changes in colors
Background:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/selected" android:state_activated="true" />
<item android:drawable="#color/white" />
</selector>
Text:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/primary" android:state_activated="true" />
<item android:color="#color/primary_text" />
</selector>
textView layout file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25.0sp"
android:background="#drawable/activated_background"
android:textColor="#drawable/activated_text"
android:id="#id/text1"
android:layout_weight="0"
android:layout_marginTop="8dip"
android:layout_marginBottom="8dip" />
</RelativeLayout>
you should use 2 drawable files and use it as your view background.
for pre-lolipop versions:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white_list_item_selected_background" android:state_pressed="true" />
<item android:drawable="#android:color/white" />
</selector>
for lolipop (v21):
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/white_list_item_selected_background" >
<item android:drawable="#android:color/white" />
</ripple>
If you use custom background, ripple effect will not be shown. You may have to use other ripple library such as Material Ripple.
In my case ripple effect is working after the first click, but for first click it didn't work for me. Have changed the background selector file with android:state_activated="true" and in main.xml android:clickable="true" then it's work fine for all time.
selector.xml (under res\drawable\selector.xml)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="#drawable/card_bg_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:state_activated="true" android:drawable="#drawable/card_bg_focused" android:state_enabled="true" android:state_focused="true"/>
<item android:state_activated="true" android:drawable="#drawable/card_bg_selected" android:state_enabled="false" android:state_selected="true"/>
</selector>
In activity_main.xml
<com.mysample.RecyclingImageView
android:id="#+id/imageview_overlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/selector"
android:clickable="true" />

Change listview row item background color and text color in android

In my custom listview, I have used relativelayout and inside that added 2 textviews vertically.
Background image is set to relativelayout. Now I want to change background image as well as textcolor of textview when listview item is pressed.
How to do this, any one have idea?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/item_bg"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#color/orange"
android:textSize="18sp"
android:textStyle="bold"
android:layout_marginTop="10dip"
android:layout_marginLeft="10dip"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="20dip"
android:layout_marginTop="8dip"
android:ellipsize="end"
android:maxLines="3"
android:duplicateParentState="true" <!-- this is a line to use..-->
android:textColor="#color/_textcolor"
android:textSize="14sp" >
</TextView>
item_bg.xml inside res/drawable folder:
<?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/bannerbg_hover"/>
<item android:state_focused="true" android:drawable="#drawable/bannerbg_hover" />
<item android:state_selected="true" android:drawable="#drawable/bannerbg_hover"/>
<item android:drawable="#drawable/bannerbg"/>
</selector>
_textcolor.xml inside res/color/ folder:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:color="#color/white"/>
<item android:state_focused="true" android:color="#color/white" />
<item android:state_selected="true" android:color="#color/white"/>
<item android:color="#color/dark_blue"/>
</selector>
Concerning the background, you should create a StateList Drawable and set it as the background of your rows.
Ex of a such drawable (could be named background_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="#color/color1" /> <!-- pressed -->
<item android:drawable="#color/color2" /> <!-- default -->
</selector>
And in the layout declaration of your row :
...
android:background="#drawable/background_selector"
...
Use the same way for your text with the Color State List

textColor: Unable to resolve color value

I got a stange error (no this won't help...)
I got a seperate layout for the tabbar. tabs_bg.xml is the layout. tab_text_selector.xml for the textColor and colors.xml. (Nameing of the colors might be misleading, but this is for testing)
If I click on the Graphical Layout, I got the error: java.lang.NumberFormatException: For input string: "#color/tab_unselected"
tabs_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabsLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:gravity="center"
android:orientation="vertical"
android:background="#drawable/tabbar_background" >
<TextView android:id="#+id/tabsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="15dip"
android:ellipsize="marquee"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:textColor="#drawable/tab_text_selector"/>
</LinearLayout>
tab_text_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
android:color="#color/tab_unselected" />
<item android:state_focused="true"
android:color="#color/tab_unselected" />
<item android:state_pressed="true"
android:color="#color/tab_selected" />
<item android:color="#color/tab_selected" />
</selector>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="tab_selected">#036DAB</color>
<color name="tab_unselected">#f36f25</color>
</resources>
Try to use uppercase letters in your colors instead of lowercase letters.
<color name="tab_unselected">#F36F25</color>

Categories

Resources