4

I have this xml where I already set stroke color but I sometimes I want to change stoke color programmatically. The file name is dummy.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffffff" />

    <stroke
        android:width="1dp"
        android:color="#ff000000" />

    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />

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

I want to change color (stroke color) programmatically, how I can do this?

I use this xml here :

  <LinearLayout
                android:layout_marginTop="2dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/a"
                android:layout_gravity="center"
                android:background="@drawable/dummy"
                android:layout_marginBottom="2sp"
                android:orientation="horizontal"
                android:padding="5dp">

                <RelativeLayout
                    android:id="@+id/serverStatusWrapper"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="2dp"
                    android:background="@drawable/shadow_green"
                    android:padding="2dp">

                    <ImageView
                        android:id="@+id/serverStatus"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:antialias="true"
                        android:src="@drawable/ic_settings_input_antenna_white_24dp" />

                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/locationStatusWrapper"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/shadow_red"
                    android:padding="2dp">

                    <ImageView
                        android:id="@+id/locationStatus"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:antialias="true"
                        android:src="@drawable/ic_warning_white_24dp" />
                </RelativeLayout>

            </LinearLayout>
1
  • You need to create your drawable dynamically i have try this but not work for me Commented May 16, 2017 at 6:56

1 Answer 1

3
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.a);

You can change the stroke as

GradientDrawable drawable = (GradientDrawable)linearLayout.getBackground();
drawable.setStroke(3, Color.RED);

And can change solid color as

GradientDrawable drawable = (GradientDrawable)linearLayout.getBackground();
drawable.setColor(Color.RED);

Hope this helps

Sign up to request clarification or add additional context in comments.

6 Comments

Ok but this xml I use in LinearLayout
then change imageView to linearlayout
I try do this but I have nullpointer : GradientDrawable drawable = (GradientDrawable)linearLayout.getBackground();
get the reference for linearlayout, let me edit the answerr
@KrzysztofPokrywka i have edited my answer, hope it will work, dont forget to accept the answer and rate it up, if it worked :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.