I was wondering if there was a way to create a ListView with rounded corners in Android...
+29
A:
Here's one way of doing it (Thanks to Android Documentation though!):
Add the following into a file (say customshape.xml) and then place it in the layout directory inside the resources folder.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#SomeGradientBeginColor" android:endColor="#SomeGradientEndColor"
android:angle="250"/>
<corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
</shape>
Thought someone else might be interested in it. Once you are done with creating this file, just set the background in one of the following ways:
Through Code: listView.setBackgroundResource(R.layout.roundedcorner);
Through XML, just add the following attribute to the container (ex: LinearLayout):
android:background="@layout/customshape"
Hope someone finds it useful...
Legend
2009-11-05 20:05:02
Thanks for the great tip. Just FYI, copy-pasting gave me a runtime exception saying, "XmlPullParserException: Binary XML file line #4<gradient> tag requires 'angle' attribute to be a multiple of 45".. Easily remedied by changing the angle to 270.
allclaws
2010-01-25 16:25:08
Thanks for the fix... But I don't know why that could be happening.. Did you find any specific reason?
Legend
2010-01-26 17:21:07
I love you for this!
teedyay
2010-03-30 17:18:21
@teedyay: Anytime pal :)
Legend
2010-04-24 02:56:48
Thanks for the tip! Just one problem though - I have created a roundedrectangle.xml but eclipse code completion was not available in that file. It works just fine on the main.xml layout file. I kinda rely on code completion to learn and experiment. Any ideas why it's not working in my new file?Thanks, D.
DanyW
2010-06-06 10:21:09
android:angle="250" is causing application to crash, changed to 45 and worked fine.
youssef azari
2010-09-01 18:02:04
@youssef: I am not sure why it is happening. It worked fine for me in 5 different instances. Do you have the log from adb by any chance?
Legend
2010-09-01 18:23:49
same as allclaws, angle should be multiple of 45 : "XmlPullParserException: Binary XML file line #4<gradient> tag requires 'angle' attribute to be a multiple of 45"
youssef azari
2010-09-02 09:27:31
A:
DanyW , customshape.xml work fine for me , I just changed the 'angle' attribute to 270 (like allclaws said)
Zohar Adar
2010-07-06 13:20:33
Zohar, once yout get a little bit more reputation you can add commments to answers and questions. You posted this comment as answer this is wrong on stackoverflow that does not use the thread like discussion board model
Janusz
2010-07-06 13:24:01