ImageView with StateListDrawable not working

928 Views Asked by At

This seems so simple, but it is just not working. The ImageView should change bitmaps when it is checked, but instead it always shows the unchecked image:

public void addImage()
{
  ImageView imageView = new ImageView(context);
  m_draw = new StateListDrawable();

  m_draw.addState(android.util.StateSet.WILD_CARD, m_bitmapUnchecked);
  m_draw.addState(new int[] { android.R.attr.state_checked }, m_bitmapChecked);

  imageView.setImageDrawable(m_draw);

  this.addView(imageView);
}

public void onClick(View v)
{
  m_draw.setState(new int[] { android.R.attr.state_checked });
}
1

There are 1 best solutions below

0
On

The first matching state is the one which will be set. You should put WILD_CARD as the last state.