When you create an android button, the original color is grey. when you press it, the color changes, when you release it, it goes back to it's original grey.
that's what I would like to do with mine, but with other colors than the default grey. The problem is, when I change the color of my button then I click on it, nothing happens, as if I was clicking on an empty layout.
Here's the XML file i wrote to solve my problem - button_connexion_style.xml
?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#ff0000ff" android:state_focused="true" /> <!-- focused -->
<item android:color="#449D44"/> <!-- default -->
<item android:drawable="#278727" android:state_pressed="true"/>
</selector>
In my button XML code I do this to call the previous XML :
android:background="#drawable/button_connexion_style"
But it doesn't work.
If anyone can help, please let me know.
flat_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/rect_pressed"/>
<item android:drawable="#drawable/rect_normal"/>
</selector>
rect_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/rect_pressed" />
<item android:bottom="#dimen/layer_padding">
<shape android:shape="rectangle">
<corners android:radius="#dimen/corner_radius" />
<solid android:color="#color/colorPrimary" />
</shape>
</item>
</layer-list>
rect_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="#dimen/corner_radius" />
<solid android:color="#color/colorPrimaryDark" />
</shape>
add below line in your button
android:background="#drawable/flat_selector"
<?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/red" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#color/blue" />
<item
android:state_focused="true"
android:state_enabled="true"
android:drawable="#color/green" />
<item
android:state_enabled="true"
android:drawable="#color/grey" />
</selector>
Create an xml file in your drawable like above,
And set images/color accordingly and then set this xml as background of your imageButton.
Please do fill with according to your color code,
button_connexion_style.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/green" android:state_pressed="true" /> <!-- pressed -->
<item android:drawable="#color/blue" android:state_focused="true" /> <!-- focused -->
<item android:drawable="#color/default_green" /> <!-- default -->
</selector>
and add these color code in values colors ,
<color name="blue">#ff0000ff</color>
<color name="green">#278727</color>
<color name="default_green">#449D44</color>
You are almost right just one thing you miss default should always last statement
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#ff0000ff" android:state_focused="true" /> <!-- focused -->
<item android:drawable="#278727" android:state_pressed="true"/>
<item android:drawable="#color/yourDefaultColor" />
</selector>
let me know if not work
Related
I have this button XML, which has transparent background and when clicked, the color changes, so that it shows a "click effect".
Drawable XML:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- press-->
<item android:drawable="#android:color/transparent" android:state_pressed="false" />
<!-- focused-->
<item style="#style/AppTheme" android:drawable="#color/color_primary" android:state_pressed="true" />
<!-- normal-->
<item android:drawable="#android:color/transparent" />
</selector>
My question is, how to put border on this button? Or an XML example so you have the same result. Thank you!
create a drawable, similar to this, selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ff0000"/>
<stroke
android:width="1dp"
android:color="#00ff00" />
</shape>
then change this line
<item
style="#style/AppTheme"
android:drawable="#color/color_primary"
android:state_pressed="true" />
to
<item
style="#style/AppTheme"
android:drawable="#drawable/selected"
android:state_pressed="true" />
the solid is the background color and the border is the stroke, you can create drawables that are used in the diferent states of the selector
You can do something like this:
1. First create a drawable file like this:
<shape android:shape="rectangle">
<!--apply button background transparent, full opacity-->
<solid android:color="#00ffffff"/>
<!--make button border solid color, nontransparent-->
<stroke android:color="#stroke color" android:width="2dp"/>
<corners android:radius="2dp"/>
</shape>
then in your provided code replace these lines:
<!-- press-->
<item android:drawable="path to above drawable" android:state_pressed="false" />
<!-- focused-->
<item style="#style/AppTheme" android:drawable="#color/color_primary" android:state_pressed="true" />
<!-- normal-->
<item android:drawable="path to above drawable" />
as
<!-- press-->
<item android:drawable="path to above drawable" android:state_pressed="false" />
<!-- focused-->
<item style="#style/AppTheme" android:drawable="#color/color_primary" android:state_pressed="true" />
<!-- normal-->
<item android:drawable="path to above drawable" />
I have a ListView, showing the default color of the selector as shown in the picture below.
I would like to change color of this selector to, lets say the color teal.
ListView.xml:
<ListView
android:drawSelectorOnTop="true"
android:listSelector="#drawable/listitem_selector"
android:choiceMode="singleChoice"
android:id="#+id/lw"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
listitem_selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/teal" />
<item android:state_focused="true" android:drawable="#color/teal" />
<item android:state_selected="true" android:drawable="#color/teal" />
<item android:state_active="true" android:drawable="#color/teal" />
<item android:state_activated="true" android:drawable="#color/teal" />
<item android:state_enabled="true" android:drawable="#color/teal" />
<item android:state_checked="true" android:drawable="#color/teal" />
<item android:state_single="true" android:drawable="#color/teal" />
</selector>
As you can see, I've tried a bunch of state_* attributes with no success, the selected item shows as the default color. How do I change this appearance?
I'm just doing the same thing. This works for me.
I don't set the #drawable in listview, I have set it in format of listview i.e. in layout where you have put textViews and checkBoxes to be displayed in listview.
list_view_format.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/list_view_item">
//your ckeckbox and textviews
</RelativeLayout>
/res/drawable/list_view_item.xml
<item android:state_pressed="true" >
<shape>
<solid android:color="#color/list_item_pressed"/>
</shape>
</item>
<item android:state_activated="true" >
<shape>
<solid android:color="#color/list_item_activated"/>
</shape>
</item>
<item>
<shape>
<solid android:color="#color/list_item_normal" />
</shape>
</item>
/res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="list_item_normal">#96ffdcb5</color>
<color name="list_item_activated">#ffaa66</color>
<color name="list_item_pressed">#ffaa66</color>
</resources>
I have a very basic selector that I am using as the background for some buttons to achieve down states. Here's the xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:drawable="#color/home_button_blue_down" android:state_selected="true" />
<item android:drawable="#color/home_button_blue_down" android:state_pressed="true" />
<item android:drawable="#color/home_button_blue" />
</selector>
With this selector the fade animation will occur every time the button changes state. In other words, the transition will animate both when going from de-pressed to pressed and also when going back from pressed to de-pressed.
Now, my question is: is it possible to make it so that only one of these state changes animates? When a user presses the button, I want the downstate transition to occur immediately without animations. When the button becomes de-pressed, I want the downstate to fade out while the normal state fades back in. Can this be done?
Animations on selector : (my drawables were colors )
De-Press (Out)
android:exitFadeDuration="#android:integer/config_shortAnimTime"
Press (In)
android:enterFadeDuration="#android:integer/config_shortAnimTime"
Full example:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="#android:integer/config_shortAnimTime"
android:exitFadeDuration="#android:integer/config_shortAnimTime">
<item android:state_checked="false" android:drawable="#color/transparent"/>
<item android:state_checked="true" android:drawable="#drawable/circle_blue"/>
</selector>
You can do something like this:
<?xml version="1.0" encoding="utf-8"?>
<animated-selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/selected"
android:state_selected="true"
android:drawable="#color/home_button_blue_down"
/>
<item android:id="#+id/usual"
android:drawable="#android:color/transparent"
/>
<transition
android:fromId="#+id/usual"
android:toId="#+id/selected" >
<animation-list>
<!--fill in your animation here-->
</animation-list>
</transition>
</animated-selector>
Keep in mind animated-selector is available only after API 21.
More info in this official guide
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:enterFadeDuration="#android:integer/config_shortAnimTime"
android:exitFadeDuration="#android:integer/config_longAnimTime">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#ffffff"
android:endColor="#917777"
android:angle="270" />
</shape>
</item>
<item android:state_pressed="false" >
<shape>
<gradient
android:startColor="#ffffff"
android:endColor="#d2adad"
android:angle="270" />
</shape>
</item>
<item android:state_checked="true">
<color android:color="#color/black"/>
</item>
</selector>
<?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/tab_profile_pressed_mdpi" />
<item android:drawable="#drawable/tab_profile_unselected_mdpi" />
</selector>
And how I set them:
((ImageView)tabHost.getTabWidget().getChildTabViewAt(i).findViewById(R.id.single_tab_img)).setImageResource(unselected_img[i]);
You need to use setBackgroundResource not setImageResource. State drawables work on the background image, not the foreground image.
I believe you need another xml file that lays out what will happen when the tab is pressed.
Example:
tab.xml
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="#drawable/tab_bg_selected" />
tab_bg_selected
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#A8A8A8" android:centerColor="#7F7F7F"
android:endColor="#696969" android:angle="-90" />
</shape>
With the default selector, long-pressing a list item causes its background to transition between two colors.
Replacing the selector with the one below removes the effect. According to this question, I need an animation to reproduce it. How would I go about doing that in xml?
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true">
<shape>
<solid
android:color="#color/state_pressed" />
</shape>
</item>
<item
android:state_focused="true">
<shape>
<solid
android:color="#color/state_focused" />
</shape>
</item>
<item>
<shape>
<solid
android:color="#color/state_idle_grey" />
</shape>
</item>
</selector>
Here is the code from list_selector_background :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="#android:color/transparent" />
<!--
Even though these two point to the same resource, have two states so
the drawable will invalidate itself when coming out of pressed state.
-->
<item android:state_focused="true" android:state_enabled="false"
android:state_pressed="true" android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_enabled="false"
android:drawable="#drawable/list_selector_background_disabled" />
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/list_selector_background_transition" />
<item android:state_focused="true"
android:drawable="#+drawable/list_selector_background_focus" />
</selector>
Found on the web.
And it uses this transition for long press clicks :
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/list_selector_background_pressed" />
<item android:drawable="#drawable/list_selector_background_longpress" />
</transition>
Found on the web too .
There is no animation for that. And remember to keep you states in the same order, or at least think about it if you swap them, order is important.
Personnally, I like when things behave in a standard way, so I would just let the standard list selector.
Regards,
Stéphane