ListView item ListSelector drawable flickering - android

I have a custom drawable for my listSelection. When I select one Item it gets selected. But on selected when I scroll through the other items (scrolling up/down) the backgroung is flickering and sometimes it is also getting disappeared.
I am including my xml layouts:
ListView:
<ListView
android:id="#id/listView"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:listSelector="#drawable/list_selector"
android:clickable="true"
android:divider="#drawable/separetor_drawable"
android:fastScrollEnabled="true"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:scrollbars="none" >
</ListView>
drawable layout: list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="6dp" />
<solid android:color="#color/grey" />
<size android:width="50dp" />
</shape>
Also I even I specify shape width the shape automatically fill_parent and that goes for height as well.
Any suggestions?

//try in your listview attribute as
android:cacheColorHint = "#00000000"
or
android:cacheColorHint="#android:color/transparent"
at java code, you should use
listView.setCacheColorHint(Color.TRANSPARENT);

Try this in your listview:
android:listSelector="#android:color/transparent"
android:cacheColorHint="#00000000"
android:focusable="false"
android:focusableInTouchMode="false"

Don't know if you got your answer, but we had a similar problem at one point. We fixed it by setting a background on the outer element in our listitem's layout file:
listitem.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/listselector"
...>
....
</RelativeLayout>
and then the drawable.. listselector.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/blue" />
<item android:state_selected="true" android:state_pressed="false" android:drawable="#color/yellow" />
<item android:state_selected="false" android:state_pressed="false" android:drawable="#color/black" />
</selector>
The background xml file sets values for pressed, selected, and normal states and you can change the colors to other drawable's if you wish. Seemed to fix the weird background flicker problem we were having.

Related

Android Recycler View Ripple Effect not working

I am using the following xml drawable as the background of my recyclerview list item.
touch_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true"
android:drawable="#color/green_text_500"/>
<!-- Default, "just hangin' out" state. The base state also
implements the ripple effect. -->
<item android:drawable="#drawable/touch_selector_base" />
</selector>
touch_selector_base.xml
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/light_grey">
<item android:id="#android:id/mask" android:drawable="#color/light_grey" />
<item android:drawable="#color/dark_grey"/>
</ripple>
In the list item I am using the touch_selector.xml as follows in my list_item_quote.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="86dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:clickable="true"
android:focusable="true"
android:background="#drawable/touch_selector"
>
// layout
</LinearLayout>
SIMILARLY I have another xml drawable touch_selector_dark.xml and touch_selector_base_dark.xml
In my recycler-view-adapter I alternate between these two drawables based on the index. For even indexes I use the darker drawable and for the odd index I use the lighter background to produce an alternating effect. But the issue is that the ripple effect is not working.
Here are the colors:
light_grey = #5b5b5b
dark_grey = #212121
green_text_500 = #37863a
use this line in your recycleview list layout
android:background="?android:attr/selectableItemBackground"
try this
worked for me
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
I think your ripple file is not valid, you can check this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#color/dark_grey"/>
</shape>
</item>
</selector>
You can use android:foreground="#drawable/touch_selector" to display the ripple effect and also use android:background="" to set any other drawable background.

Yellow background on back of image On click

I am using GridView to show the gallery images. But when i click on any image, yellow color on back of image is coming. How can i remove it?
Here is the code i am using
<GridView
android:id="#+id/party_gallery_gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="#dimen/party_detail_layout_margin"
android:listSelector="#null"
android:numColumns="3"
android:scrollbars="none"
android:verticalSpacing="#dimen/party_detail_layout_margin"
android:visibility="gone" >
</GridView>
You can see the yellow background in the below image
You can a new XML bg_pressed.xml in drawable directory.
And it must contain <selector> tag
<?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="#000" />
</shape>
</selector>
and than
you can in Grid Cell
android:background="#drawable/bg_pressed.xml"
I hope ıt helps :)
Add this line to gridview
android:listSelector="#00000000"
Try to make gridview background trasnparent, like this way :
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridView"
android:numColumns="2"
android:gravity="center"
android:background="#ffffffff"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
Find your item view layout file and change its background to a corresponding background drawable.
Here is an example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="#dimen/bb_drawerlayout_menu_item_height"
android:background="#drawable/appkit_drawerlayout_menu_item_bg"
android:clickable="true"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingEnd="12dp"
android:paddingStart="0dp"
tools:background="#color/black"
tools:layout_width="240dp">
The content of appkit_drawerlayout_menu_item_bg is as below:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/bbkit_black" android:state_pressed="true"/>
<item android:drawable="#color/bbkit_black" android:state_activated="true"/>
<item android:drawable="#color/transparent" android:state_pressed="false"/>
<item android:drawable="#color/transparent"/>
</selector>
Hope this makes sense to you.

