abh22ishek
abh22ishek

Reputation: 2671

Listview items with rounded corners

I am trying to make listview items rectangular shape with rounded corners but what's happening with me, only first row comes with rounded corners on upper part, rest of the child listview items comes in rectangular shapes with no rounded corners. I don't know why this is happening.

Here is my listview codes:

<ListView
    android:id="@+id/listViewdoctorwithappointment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="7dp"
    android:layout_marginRight="7dp"
    android:layout_marginTop="10dp"
    android:minHeight="80dp"
    android:background="@drawable/customshapeduplicate"
    android:cacheColorHint="@android:color/transparent"
    android:divider="#4F94CD"
    android:dividerHeight="10dp"

    android:smoothScrollbar="true"

    android:listSelector="@drawable/selector"
    android:scrollbars="none" >
</ListView>

And Here is my customshapeduplicatefile is this:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <gradient android:startColor="#ffffff" android:endColor="#ffffff" 
        android:angle="270"/> 

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

And this is the image:

enter image description here

Upvotes: 8

Views: 12660

Answers (3)

Raghunandan
Raghunandan

Reputation: 133560

I suggest you use a custom list adapter. Modify the below code according to your requirements.

 public View getView(final int arg0, View arg1, ViewGroup arg2) {
    final ViewHolder vh;
    vh= new ViewHolder();

    if(arg1==null )
    {
                    arg1=mInflater.inflate(R.layout.lyourcustomlayouttobe inflated, arg2,false);
            arg1.setTag(vh);
            }

    return arg1;
}

Your custom layout

 <?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="horizontal"
 android:cacheColorHint="#000000"
 android:background="@drawable/listviewbkg">
 //other items to be inlfated.
 </LinearLayout>

Create a drawable folder under resources. post the below xml as listviewbkg

 <?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true" 
    android:drawable="@drawable/pressed" />
 <item  android:state_focused="false" 
    android:drawable="@drawable/normal" />
 </selector>

Shape when normal under drawable with name normal.xml

 <?xml version="1.0" encoding="UTF-8"?> 
 <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
 <solid android:color="#FFFFFF"/>    
 <stroke android:width="3dp"
        android:color="#0FECFF" /><!-- #330000FF #ffffffff -->
 <gradient                               // remove the gradient if do not wish to use.
    android:startColor="#ffffffff" 
    android:endColor="#110000FF" 
    android:angle="90"/> 

 <padding android:left="5dp"
         android:top="5dp"
         android:right="5dp"
         android:bottom="5dp"/> 
 <corners android:bottomRightRadius="7dp"      // change this to increase the rounded edge radius
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp"
         android:topRightRadius="7dp"/> 
 </shape>

Shape when pressed under the name pressed.xml in drawable folder

  <?xml version="1.0" encoding="UTF-8"?> 
  <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
  <solid android:color="#FF1A47"/>    
  <stroke android:width="3dp"
        android:color="#0FECFF"/>
  <padding android:left="5dp"
         android:top="5dp"
         android:right="5dp"
         android:bottom="5dp"/> 
  <corners android:bottomRightRadius="7dp"
         android:bottomLeftRadius="7dp" 
         android:topLeftRadius="7dp"
         android:topRightRadius="7dp"/> 
  </shape>

Upvotes: 3

KDeogharkar
KDeogharkar

Reputation: 10959

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

android:background="@drawable/customshapeduplicate"

Upvotes: 3

Jave
Jave

Reputation: 31846

That is because the background-attribute is the background of the entire list, not the individual rows.

To achieve what you want, either set a custom background through your Adapter or list-item layout, or look at this answer.

Upvotes: 0

Related Questions