How to programmatically set the stroke color of a shape used on a home screen widget

61 views Asked by At

So, I have this rounded_corner_shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke
        android:width="1dp"
        android:color="TO_BE_SET_DYNAMICALLY" />

    <corners android:radius="8dp" />
</shape>

used in ip_properties.xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/ip_property_tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/rounded_corner_shape" />
</LinearLayout>

I then want to dynamically set the stroke color from a RemoteViewsService.RemoteViewsFactory implementation, like so:

class WifiPropertyViewsFactory: RemoteViewsService.RemoteViewsFactory{
    // other overrides...

    override fun getViewAt(position: Int): RemoteViews =
       RemoteViews(context.packageName, R.layout.ip_properties)
                .apply {
                    setTextViewText(R.id.ip_property_tv_1, "some data")
                    // set background rounded corner shape stroke color
                }

}

Due to not knowing any better, I tried to set it via RemoteViews.setInt(R.id.ip_property_tv_1, "setBackgroundTint", colorInt), but to no avail. Does somebody know how to make it work?

1

There are 1 answers

0
Rahul Rokade On

Try this code - text view change background stroke color programmatically Used this code in setOnClickListener() method

edtSelectState.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            GradientDrawable gradientDrawable = new GradientDrawable();
            gradientDrawable.setStroke(2, Color.RED);

            edtSelectState.setBackground(gradientDrawable);
        }
    });