I have an ImageButton and I want to make it so the button background changes color when the button is pressed. I have copied the button_bg.xml file from this question.
button_bg.xml looks like this:
<?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"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
And line #54 looks like this:
<ImageButton
android:id="#+id/sendButton"
android:background="#drawable/button_bg"
android:src="#drawable/ic_action_send_now"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dp"
android:layout_alignParentRight="true" />
I have tried removing the line:
android:background="#drawable/button_bg"
which stops the application crashing but the buttons don't change color.
Any help would be appreciated
By changning button_bg.xml to the following:
<?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/clr_pressed"/> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#android:color/transparent"/> <!-- focused -->
<item android:drawable="#android:color/transparent"/> <!-- default -->
</selector>
and adding the following to strings.xml
<drawable name="clr_normal">#AAAAAA</drawable>
<drawable name="clr_pressed">#777777</drawable>
the problem was solved and the code worked as intended.
Related
I have a ListView, I want to override default color to my custom color(while clicking). I have created xml file: BlueBackground.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#color/pressed_color" /> <!-- focused -->
<item android:state_focused="true" android:state_pressed="true" android:drawable="#color/pressed_color" /> <!-- focused and pressed-->
<item android:state_pressed="true" android:drawable="#color/pressed_color" /> <!-- pressed -->
</selector>
colors.xml
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<color name="pressed_color">#4d90fe</color>
</resources>
setting background resource to listview:
nativeListview.SetSelector (Android.Resource.Drawable.BlueBackground);
While clicking it works perfectly, but when I press up the default color ("orange") appears for a while.
How can I remove that orange color?
I want to change background of a button when it is clicked. I tried to use a selector. But It didn't work. Here is the selector (add_grp_slctr.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="#drawable/add_grp_d"/>
<item android:state_pressed="true" android:drawable="#drawable/add_grp_d" />
<item android:drawable="#drawable/add_grp" />
</selector>
And the button :
<Button
android:id="#+id/addGrpBtn"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:background="#drawable/add_grp_slctr"
android:onClick="addGrpDialogOpen" />
add_grp_d and add_grp are images(png).
I tried a similar code which will be white by default, black when pressed on an onclick of a button:
//***This is the btn_selector which is to be declared in 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="#android:color/black" /> <!-- pressed -->
<item android:drawable="#android:color/white" /> <!-- default -->
</selector>
and called this on the button.xml -->
android:background="#drawable/btn_selector"
Hope this would help..:)
go through the http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList once. Also state_focussed for button only works when you are focussing the button using a hardware-keyboard.
As for your case
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="false" android:state_selected="false" android:drawable="#drawable/button_default"/>
<item android:state_pressed="true" android:state_selected="false" android:drawable="#drawable/button_default" />
<item android:state_pressed="false" android:state_selected="true" android:drawable="#drawable/button_selected"/>
<item android:state_pressed="true" android:state_selected="true" android:drawable="#drawable/button_selected" />
<item android:drawable="#drawable/button_selected" />
</selector>
Use your selector
change your code like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="#drawable/add_grp_d"/>
<item android:state_pressed="true" android:drawable="#drawable/add_grp" />
<item android:drawable="#drawable/add_grp_d" />
</selector>
My Code
<item android:drawable="#drawable/shadow_design_click" android:state_pressed="true"/>
<item android:drawable="#drawable/shadow_design" android:state_focused="true"/>
<item android:drawable="#drawable/shadow_design"/>
</selector>
I think you should change your selector a bit.
Check this answer here.
Instead of passing cuastom XML file, if you just want to change the color of you button then you can try with the following way.
Button lineColorCode = (Button)findViewById(R.id.button1);
Now inside button's click event use following code.
int color = Color.parseColor("#AE6118"); //The color u want
lineColorCode.setColorFilter(color);
I am trying to very simply style a Button. I just want it blue with with text when not pressed, and white with blue text when clicked.
I tried to do this with a style and a selector.
In my layout I have this Button:
<Button
android:id="#+id/button1"
style="#style/MyButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/login" />
And in res/values/styles I have this styles.xml:
<style name="MyButton">
<item name="android:background">#drawable/btn_background</item>
<item name="android:textColor">#drawable/btn_textcolor</item>
</style>
And of course, the two selectors, in res/drawable, btn_background.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/white" />
<item android:color="#color/SapphireBlue" />
</selector>
and btn_textcolor.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/SapphireBlue" />
<item android:color="#color/white" />
</selector>
The error I get now when I either run the app, or open the layout editor is:
<item> tag requires a 'drawable' attribute
I understand the message, but I don't have a drawable, is it is a simple, flat button.
How can I create such a simple button?
Update according to this post, it should work.
Try this way,hope this will help you to solve your problem.
btn_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/white" android:state_pressed="true"></item>
<item android:drawable="#drawable/white" android:state_focused="true"></item>
<item android:drawable="#drawable/SapphireBlue" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:drawable="#drawable/white" android:state_enabled="false"></item>
</selector>
btn_textcolor.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/SapphireBlue" android:state_pressed="true"></item>
<item android:color="#drawable/SapphireBlue" android:state_focused="true"></item>
<item android:color="#drawable/white" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
<item android:color="#drawable/SapphireBlue" android:state_enabled="false"></item>
</selector>
Background attribute expects a drawable and the drawable selector must be in one of the /drawable folders.
TextColor attribute expects a color and the color selector must be in one of the /color folders.
/res/color/my_textcolor_selctor.xml
/res/drawable/my_background_selector.xml
Also, the drawable selector must use 'android:drawable' for selector items (a color resource maybe used for the attribute value since a color is a drawable),
whereas the color selector must use 'android:color' for the selector items.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#color/disabledbackgroundColor" />
<!-- default -->
<item android:drawable="#color/myBackgroundColor" />
</selector>
<?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/disabledColor" />
<!-- default -->
<item android:color="#color/defaultColor" />
</selector>
android:background seems to accept drawable selectors but not color selectors... so just leave a color as your android:background, and use a normal color selector with android:backgroundTint, which expects colors anyway:
at styles.xml:
<style name="MyButton">
<item name="android:background">#color/some_fixed_color</item>
<item name="android:backgroundTint">#color/background_color_selector</item>
<item name="android:textColor">#drawable/btn_textcolor</item>
</style>
(res/color/background_color_selector.xml is a normal color selector.)
I want to set the color of row in listview if selected to yellow and otherwise be white so I use the following selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="#color/encounter_normal" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="#color/encounter_normal" />
<item android:state_enabled="true"
android:state_selected="true" android:drawable="#color/encounterselector_color" />
<item
android:drawable="#color/encounter_normal" />
</selector>
where
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="encounterselector_color">#fbeda5</color>
<color name="encounter_normal">#ffffff</color>
</resources>
and I use it like the following
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/EncounterGrid"
android:background="#drawable/encounterlistview"
>
<!-- remaining code -->
but the row is always white , any idea how to fix that
Setting the background color with a selector is a bit tricky. Basically you have to create a drawable for each color and use them in your android:drawable attributes. You cannot directly use colors.
Check this related question for more details.
I use the following:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the switched off state -->
<item android:state_enabled="false"
android:drawable="#drawable/grey_bar" />
<!-- These are the partial states -->
<item android:state_pressed="true"
android:drawable="#drawable/button_pressed" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/button_focused" />
<!-- This is the switched on state -->
<item android:state_enabled="true"
android:drawable="#drawable/button_normal" />
</selector>
Where all of the drawables I point to are defined in xml, or are existing 9 patch images.
use android:color on your selector and not android:drawable because you are getting reference to the color resources, so your selector will be like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true"
android:state_pressed="true" android:color="#color/encounter_normal" />
<item android:state_enabled="true"
android:state_focused="true" android:color="#color/encounter_normal" />
<item android:state_enabled="true"
android:state_selected="true" android:color="#color/encounterselector_color" />
<item
android:color="#color/encounter_normal" />
</selector>
Nothing was working for me until I set drawSelectorOnTop = "true".
Everything worked after that.
I'm trying to get a TableRow, in a TableLayout, to change the background color when a user touches it, but it doesn't seem to be working. When I click on the TableRow, nothing happens. This what I have so far:
Layout:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/BaseStyle"
android:stretchColumns="*">
<TableRow style="#style/TableRow">
<TextView
android:id="#+id/settings
style="#style/BaseStyle.SettingsText"
android:text="Settings"
android:onClick="onClick"
android:clickable="true"
/>
</TableRow>
</TableLayout>
Style:
<style name="TableRow">
<item name="android:background">#drawable/onclickhighlight</item>
</style>
Drawable (onclickhighlight.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active state -->
<item android:state_selected="true" android:state_focused="false" android:state_pressed="false" android:drawable="#android:color/background_light" />
<!-- Inactive state-->
<item android:state_selected="false" android:state_focused="false" android:state_pressed="false" android:drawable="#android:color/transparent" />
<!-- Pressed state-->
<item android:state_selected="true" android:state_focused="true" android:state_pressed="true" android:drawable="#android:color/background_light" />
</selector>
Colors:
<resources>
<color name="background_light">#FFFFFF</color>
</resources>
I know the TableRow is using the onclickhighlight StateListDrawable because if I change the "Inactive state"s android:drawable property from #android:color/transparent, to #android:color/background_light, the background color changes to white.
I am having exactly the same issue and I'm trying to find the answer. I bet if you were to remove the android:onClick="onClick" from the table row xml, you'd find the highlight would work. But then, how would you get the click event to associate with the method you want?!
I had the same problem, and this is what worked for me.
<?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/yourcolor" />
<item android:drawable="#color/yournormalbackground"/>
</selector>