How do I get the color from a GradientDrawable?
I have EditText
with a background defined in the xml file:
<EditText
android:id="@+id/myEditText"
style="@style/Theme.x.EditText.ReadOnly"
android:layout_marginRight="5dip"
android:layout_span="7"
android:nextFocusDown="@+id/myEditText2"
android:selectAllOnFocus="true" />
xml file my_edit_text.xml
::
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="@android:color/background_dark" android:endColor="@android:color/background_dark"
android:angle="0"/>
<solid android:color="#FA0000"/>
<stroke android:width="1px" android:color="#5A5D5A"/>
<corners
android:bottomLeftRadius="3dip"
android:topLeftRadius="3dip"
android:bottomRightRadius="3dip"
android:topRightRadius="3dip"
android:color="@color/crAmarelo"/>
<padding
android:left="5dip"
android:right="5dip"/>
</shape>
And I need to get a color that looks like:
EditText et = (EditText) solo.getView(R.id.myEditText);
GradientDrawable gd = (GradientDrawable) et.getBackground();
But I don't know how to get the solid color from GradientDrawable
.
+3
source to share
1 answer
You can reference GradientDrawable.java then make the correct modification to make it work ~
import android.content.res.Resources;
...
// it the same with the GradientDrawable, just make some proper modification to make it compilable
public class ColorGradientDrawable extends Drawable {
...
private int mColor; // this is the color which you try to get
...
// original setColor function with little modification
public void setColor(int argb) {
mColor = argb;
mGradientState.setSolidColor(argb);
mFillPaint.setColor(argb);
invalidateSelf();
}
// that how I get the color from this drawable class
public int getColor() {
return mColor;
}
...
// it the same with GradientState, just make some proper modification to make it compilable
final public static class GradientState extends ConstantState {
...
}
}
+2
source to share