0

I have a drawable called "XYZ"

<item android:state_pressed="false">
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="@color/highlightpen_blue" />

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

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

in a button android:background="@drawable/XYZ"

i just want to create XYZ in code purely to change @color/highlightpen_blue

2 Answers 2

1

Like Fahim's answer,but some difference.

StateListDrawable states = (StateListDrawable) btn.getBackground();
GradientDrawable bgShape = (GradientDrawable) states.getCurrent();
bgShape.setColor(Color.BLACK);
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it with the of GradientDrawable

GradientDrawable background = (GradientDrawable) titleTextView.getBackground();
 background.setColor(getResources().getColor(R.color.some_color))

2 Comments

thanks but it should not work because the Background is not a GradientDrawable, but Selector. I actually want the button with some solid color with round corner
@DayDayHappy this will change the color of <shape> tag in the xml at runtime and it will keep the round corner, what you are doing in the xml

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.