user586679
user586679

Reputation: 1

Button state_pressed/state_focused to make an ImageView VISIBLE

my activity has a Button and an ImageView, originally set to invisible with android:visibility. I'd like to change ImageView's state to visible on button focus.

Right now I can get this done with imageview.setVisibility(View.VISIBLE); inside the onClick() listener of the Button. The problem is the visibility change comes after the pressed/focused states of the button.

Basically I'd like to tie the ImageView to the Button like so: 1) when the button is pressed/focused (orange highlight) the imageview becomes visible. 2) when the user lets go of the button, the imageview becomes invisible again. Sounds like I need to create a custom button and override some methods but I'm not sure what. Thanks for any tips

Upvotes: 0

Views: 1123

Answers (1)

f20k
f20k

Reputation: 3106

Try overriding onTouchEvent:

@override

public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN) {
            // show image
        } else if(event.getAction() == MotionEvent.ACTION_UP) {
           // hide image
        }

Upvotes: 1

Related Questions