Bala Sivagnanam
Bala Sivagnanam

Reputation: 863

creating colored stripes in android ListView

I am trying to create thin colored stripes in my app. This is similar to stripes of different color that we see in calendar apps.

Is there any easy way to do it.

What I am doing: I have a shape defined with width and height and I am trying to apply it to background of my ListView as it will occupy certain part of the ListView.

What is happening: This is getting applied to the whole background.

What I need: I am looking for help which can make this as stripes instead of complete background. or any other way or alternatives for doing the same.

Some code: rectangle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners 
        android:radius="0dp"
        />
    <size 
        android:width="20dp"
        android:height="40dp"       />
    <solid 
        android:color="@color/opaque_red"
        />
</shape>

list_view_item_cell.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="95dp"
    android:divider="?android:dividerVertical"

    android:showDividers="middle" >

Posting a picture to give more clarity:

Upvotes: 0

Views: 1556

Answers (2)

Mahesh
Mahesh

Reputation: 361

Yes changing the colour of the shape is possible programatically.

Assume if you have a LayerDrawable set to the view background: Then this is something which you can do:

LayerDrawable layerList = (LayerDrawable) view.getBackground();
    if(layerList != null) {
        final RotateDrawable shape = (RotateDrawable) layerList
                .findDrawableByLayerId(R.id.header_titled);
        final GradientDrawable tildShapeDrawable = (GradientDrawable)shape.getDrawable();
        if (tildShapeDrawable != null) {
            tildShapeDrawable.setColor(android.R.color.white);
        }
    }

Upvotes: 2

srs
srs

Reputation: 647

You can add a View of the width and height equal to that certain part where you want to have the colored background. For example, if you want to show colored strip of width and height = 20dip. And this colored strip should appear at the left top of List item. Below is the sample

list_view_item_cell.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="95dp"
        android:divider="?android:dividerVertical"
        android:showDividers="middle" >
<!-- You can use any view here. Set background "rectangle.xml" drawable to this view.-->
    <View  
        android:layout_width="20dip" 
        android:layout_height="20dip"
        android:background="@drawable/rectangle"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        />

    </RelativeLayout>

Upvotes: 4

Related Questions