programmatically change the solid color in xml

ermaIsabelermaIsabel USMember
edited January 2014 in Xamarin.Android

Hi everyone,

I am using Tabhost. I have 4 tabs (please see the attached image). I got the pattern working. But with same background color. I need to change the background color programmatically.

Here is my code:

selectedTab.xml

        <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:top="10dp">
            <shape android:shape="rectangle">
                <solid android:color="#333333" />
            </shape>
        </item>
        <item android:top="12dp" android:left="1dp" android:right="1dp"
            android:bottom="0dp">
            <shape android:shape="rectangle">
                <solid android:color="#8ACAE1" /> // I need to change this color programmatically.
            </shape>
        </item>
    </layer-list>

selector.xml

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/selectedTab"  // Here the above selectedTab.xml is called
              android:state_selected="true"/>
        <item android:drawable="@drawable/unselectedTab"/>
    </selector>

Tabstyle.axml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:background="@drawable/selector" // here the above selector.xml file is called
        android:layout_height="fill_parent">
        <ImageView
            android:src="@drawable/icon"
            android:layout_width="100dp"
            android:layout_height="fill_parent"
            android:id="@+id/tabIcon" />
    </LinearLayout>

I tried the following code to change the solid color in main.cs

            View tab = LayoutInflater.Inflate (Resource.Layout.TabStyle, null);    

            GradientDrawable bgShape = (GradientDrawable)tab.Background;
    bgShape.SetColor (Android.Graphics.Color.ParseColor(colorcode));

But it throws cast exception. And i am not sure about the correctness of the code.

How will i be able to change the color of the shape defined in the xml file?

Please help,
Thanks.

t.png 6.3K

Posts

  • MihaMarkicMihaMarkic SI ✭✭✭✭

    One of your casts is wrong. I'd say this one

    (GradientDrawable)tab.Background;

    Try looking at what type it is through debugging or something for beginning.

  • HarshadcseHarshadcse USMember ✭✭✭

    @ermaIsabel‌ Same my isssue but resolve it.

    **Change yout code here **

    selectedTab.xml

            <?xml version="1.0" encoding="utf-8"?>
        <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
            <item android:top="10dp">
                <shape android:shape="rectangle">
                    <solid android:color="#333333" />
                </shape>
            </item>
            <item android:id="@+id/shape_id" android:top="12dp" android:left="1dp" android:right="1dp"
                android:bottom="0dp">
                <shape android:shape="rectangle">
                    <solid android:color="#8ACAE1" /> // I need to change this color programmatically.
                </shape>
            </item>
        </layer-list>
    

    following this code in activity :

    LayerDrawable bgDrawable = (LayerDrawable)tab.Background;
    GradientDrawable bgShape = (GradientDrawable)bgDrawable.FindDrawableByLayerId (Resource.Id.shape_id);
    bgShape.SetColor (Color.Rgb(r3,g3,b3));
    

    working code here....

  • BrianKilbyBrianKilby USMember ✭✭

    Are you guys still here? I need to talk...

Sign In or Register to comment.