What state is a view in after it is clicked (i.e. after the click is released)?

104 Views Asked by At

I have a clickable ImageView with a background color, and what I want is that its background color is changed when it is touched/clicked and it should remain changed even after the click is released, until something else is clicked. For that I am writing a state-list drawable.

I am using the pressed state, which is while the view is being clicked. I also want to know what state is the ImageView in after the click is released (until something else is clicked).

I am talking about the following states given here:

  • state_pressed
  • state_focused
  • state_hovered
  • state_selected
  • state_checkable
  • state_checked
  • state_enabled
  • state_activated
  • state_window_focused
1

There are 1 best solutions below

1
On

This is example !!!

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), drawable);
    StateListDrawable states = new StateListDrawable();
    states.addState(new int[]{android.R.attr.state_pressed},
            ResourcesCompat.getDrawable(getResources(), R.drawable.image_pressed, null));
    states.addState(new int[]{android.R.attr.state_focused},
            ResourcesCompat.getDrawable(getResources(), R.drawable.focused, null));
    states.addState(new int[]{},
            new BitmapDrawable(getResources(), bitmap));
    imageView.setImageDrawable(states);