Search code examples
androidlistrounded-corners

android list item's child view with rounded corners


I am developing an Alarm Clock application. I want an activity to list all the existing alarms. I am using a custom layout for each row of the ListView. It should display the alarm's description, type and the time.

Here is the row layout: alarm_item_2.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="fill_parent"
          android:background="@drawable/selector">
    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/time_background"
        android:textColor="#ffffff"
        android:layout_marginTop="6dp"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:padding="6dp"
        android:textSize="36sp"
    />
    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@id/time"
        android:paddingTop="6dp"
        android:textStyle="bold"
        android:textSize="20sp"
        android:textColor="#000"
    />
    <TextView
        android:id="@+id/type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/time"
        android:layout_below="@id/description"
        android:textSize="18sp"
        android:textColor="#000"
    />
</RelativeLayout>

Also the layout for the activity: view_alarms.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="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:cacheColorHint="#fff"
    android:background="#fff"
    android:drawSelectorOnTop="true"
/>
</LinearLayout>

What I want is to make the view displaying the time be black and with round corners. I am using a shape resource: time_background_xml in res/drawable folder:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <solid android:color="#000000"/>
    <corners android:radius="10dp"/>
</shape>

However, the background of the button remains white. What is the problem with my code? Obviously, I'm missing something important.


Solution

  • I think the time_background_xml should be like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="rectangle">
                <solid android:color="#000000"/>    
                <corners android:radius="10dp"/>
            </shape>
        </item>    
    </layer-list>
    

    Take a look at this for more help.

    UPDATE:

    I think this will work:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
        <solid android:color="#000000"/>    
    
        <stroke android:width="3dp"
                android:color="#000000"/>
    
        <padding android:left="1dp"
                 android:top="1dp"
                 android:right="1dp"
                 android:bottom="1dp"/> 
    
        <corners android:radius="10dp"/> 
    </shape>
    

    Also, take a look at this for an alternative solution.