I have a scrollable listview. I need to highlight the top and bottom border of the row like this .As am a newbie tell me how to do this. Thanks in advance

- 1,419
- 3
- 23
- 38
5 Answers
android:listSelector="@drawable/list_selectorcolor"
In drawable create like this
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="90"
android:endColor="#000000"
android:startColor="#000000"
android:type="linear"/>
<stroke
android:width="1dp"
android:color="#800080"
android:dashWidth="2dp"/>
</shape>

- 1,994
- 1
- 16
- 21
-
This is working but i need borders only on top and bottom of the view not on sides. can u please tell me how can i do that – AndroidOptimist Sep 07 '13 at 06:23
-
Stroke can in four sides, better create a image like that set as background instead of drawable – APriya Sep 07 '13 at 06:38
Create a selector file like this for different states of list view under drawable folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/app_tint_pressed"
android:state_selected="true"/>
<item android:drawable="@color/app_tint_pressed"
android:state_pressed="true" />
<item android:drawable="@color/app_tint" />
</selector>
Apply this file like this under ListView in xml
android:listSelector = @drawable/myselector
For different states check state list on this link http://developer.android.com/guide/topics/resources/drawable-resource.html
you can use nine-patch images and color as well for different states in your case the normal state is black color and for pressed state either create a nine-patch image with black and borders you want or you can create it in xml as well
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@android:color/white"/>
<stroke
android:width="2dp"
android:color="#800080"
android:dashWidth="2dp"/>

- 4,872
- 8
- 35
- 46
-
here for app_tint color i need to display color code in string file ah? it's showing error – AndroidOptimist Sep 07 '13 at 05:28
-
app tint color in your case would be black as per your image so you can use #000000 or @android:color/black or you can create a color file under values folder and define the color there and refer it like i have done @color/app_tint like you do for string resources the difference is colors are defined with color tag. check this link http://stackoverflow.com/questions/3769762/android-color-xml-resource-file – Ravi Sep 07 '13 at 05:37
-
if i apply this selection is made for only around the textview not for whole row, can u tell me how to achieve this for whole row – AndroidOptimist Sep 10 '13 at 06:15
-
it shouldnt happen that way but you can try a different approach instead of applying the selector file at listSelector , apply the file as a background to the parent view (Linear , relative etc) you are using for the layout file you provide in the custom adapter for each row – Ravi Sep 10 '13 at 07:08
-
i solved that issue it's because i assigned my layout width as wrap_content instead of fill_parent. Once i changed that my error got resolved. Thanks for ur help :-) – AndroidOptimist Sep 16 '13 at 09:26
-
Just add following detail in the listview in xml file
android:divider="#FF3366" android:dividerHeight="3dp"

- 3,250
- 6
- 34
- 57
Try this according to your code
viewHolder.imageview.setText(entry.getString("calculator"));
if(position % 2 == 0){
viewHolder.linearLayout.setBackgroundResource(R.color.grey);
}
else{
//viewHolder.linearLayout.setBackgroundResource(R.color.white);
}
It will change the color of row

- 9
- 1
set the view's background to a xml file. in resource drawable we can use xml files. check the Api demos drawable folder. that xml file contains this code.
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
android:drawable="@drawable/button_image_in_normal_state" />
<item android:state_pressed="true"
android:drawable="@drawable/button_image_in_pressed_state_with_border" />
</selector>
and assign this xml filename in view's background.
also refer this link

- 10,861
- 8
- 50
- 75