4

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.

Daniane
  • 41
  • 1
  • 3
  • A gradient is an interpolation between colors -- not a single color. It doesn't make sense to try to get the color of the View overall (if it's a gradient) unless you're specifying a particular pixel of the view. If you're talking about `solid` color, that's not a `GradientDrawable` -- it's a property of the `ShapeDrawable` you've defined by the `shape` tag. – Kevin Coppock Sep 12 '14 at 21:40
  • @kcoppock yeah, i'm talking about `solid` color. When i try this: `EditText et = (EditText) solo.getView(R.id.myEditText); ShapeDrawable sd = (ShapeDrawable) et.getBackground();` i have a error `java.lang.ClassCastException: android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.ShapeDrawable` – Daniane Sep 15 '14 at 12:52

0 Answers0