How to put a border on a ListView item when the user selects that item in the android application?
How to put a border on a ListView item when the user selects that item in the android application?
Follow these steps:
1 - Add the attribute android:choiceMode
to ListView ,
to allow you to select only one line:
android:choiceMode="singleChoice"
to allow you to select more than one line:
android:choiceMode="multipleChoice"
To customize the appearance of the selection requires a Chooser.
2 - Create the draw / drawable folder to use in the Chooser:
list_item_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="cor do fundo quando selecionado"/>
<stroke
android:color="cor da borda quando selecionado"
android:width="3dip"/>
</shape>
list_item_unselected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="cor do fundo quando não selecionado"/>
<stroke
android:color="cor da borda quando não selecionado"
android:width="3dip"/>
</shape>
Put the colors to your liking!
If in any case you want the color to be the one at the bottom of the ListView use @android:color/transparent
3 - Create the Chooser:
list_item_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_selected" android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_selected" android:state_selected="true"/>
<item android:drawable="@drawable/list_item_selected" android:state_activated="true"/>
<!-- não seleccionada -->
<item android:drawable="@drawable/list_item_unselected"/>
</selector>
4 - Assign the Selector to the item of the list:
android:background="@drawable/list_item_selector"
Note: I did not test, I hope I was not mistaken.