1

I am trying to change the image in an ImageView by accessing source of image in CheckBox. Here is part of my xml code

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/myimage"
    android:id="@+id/img"/>
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableRight="@drawable/image1"
     android:id="@+id/q4op1"/>
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableRight="@drawable/images2"
     android:id="@+id/q4op2"/>
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableRight="@drawable/images"
    android:id="@+id/q4op3"/>
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableRight="@drawable/download"
    android:id="@+id/q4op4"/>

I want to check whether the CheckBox is checked or not. if checked then the image in drawableRight of that CheckBox should appear in the ImageView.

Can you guide me how can i write java code for this?

Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60
arti
  • 31
  • 8

1 Answers1

2

I think this may solve your problem. Below code will get you all the drawables.

Drawable[] drawables = textView.getCompoundDrawables();

Now, if you want to compare then you can use

Bitmap bmp1 = ((BitmapDrawable)drawables[1] ).getBitmap();
Bitmap bmp2 = ((BitmapDrawable)getResources().getDrawable(R.drawable.twt_hover)).getBitmap();

if(bmp1 == bmp2)
{
   //Code block
}

OR

in your case

// Left(0), top(1), right(2), bottom(3) drawables.
// This is the right drawable.
Drawable rightCompoundDrawable = drawables[2];

For checking if the checkbox is checked

cb = (CheckBox) rowView.findViewById(R.id.q4op1); 
if(cb.isChecked()) {
  // do your drawable task here
}
Rahul Khurana
  • 8,577
  • 7
  • 33
  • 60