views:

4459

answers:

2

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
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
Thanks for the fix... But I don't know why that could be happening.. Did you find any specific reason?
Legend
I love you for this!
teedyay
@teedyay: Anytime pal :)
Legend
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
android:angle="250" is causing application to crash, changed to 45 and worked fine.
youssef azari
@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
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
A: 

DanyW , customshape.xml work fine for me , I just changed the 'angle' attribute to 270 (like allclaws said)

Zohar Adar
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