Jon Vance
Jon Vance

Reputation: 504

ImageView with StateListDrawable not working

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 });
}

Upvotes: 2

Views: 922

Answers (1)

Rafael Nobre
Rafael Nobre

Reputation: 5131

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

Upvotes: 3

Related Questions