DayDayHappy
DayDayHappy

Reputation: 1679

Create Android Selector in code

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

Upvotes: 0

Views: 148

Answers (2)

cruelcage
cruelcage

Reputation: 2094

Like Fahim's answer,but some difference.

StateListDrawable states = (StateListDrawable) btn.getBackground();
GradientDrawable bgShape = (GradientDrawable) states.getCurrent();
bgShape.setColor(Color.BLACK);

Upvotes: 1

Fahim
Fahim

Reputation: 12358

You can do it with the of GradientDrawable

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

Upvotes: 0

Related Questions