0

So I have an Android application in which sliders are used to set the value for multiple parameters. I am using a "Google Cards" type of layout for each slider box. Each card is its own relative layout contained within a LinearLayout which is placed within a ScrollView. I would like to change the color of the card based on the value the user sets in the card, however, the problem I have is that the color for the background is defined in the XML for the card design.

How can I change the color in the XML for the card design based on the value the user puts in on the slider? Also, is there a way to have a gradient effect when changing colors. For example, 0 could be a strong red, while 10 is a strong green, 5 would be a yellow and then 2.5 would be a mix between red and yellow and 7.5 would be a mix between yellow and green.

XML for layout background:

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

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle"
        android:dither="true">
        <corners android:radius="2dp"/>
        <solid android:color="#ccc"/>
    </shape>
</item>

<item android:bottom="2dp">
    <shape android:shape="rectangle"
        android:dither="true">
        <corners android:radius="2dp"/>
        <solid android:color="@android:color/white"/>

        <padding android:bottom="8dp"
            android:left="8dp"
            android:right="8dp"
            android:top="8dp"/>
    </shape>
</item>
</layer-list>

Relative Layout XML:

<RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/bg_card"
                android:layout_marginBottom="4dp"
                android:layout_marginLeft="6dp"
                android:layout_marginRight="6dp"
                android:layout_marginTop="4dp"
                android:id="@+id/infoLayout">
</RelativeLayout>

What a Card looks like:

What A Card Looks Like

Tanishq dubey
  • 1,522
  • 7
  • 19
  • 42
  • http://stackoverflow.com/questions/4941606/changing-gradient-background-colors-on-android-at-runtime – Illegal Argument Jun 26 '14 at 03:20
  • That seems like it would work, however, would that work for each 'card' that I have even though they all reference the same background XML? Or should I make copies of the background XML for each card? – Tanishq dubey Jun 26 '14 at 12:41

0 Answers0