How to get the color from a GradientDrawable?

0
0

I have an EditText with the background defined in a 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 the color that the view has:

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 the GradientDrawable.

  • You must to post comments
0
0

You can reference GradientDrawable.java then make some proper modification to make it work~

import android.content.res.Resources;
...
// it's 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's how I get the color from this drawable class
public int getColor() {
return mColor;
}
...
// it's the same with GradientState, just make some proper modification to make it compilable
final public static class GradientState extends ConstantState {
...
}
}
  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website