I'm struggling with ListView selector. I want to have gray color on list item when the user has his finger on the screen. That's all. When the user releases the finger (or move it elsewhere) the color should back to white as it should be all the time. This is my ListView:
<ListView android:layout_width="match_parent"
android:layout_height="match_parent"
android:duplicateParentState="true"
android:textAppearance="?android:textAppearanceMedium"
android:drawSelectorOnTop="false"
android:listSelector="#drawable/my_list_selector"
android:id="#+id/list"/>
This is selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary"
android:state_activated="true"/>
<item android:drawable="#color/colorPrimaryDark"
android:state_activated="false"/>
</selector>
What am I doing wrong in here?
P.S. If true and false are given in the opposite way nothing happens.
Seems like you want to use android:state_pressed instead of android:state_activated
code
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary"
android:state_pressed="true"/>
<item android:drawable="#color/colorPrimaryDark"/>
</selector>
After the discussion with OP and #pskink the issue was resolved with
<item android:state_window_focused="false" android:drawable="#color/transparent"/>
Related
I'm selecting
lst_Center.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
and adding a listView:
android:choiceMode="singleChoice"
and LinearLayout:
android:background="#drawable/list_selector"
And adding a list_selector.xml:
<item android:drawable="#color/Blue" android:state_enabled="true"/>
<item android:drawable="#color/Red" android:state_pressed="true"/>
But it's not working, how can I fix it?
Create list_background.xml in drawable folder and add below lines.
<?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/anycolor" />
<item android:state_focused="true" android:drawable="#color/anycolor" />
<item android:drawable="#color/anycolor" />
</selector>
Set background for your ListView.
android:background="#drawable/list_background"
If you want to highlight only the selecting list item means then you should set the background for textview. For that just go through about custom adapter.
Hope it helps you.
I am trying to customize a Button UI in Android.
I tried the following things:
btn.setBackgroundColor
btn.setBackgroundResource
btn.setBackgroundColor
But all of these are increasing the size of the Button, and because of that the Buttons near by can not be segregated (??).
Please suggest something.
If you want to apply a Hover effect then you have to do this in your XML where button layout is like
Button
android:id="#+id/xyz"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="15dp"
android:background="#drawable/general_btn_hover_effect"
android:onClick="somefunction"
android:text="#string/search_number"
android:textColor="#ffffff"
android:textSize="20sp"
/>
Notice android:background="#drawable/general_btn_hover_effect" there and then in #drawable folder make a general_btn_hover_effect.xml and write this into it
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/blank_normal_bg" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="#drawable/blank_hover_bg" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="#drawable/blank_hover_bg" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="#drawable/blank_normal_bg"/>
</selector>
Changing background color can not change size of button. Maybe you changed size in xml layout. Check again
I have a selector for textColor of a RadioButton like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#fff"/>
<item android:state_focused="true" android:color="#f00"/>
<item android:state_pressed="true" android:color="#0f0"/>
<item android:state_focused="false" android:state_pressed="false" android:color="#00f"/>
</selector>
I expected that the one selected RadioButton will have different color than the others.
However, all of the RadioButtons have blue text (using android:state_focused="false" android:state_pressed="false"), even the one that is selected.
What am I doing wrong?
It looks like you're just using the wrong selectors. The docs describe selecting as follows:
During each state change, the state list is traversed top to bottom and the first item that matches the current state is used—the selection is not based on the "best match," but simply the first item that meets the minimum criteria of the state.
Source link
So, in order:
state_selected is never true as RadioButtons use state_checked
when checked.
state_focused is never called because RadioButton
will never receive input focus.
state_pressed should be working.
When you hold your finger down you don't see the text appearing
green?
state_focused false and state_pressed false ends up being
default so you see blue.
If you would like to see different states, try these:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#0f0"/>
<item android:state_checked="true" android:color="#fff"/>
<item android:color="#00f"/>
</selector>
I have tested the above and can see all colors being expressed appropriately.
According to Android.
https://developer.android.com/guide/topics/resources/color-list-resource.html.
https://developer.android.com/reference/android/content/res/ColorStateList.html
You have to create a folder called 'color' in 'res' directory and create a new file called radiobuttonstate.xml for example which it looks 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:color="YOUR COLOR" />
<item
android:state_pressed="true"
android:state_enabled="false"
android:color="YOUR COLOR" />
<item android:color="YOUR COLOR"
android:state_checked="true"/>
<item
android:state_enabled="false"
android:color="YOUR COLOR" />
<item android:color="YOUR COLOR" />
</selector>
then in your radio button define in the android:textColor attribute your color list you previously defined.
<RadioButton
android:id="#+id/radio_H"
android:layout_width="30dp"
android:layout_height="30dp"
android:text="#string/string_example"
android:textColor="#color/radiobuttonstate"
android:textAlignment="center" />
The answer provided by #GrantAmos is perfect and working. If you want to text color selector through XML, please use this code.
android:textColor="#color/textview_selector"
However, if you want to set the selector programmatically, use this code -
radioButton.setTextColor(ContextCompat.getColorStateList(getContext(), R.color.textview_selector));
Hope it will save someone's time.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#color/dark_grey"/>
<item android:state_checked="true" android:drawable="#color/topic_green"/>
</selector>
This one works for me. Actually when i use android:color="#color/dark_grey". It didn't work. But when i changed to drawable it did.
<style name="MyRadioButton" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">#color/lbl_login</item>
<item name="colorControlActivated">#color/btn_login_back_color</item>
</style>
<RadioButton
android:id="#+id/btn_radio_credit_card"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/_8sdp"
android:fontFamily="#font/poppins_regular"
android:paddingStart="#dimen/_14ssp"
android:paddingEnd="#dimen/_1sdp"
android:text="Credit Card"
android:textColor="#color/lbl_login"
android:textSize="#dimen/_14ssp"
android:theme="#style/MyRadioButton" />
Refrence
So I'm trying to set a custom ExpandableListView indicator icon but it isn't working. I created an icon and saved it to drawable folder. Here are the icons:
This is the selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/arrow_up" android:state_empty="true"/>
<item android:drawable="#drawable/arrow_down" android:state_activated="true"/>
</selector>
ExpandableListView xml:
<ExpandableListView
android:id="#+id/lvExp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:groupIndicator="#drawable/custom_arrow" />
However, when I launch the app, there is not indicator at all. Any idea what I have done wrong?
this should do the trick
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_expanded="true" android:drawable="#drawable/arrow_up" />
<item android:drawable="#drawable/arrow_down" />
</selector>
you wanted the state_expanded for when the row gets expanded and then any other state besides expanded is just the normal down arrow. You should however handle the state_empty when there is nothing to expand to
I think you just need to add this one line to your selector and change the arrow down condition:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/arrow_up" android:state_empty="true"/>
<item android:drawable="#drawable/arrow_down" android:state_expanded="true"/>
<item android:drawable="#drawable/arrow_up" />
</selector>
I've the following problem: I made a custom state selector for Android Buttons. They work like expected. The problem begins when I want the same states to apply to a ListView item row.
So I set the style in the Adapter and now I discover that the same state looks different on the listbox view item. It is more of a blend between the default style and the color I want to set. When I long press the item or release the item, I notice that it fades away first into the default color (blue) and then transparent.
I guess I do miss some state/state combination, but I can't figure out which. I tried a lot, also examples from StackOverflow, but nothing works. Hopefully someone here had the same challenge and has a correct override. Below is my drawable selector.
Thanks in advance!
XML:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime"
android:enterFadeDuration="#android:integer/config_shortAnimTime"
>
<item android:state_window_focused="false"
android:state_enabled="true"
android:drawable="#android:color/transparent" />
<item android:state_window_focused="false"
android:state_enabled="false"
android:drawable="#android:color/transparent" />
<item android:state_window_focused="false"
android:state_focused="true"
android:drawable="#android:color/transparent" />
<item android:state_pressed="true"
android:drawable="#android:color/holo_green_light" />
<item android:state_focused="true"
android:state_pressed="false"
android:state_enabled="true"
android:drawable="#android:color/holo_green_light" />
<item android:drawable="#android:color/transparent" />
</selector>
In touch mode there is no such thing as selected or focused when you are not pressing on an item.
But you can work around it. You need to set choice mode for the list (single or multiple, the default is.none) and then use state_activated in your selector.