0

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);
Kalariya_M
  • 1,357
  • 12
  • 20
Almis
  • 3,684
  • 2
  • 28
  • 58

1 Answers1

0

Try this

  • add this onClick of button as programmatic
  • add this xml in drawable folder

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

<stroke android:width="2dp"
    android:color="@color/colorwhite" />

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

<corners android:bottomRightRadius="@dimen/_30dp"
    android:bottomLeftRadius="@dimen/_30dp"
    android:topLeftRadius="@dimen/_30dp"
    android:topRightRadius="@dimen/_30dp"/>
 </shape>

       

//here we set background 
 btn.setBackgroundResource(R.drawable.btn_border);

  • Here's the OUTPUT of program

enter link description here

Community
  • 1
  • 1
Vishal Yadav
  • 1,020
  • 4
  • 15
  • 30