Issue
Each button represents something clickable, which has a color stored in the database that a user can configure. The background of the button is the <shape>
that follows.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="1dp" android:color="#000" />
<corners android:radius="5dp" />
<gradient android:startColor="#FFF"
android:centerColor="#FFF"
android:endColor="#666"
android:angle="315" />
<padding android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp" />
</shape>
I'd like to get at the <gradient>
element — specifically the startColor
centerColor
and endColor
attributes. I can do this:
button.getBackground().setTint(theColor);
But that seems to wipe out the stroke and gradient.
Is this possible? Is there a better way?
Solution
try below snippet, give it a try
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{ContextCompat.getColor(this,R.color.white), ContextCompat.getColor(this,R.color.red_500), ContextCompat.getColor(this,R.color.blue_500)});
gd.setShape(GradientDrawable.RECTANGLE);
gd.setStroke(1, ContextCompat.getColor(this, R.color.black));
gd.setCornerRadius(5f);
gd.setBounds(2, 2, 2, 2);
findViewById(R.id.btn_analytics).setBackground(gd);
Answered By - Bhavnik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.