John E
John E

Reputation: 3

Getting Background Color of textView (I have no idea whats going on)

So i have this code and i'm trying to get current background color of the textView that has been clicked on.

I get the following error:

java.lang.ClassCastException: android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.ColorDrawable

private final class ChoiceTouchListener implements OnTouchListener {
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {

            TextView textViewTouched = (TextView) view;

***error at this line***  ColorDrawable textViewBackground = (ColorDrawable)textViewTouched.getBackground();


            //change color of textView quickly so that the shadow dragged object made down below
            //will have a different colour then textView
            view.setBackgroundColor(0xfff00000);

            //setup drag
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            //start dragging the item touched
            view.startDrag(data, shadowBuilder, view, 0);

            //change the textView colour back so the user doesn't see a change so that the
            //shadow object is a different colour then the textView
            view.setBackgroundColor(0xf32cd320); 
            return true;
        }
        else {
            return false;
        }
    }
}

But if I move

ColorDrawable textViewBackground = (ColorDrawable)textViewTouched.getBackground(); 

To below

view.setBackgroundColor(0xfff00000); 

It works just fine.

I have no idea why its doing what its doing but I need to get the background color before I apply the new color.

edit

I should also mention i have this in a function lower down in my code and it works just fine in that function.

More Edit

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/DisplayConfig"
    android:keepScreenOn="true">


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="18.5dp"
        android:gravity="center"
        android:paddingBottom="0dp"
        android:text="@string/intro" />

    <TextView
        android:id="@+id/option_1"
        android:layout_width="fill_parent"
        android:layout_margin="0dp"
        android:background="@drawable/drag_drop_option"
        android:gravity="left"
        android:text="@string/option_1"
        android:textStyle="normal"
        android:textColor="#FFFFFF"
        android:layout_height="150dp" />

    <TextView
        android:id="@+id/option_2"
        android:layout_width="fill_parent"
        android:layout_margin="0dp"
        android:background="@drawable/drag_drop_option"
        android:gravity="left"
        android:text="@string/option_2"
        android:textColor="#FFFFFF"
        android:textStyle="normal"
        android:layout_height="150dp" />

    <TextView
        android:id="@+id/option_3"
        android:layout_width="fill_parent"
        android:layout_margin="0dp"
        android:background="@drawable/drag_drop_option"
        android:gravity="left"
        android:text="@string/option_3"
        android:textColor="#FFFFFF"
        android:textStyle="normal"
        android:layout_height="151dp" />


    </LinearLayout>

    <LinearLayout android:id="@+id/bottom_button_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center_horizontal"
    android:background="#000000"
    android:orientation="horizontal">

    <Button android:id="@+id/occupancy_button"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="72dp"
        android:layout_weight="1"
        android:text="Room Mode"
        android:textColor="#FFFFFF"/>

    </LinearLayout>
</FrameLayout>

and setting of the view for the listener is done like this.

option1.setOnTouchListener(new ChoiceTouchListener());

Upvotes: 0

Views: 1317

Answers (1)

snachmsm
snachmsm

Reputation: 19253

Maybe you don't have ColorDrawable but another type, so you are getting ClassCast?

Look HERE, Drawable have a lot of subclasses

Known Direct Subclasses

AnimatedVectorDrawable, AnimatedVectorDrawableCompat, BitmapDrawable, ColorDrawable, DrawableContainer, DrawableWrapper, DrawerArrowDrawable, GradientDrawable, LayerDrawable, NinePatchDrawable, PictureDrawable, RoundedBitmapDrawable, ShapeDrawable, VectorDrawable, VectorDrawableCompat

Known Indirect Subclasses

AnimatedStateListDrawable, AnimationDrawable, ClipDrawable, InsetDrawable, LevelListDrawable, PaintDrawable, RippleDrawable, RotateDrawable, ScaleDrawable, StateListDrawable, TransitionDrawable

edit:

getBackground() method returns Drawable and you are immediatelly casting this to (ColorDrawable). stacktrace says that this is not ColorDrawable, but GradientDrawable, another subclass of Drawable

setBackgroundColor(int color) is creating ColorDrawable for background, so when you move below your line with casting to ColorDrawable then is working

it may don't throw any errors for other views, because these might have set background color earlier in another way

edit2:

you may check with instanceOf, but this is not an efficient and optimal way to resolve your problem...

if( ! textViewTouched.getBackground() instanceOf ColorDrawable)
    textViewTouched.setBackgroundColor(Color.parseColor("#32cd32"));
ColorDrawable textViewBackground = (ColorDrawable)textViewTouched.getBackground();

Upvotes: 1

Related Questions