How to Customize ListView Seletion in Android - android

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

Related

Android - Navigation View item menu background color

i try to change color of my item Navigation View menu :
styles.xml
<item name="android:activatedBackgroundIndicator">#drawable/activated_background</item>
activated_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/White" />
<item android:state_focused="true" android:drawable="#color/White" />
<item android:state_activated="true" android:drawable="#color/White" />
<item android:drawable="#android:color/transparent" />
</selector>
drawer.xml :
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="#+id/group_1">
<item
android:id="#+id/drawer_menu_account"
android:checked="false"
android:title="#string/menu_account"
android:icon="#drawable/ic_account_grey600_24dp" />
<item
android:id="#+id/drawer_menu_my_post"
android:checked="false"
android:title="#string/menu_my_post"
android:icon="#drawable/ic_pin_grey600_24dp" />
</group>
[...]
Background color don't change..
What am i doing wrong ?
You don't set the drawable for the background of a Navigation View item in your styles.xml file. Open up your XML layout file containing your Navigation View widget, and add the following line to the widget's attributes:
app:itemBackground="#drawable/activated_background.xml"
If you're having trouble with the "app" pointer, add the following line in as well:
xmlns:app="http://schemas.android.com/apk/res-auto"
Note that this only changes the colour of the background of a selected list item. If you'd like the icon and text colour to change as well, use the app:itemTextColor attribute instead.
Below code works fine for me
my nav view:
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="#color/nav.background"
app:menu="#menu/menu_drawer"
app:itemBackground="#drawable/nav_item_background"
app:itemTextColor="#drawable/nav_item_text"/>
drawable/nav_item_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:drawable="#color/nav.item.background" />
<item android:state_checked="true" android:drawable="#color/nav.item.background" />
<item android:state_focused="true" android:drawable="#color/nav.item.background" />
<item android:state_activated="true" android:drawable="#color/nav.item.background" />
<item android:drawable="#color/nav.item.background.default" />
</selector>
drawable/nav_item_text.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#android:color/white" android:state_checked="true" />
<item android:color="#bdbdbd" />
</selector>
You must use android:background="color" for back color of navigation menu
like:
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="#+id/shitstuff"
app:itemTextColor="#color/black"
app:menu="#menu/drawermenu"
android:background="#color/colorAccent"
android:layout_marginTop="-24dp"
/>
and for items color use itemTextColor="color"
To have the background display on your selected item you need to add
android:checkable="true"
on every item of your menu.xml items, then set :
app:itemBackground="#drawable/nav_view_item_background"
on your NavigationView and finally:
drawable/nav_view_item_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/primary" android:state_checked="true" />
<item android:drawable="#android:color/transparent" />
</selector>
#HaloMediaz and #HemantShori
To keep the background of the selected item in a selected state,
use android:state_checked="true" instead of android:state_pressed="true" in the color state resource.
So your activated color state resource should look like this:
activated_background.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#color/White" />
<item android:state_focused="true" android:drawable="#color/White" />
<item android:state_activated="true" android:drawable="#color/White" />
<item android:drawable="#android:color/transparent" />
</selector>

Android ListView displays ListSelector only after scrolling

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.

selector for listview doesn't work

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.

Android ListView selector not working well

`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" />

StateListDrawable not working on TableRow

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>

Categories

Resources