Selector in Custom View With 9 Patch Image Background

I have problem with implementing selector in custom view. I have searched it for the last 1 hour but I could not find the solution. I have custom view with imageview, some textviews and nine patch background but the problem is when it's on pressed state, the color of selector doesn't cover the image view.
For the implementation, I'm using background selector for the layout and I just change the 9 patch image on pressed and un-pressed state, then disable default selector in listview.
Do you have any better solution? I see in google play, the blue selector covers all of the views and that is what I want.
For the xml,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/touch_selector"
android:orientation="horizontal"
android:padding="16dp" >
<ImageView />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView />
<TextView />
</LinearLayout>
</LinearLayout>
And for the touch selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/card_background_pressed" android:state_focused="true"/>
<item android:drawable="#drawable/card_background_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/card_background_white" android:state_focused="false" android:state_pressed="false"/>
</selector>
where card_background is 9 patch image.
EDIT :
This is the example from google play,
The accepted is not correct.
It's possible to use a nine-path in the selector, you just need to use the <nine-patch>. Something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<nine-patch
android:src="#drawable/marker"/>
</item>
<item>
<shape>
<solid android:color="#000000" />
</shape>
</item>
</selector>

Android - ListView Item not changing its state

I'm currently facing a problem with background drawables on ListView Items.
I have a ListView XML, an Item XML, and a Drawable XML for the differents states that one item can have.
The problem is that when I click or press one of the item nothing changes visually, but the click works because the onItemClick() method I overrided is called and its code executed... Its like I didn't set the #background parameter !
layout/my_activity.xml (which contains the listview) :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical" >
<include ... />
<include ... />
<View
... />
<ListView
android:id="#+id/listViewPacks"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#color/blue_vdark"
android:dividerHeight="2dp" >
</ListView>
</LinearLayout>
layout/listview_item_a.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="wrap_content"
android:background="#drawable/listview_item_a_d"
android:orientation="vertical"
android:padding="5dp" >
<TextView
... />
<TextView
... />
<TextView
... />
</LinearLayout>
drawable/listview_item_a_d.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"><shape>
<gradient android:angle="270" android:endColor="#bbbbbb" android:startColor="#e9e9e3" />
</shape></item>
<item android:state_enabled="true"><shape>
<gradient android:angle="270" android:endColor="#ecca2e" android:startColor="#f9f7c9" />
</shape></item>
<item><shape>
<solid android:color="#color/gray_dark" />
</shape></item>
</selector>
First, there is no persistent selected or focused state in touchmode.
You can get around this by using state_activated.
You can achieve this by setting the choice mode for the list to single or multiple (the default is none).
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
Then use the activated state in your selector XML:
<item android:state_activated="true">
<shape>
<gradient android:angle="270" android:endColor="#bbbbbb" droid:startColor="#e9e9e3" />
</shape>
</item>
Note that state_activated is for API 11+... for previous versions I believe you would have to go with an array in a custom adapter to keep track of the selected state and use that to set the background color/drawable in your adapter getView method.
I have an adapter with my ListView and the problem was I changed my views' background in the getView method so I think the selector was drawn behind, or maybe not drawn at all. Anyway I just removed those lines of code and now it works fine, thanks !

Selector for custom list view "Change ListView background - strange behaviour"

I am using this tutorials and I want to add selectors for this list view. I tried some codes but it doest work. How can I do it.
I used code as list_selector.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/list_selector_background_focus" />
<item android:state_pressed="true"
android:drawable="#drawable/list_selector_background_pressed" />
</selector>
and my List view
<ListView
android:id="#+id/select_names_tags_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/list_selector"
android:layout_alignParentTop="false"
android:layout_gravity="center_vertical">
</ListView>
Just change a word here from listSelector to background. Because you want it as the background drawable selector, right?
<ListView
android:id="#+id/select_names_tags_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:layout_alignParentTop="false"
android:layout_gravity="center_vertical">
</ListView>
EDIT:
try changing your selector file to (I changed the order of the two items):
<?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/list_selector_background_pressed" />
<item android:state_focused="true"
android:drawable="#drawable/list_selector_background_focus" />
</selector>

Categories

Resources