Android On Focus Listener and On Click Listener on ImageView

Anukool picture Anukool · Feb 12, 2013 · Viewed 14.7k times · Source

I have an imageview - It has both the attributes -focusable and focusableintouchmode set to true

<ImageView
        android:id="@+id/ivMenu01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:focusable="true"
        android:focusableInTouchMode="true" >
    </ImageView>

I have implemented the onFocusChangeListener in my activity-


 @Override
public void onFocusChange(View v, boolean hasFocus) {
    switch (v.getId()) {
    case R.id.ivMenu01:

            if (hasFocus) {
                ivMenu01.setImageBitmap(Utility
                        .getBitmap("Home_ford_focus.png")); // Focussed image
            } else {
                ivMenu01.setImageBitmap(Utility.getBitmap("Home_ford.png")); // Normal image
            }

        break;

    default:
        break;
    }

}

Also the onClickListener -

 case R.id.ivMenu01:
                ivMenu01.requestFocus();
                Intent iFord = new Intent(HomeScreen.this, FordHome.class);
                startActivity(iFord);

break;

Now when i click the ImageView the first click gives the focus to the ImageView and the second click performs the action. I am not sure why this is happening .
The first click should request focus as well as perform the action.
Any help on how to do this will be highly appreciated.

Answer

andr picture andr · Feb 12, 2013

It's the way the widget framework is designed.

When you look at View.onTouchEvent() code, you'll find out that the click action is performed only if the view has taken focus:

    // take focus if we don't have it already and we should in
    // touch mode.
    boolean focusTaken = false;
    if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
        focusTaken = requestFocus();
    }

    if (!mHasPerformedLongPress) {
        // This is a tap, so remove the longpress check
        removeLongPressCallback();

        // Only perform take click actions if we were in the pressed state
        if (!focusTaken) {
            // click
        }
    }

So, as you noticed, the first click makes the view gain focus. The second one will trigger the click handler since the view already has focus.

If you want to alter the bitmap of the ImageView when it's pressed, you should implement an View.OnTouchListener and set it via ImageView.setOnTouchListener() method. That listener should look more or less like this:

private View.OnTouchListener imageTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            // pointer goes down
            ivMenu01.setImageBitmap(Utility.getBitmap("Home_ford_focus.png"));
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            // pointer goes up
            ivMenu01.setImageBitmap(Utility.getBitmap("Home_ford.png"));
        }
        // also let the framework process the event
        return false;
    }
};

You can also use a Selector aka State List Drawable to achieve the same thing. See reference here: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList