How to Create a Listview with Rounded Corners in Android

How do I create a ListView with rounded corners in Android?

Here is one way of doing it (Thanks to Android Documentation though!):

Add the following into a file (say customshape.xml) and then place it in (res/drawable/customshape.xml)

<?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="270"/>

<corners
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp"/>
</shape>

Once you are done with creating this file, just set the background in one of the following ways:

Through Code:
listView.setBackgroundResource(R.drawable.customshape);

Through XML, just add the following attribute to the container (ex: LinearLayout or to any fields):

android:background="@drawable/customshape"

Hope someone finds it useful...

How to Create Rounded Corner Shape ListView

As per your requirement

create a shape file in your drawable folder .Put this xml in it and change the color as per your requirement .

 <?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/shape_my">
<stroke android:width="4dp" android:color="#636161" />
<padding android:left="5dp"
android:top="5dp"
android:right="5dp"
android:bottom="5dp" />
<corners android:radius="24dp" />
<solid android:color="#FFF" />
</shape>

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />

</LinearLayout>

In the ListView set the background android:background="@drawable/shape"

list_row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >

<TextView
android:id="@+id/category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Category"
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />

<TextView
android:id="@+id/expiry_date"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/category"
android:layout_marginTop="1dip"
android:text="Text here."
android:textColor="#343434"
android:textSize="10dip" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/arrow" />

</RelativeLayout>

Listview items with rounded corners

set the background not for the listview but the custom layout that you are using to display listview row

android:background="@drawable/customshapeduplicate"

How to round corners of list item at the top and bottom of the list view? (not only for top and bottom items)

This solution is not pretty but it does what you want. The idea is to invert the corner and draw them as foreground :

the ListView is wrapped in a FrameLayout :

<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="10dp"
android:foreground="@drawable/inverted_corners"
android:layout_weight="1">
<ListView
android:id="@+id/listView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:listSelector="@drawable/list_selector" />
</FrameLayout>

which foreground (drawable/inverted_corners.xml) is set to a rounded hole, drawn with the background color. The trick is to draw a line outside the shape, instead of a filled shape :

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="-11dp"
android:left="-11dp"
android:right="-11dp"
android:top="-11dp">
<shape android:shape="rectangle">
<stroke
android:width="10dp"
android:color="#cacaca" />
<corners android:radius="22dp" />
</shape>
</item>
</layer-list>

This garanties that the rounded corners are on top of the selector, and the overscroll effect.

ListView rows with rounded corners

For the colored circle- I'd suggest just a text view set to the proper size with a circle drawable set as the background of the view. Should get what you want.

For the background- I'd just stick the entire row in either a linear or relative layout, then set a RoundedBitmapDrawable as the background of the layout. That will give you the rounded background effect. If necessary add some margin to the top and bottom of each view to increase the gap between items.

Images with rounded corners in a ListView

Try this way: create rounded_corner.xml file into drawable\rounded_corner.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<solid android:color="#101010" />

<stroke
android:width="1dp"
android:color="#808080" />

<corners android:radius="15dp" />

And set as Background to your ImageView like

android:background="@drawable\rounded_corner"

And also set your RSS image to ImageView as a Src or a Bitmap like:

 imageview.setBitmap(yourrssimage);

Creating listview with rounded corner shows partial corners

What I can see from the image you post in your question.

Your ListView is overlaping your border somehow.
Try giving the padding to your listview.

<ListView
android:id="@+id/contact_us_list_view"
android:layout_width="wrap_content"
android:divider="@null"
android:dividerHeight="0dp"
android:padding="10dp" // OverHere
android:layout_height="wrap_content"/>

what else i can see in your layout is that you are using "sp" instead of "dp"
in many places.



Related Topics



Leave a reply



Submit