I have the following selector for a button (with 2 states, regular and pressed):
StateListDrawable gradientDrawable = (StateListDrawable) inflatedView.getBackground();
DrawableContainerState drawableContainerState = (DrawableContainerState) gradientDrawable.getConstantState();
Drawable[] children = drawableContainerState.getChildren();
LayerDrawable selectedItem = (LayerDrawable) children[0];
LayerDrawable unselectedItem = (LayerDrawable) children[1];
GradientDrawable selectedDrawable = (GradientDrawable) selectedItem.getDrawable(0);
GradientDrawable unselectedDrawable = (GradientDrawable) unselectedItem.getDrawable(0);
selectedDrawable.setStroke(STORKE_SIZE, NOTIFICATION_COLOR);
unselectedDrawable.setStroke(STORKE_SIZE, NOTIFICATION_COLOR);
I used this to change the stroke, it can be helpfull.
I have below selector round_circle_blue.xml shape xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<size android:width="@dimen/_50sdp" android:height="@dimen/_50sdp" />
<solid android:color="@color/color_blue" />
<stroke android:width="@dimen/_2sdp" android:color="@color/white" />
</shape>
</item>
</selector>
And I have set it as background in text view as below
<TextView
android:id="@+id/tvActivityicon"
android:layout_width="@dimen/_40sdp"
android:layout_height="@dimen/_40sdp"
android:textColor="@color/white"
android:gravity="center"
android:text="A"
android:textSize="@dimen/_14sdp"
android:background="@drawable/round_circle_blue" />
and in kotlin file i have use below code to change the color.
val gradientDrawable = tvActivityicon.background as StateListDrawable
val drawableContainerState = gradientDrawable.constantState as DrawableContainer.DrawableContainerState
val children = drawableContainerState!!.children
val selectedItem = children[0] as GradientDrawable
selectedItem.setColor(Color.parseColor("#FD00DF"))