I trying to change the border color of the button when it's in pressed state programmatically. The reason that I'm doing this is because I made the dynamic theme. Below you can see all my research but it doesn't really help me.
I found how to change it using XML from this example Change border color when edittext is focused
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true"
android:state_pressed="true">
<shape android:padding="10dp" android:shape="rectangle">
<solid android:color="#FFFFFF" />
<stroke android:width="2dp" android:color="#ffa0a496" />
<corners android:bottomLeftRadius="15dp" android:bottomRightRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp" />
</shape>
</item>
<item android:state_enabled="true">
<shape android:padding="10dp"
android:shape="rectangle">
<solid android:color="#FFFFFF" />
<corners android:bottomLeftRadius="15dp" android:bottomRightRadius="15dp" android:topLeftRadius="15dp" android:topRightRadius="15dp" />
</shape>
</item>
Below code changes the background color based on the button which I got from here Android create selector programmatically
public static StateListDrawable makeSelector(int color) {
StateListDrawable res = new StateListDrawable();
res.setExitFadeDuration(400);
res.setAlpha(45);
res.addState(new int[]{android.R.attr.state_pressed}, new ColorDrawable(color));
res.addState(new int[]{}, new ColorDrawable(Color.TRANSPARENT));
return res;
}
view.setBackground(makeSelector(Color.RED));
And this code changes the border color programmatically but without specifying the state which I got from here Android Button or TextView Border programmatically without using setBackgroundDrawable method
GradientDrawable gd = new GradientDrawable();
gd.setColor(0xFF00FF00); // Changes this drawbale to use a single color instead of a gradient
gd.setCornerRadius(5);
gd.setStroke(1, 0xFF000000);
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setBackgroundDrawable(gd);