`I am utilizing list selector, but its not working acoordingly. I am gettings effects after clicking list item, but there is no effect before item is clicked. Here is my code for selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="#drawable/ic_launcher" />
<item android:state_pressed="true"
android:drawable="#drawable/headerplain" />
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/headerplain" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="#drawable/headerplain" />
</selector>
and I have applied it to listView as
<ListView
android:id="#+id/listView123"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/list_selector"
android:drawSelectorOnTop="true"
android:focusable="true"
>
</ListView>
Please help me to get out of this problem
Thanks in advance
This is a good tutorial explaining very well how selector works
I have rowback and rowback_selected in my drawable folder...
and Use android:state_focused,android:state_selected,android:state_pressed...
<item android:state_focused="true" android:drawable="#drawable/rowback" />
<item android:state_pressed="true" android:drawable="#drawable/rowback_selected" />
<item android:state_selected="true" android:state_focused="false"
android:state_pressed="false" android:drawable="#drawable/rowback_selected" />
<item android:state_window_focused="false"
android:drawable="#drawable/rowback" />
Related
I added a simple ListSelector to the ListView.
The Problem is, that the ListSelector is only displayed, after the ListView is scrolled a little.
How can I make the ListSelector to apear immediately on select?
ListView
<ListView
android:id="#+id/contentListView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_above="#id/buttonEntwerten"
android:scrollingCache="false"
android:cacheColorHint="#00000000"
android:listSelector="#drawable/list_selector"
/>
#drawable/list_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/list_selector__gradient_bg_hover" />
</selector>
you can check out how android has configured it, and change it accordingly (file name is "list_selector_background.xml") :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="#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>
in short, you just have to handle the state_pressed state . the rest are good states to handle too.
the problem in your code is that it has no states.
I want to customize the selection effect of the items in the listView, but I don't know the way.
Will be useful if I should change the selection item background with a different png when the user select the item.
Any suggestion?
Use a selector for this:
<ListView android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:divider="#null"
android:dividerHeight="0dip"
android:listSelector="#drawable/list_selector" />
the selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/yourdrawable" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/yourdrawable" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/yourdrawable" />
<item android:drawable="#drawable/yourdrawable" />
</selector>
Here is a good tutorial for it.
What you are looking for is selector
Create a selector xml in res and set the background for the list item to the selector xml.
Example:
<?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/selected" />
<item
android:drawable="#drawable/normal" />
</selector>
Also look at:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
I want to change the background of the button when the button is unclickable.I have used selector for that but it is not working in the case when the button remains unclickable.
This is the selector file:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#drawable/button_lightgrey" />
<item android:state_pressed="true" android:state_enabled="true" android:drawable="#drawable/button_blue"/>
<item android:state_focused="true" android:drawable="#drawable/button_darkgreen" />
<item android:drawable="#drawable/button_lightgreen" />
</selector>
This is the button where i am using this selector file:
<Button
android:id="#+id/PrevButton"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_marginLeft="5.0dip"
android:layout_weight="1.0"
android:background="#drawable/ibtn"
android:onClick="onPrevButtonClick"
android:text="Prev" />
Please help me.All other functions are working in the selector file but only the unclickable button case is not working.Thanks
instead of button.setClickable(false) do button.setEnabled(false) and use the same selector xml.
this may helps you
i think in your selector file having issue try selector file like below
pls change images as you require
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/img_btn_repeat_pressed"
android:state_focused="true"
android:state_pressed="true" />
<item android:drawable="#drawable/img_btn_repeat_pressed"
android:state_focused="false"
android:state_pressed="true" />
<item android:drawable="#drawable/img_btn_repeat_pressed" android:state_focused="true" />
<item android:drawable="#drawable/img_btn_repeat"
android:state_focused="false"
android:state_pressed="false" />
</selector>
Just modify your selectors with below selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false" android:state_selected="false"
android:state_pressed="false" android:drawable="#android:color/transparent" />
<item android:state_focused="false" android:state_selected="true"
android:state_pressed="false" android:drawable="#drawable/round_corner_button_sort" />
<!-- Focused states -->
<item android:state_focused="true" android:state_selected="false"
android:state_pressed="false" android:drawable="#drawable/round_corner_button_sort" />
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="#drawable/round_corner_button_sort" />
</selector>
Hope this helps you.
Edit
As per your comments you can't use selector for button which is the state as false(button.setclickable(false);) In this state you can't use your selector
I'm using this code sheet to define textView's color in different events. The textView is in listView (10+ items) and the effect (color) applies on every textView in every listItem. How I can change color of only textView that is pressed?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="true"
android:color="#color/text_white" />
<item
android:state_focused="false"
android:state_pressed="true"
android:color="#color/text_white" />
<item
android:state_focused="true"
android:color="#color/text_white" />
<item
android:state_selected="true"
android:color="#color/text_white" />
<item
android:state_checked="true"
android:color="#color/text_white" />
<item
android:state_selected="false"
android:state_checked="false"
android:state_focused="false"
android:state_pressed="false"
android:color="#color/text_blue" />
</selector>
try this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" //<<<<<Here focus us true then txt_pressed
android:state_pressed="false"
android:drawable="#drawable/txt_pressed" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/txt_pressed" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/txt_pressed" />
<item android:drawable="#drawable/txt_default" />
</selector>
In the xml file, to the textview you want to change color, add
android:background="#drawable/filename"
I have set a custom list selector for a Simple ListView but when I select an item, the whole ListView turns blue. I don't understand where the problem is.
This is my ListView:
<ListView android:id="#+id/TopListView" android:layout_width="fill_parent"
android:listSelector="#drawable/regular_selector"
android:layout_height="wrap_content">
</ListView>
and it's the regular_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_focused="true"
android:drawable="#color/transparent" />
<item android:state_pressed="true"
android:drawable="#color/blue" />
<item android:state_focused="true"
android:drawable="#color/blue" />
</selector>
I was having the same issue of the whole list view highlighting, when using a color as the background. Curiously this only happened below api 11.
Solution was to use a solid shape drawable to wrap the colour:
list_selector_shaped_background_press.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#color/list_selector_pressed"/>
</shape>
List_selector_background.xml
<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" />
<!-- this has to be in a shaped drawable otherwise the whole list is highlighted selects -->
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/list_selector_shaped_background_pressed" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/list_selector_shaped_background_pressed" />
<item android:state_focused="true"
android:drawable="#drawable/list_selector_background_focus" />
</selector>
I guess it is the usual problem. You have to set this in your listview:
android:cacheColorHint="#00000000"
The goal is to disable an optimization that the framework does for improving drawing performance during scrolling operations.
When I set the regular_selector.xml as listview item background it work!