AndroidDev
AndroidDev

Reputation: 2647

Android Disable Multitouch on Views

I am facing multitouch issue. The problem is I can simultaneously touch two buttons on my screen.

I know this question is asked several times in this forum and the only solution is to declare android:splitMotionEvents="false" in your parent layout. But after declaring this the issue remains. Is it the problem with the hardware or is it with the code ? Any pointer here is appreciated.

Upvotes: 2

Views: 2853

Answers (2)

Clark Will Battle
Clark Will Battle

Reputation: 21

This is what worked for me. In addition to setting the android:splitMotionEvents="false" on every ViewGroup that contains the buttons I put this in MyAdapter.getView()...

    view.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View cell, MotionEvent event) {
            // Process touches only if: 1) We havent touched a cell, or 2) This event is on the touched cell
            if (mTouchedCell == null || cell.equals(mTouchedCell)) {
                int action = event.getAction();
                if (action == MotionEvent.ACTION_DOWN) {
                    cell.startAnimation(mShrink);
                    mTouchedCell = cell;
                } else if (action == MotionEvent.ACTION_CANCEL) {
                    if (cell.equals(mTouchedCell)) {
                        cell.startAnimation(mGrow);
                    }
                    mTouchedCell = null;
                    return true;
                } else if (action == MotionEvent.ACTION_UP) {
                    cell.startAnimation(mFadeOut);
                    mTouchedCell = null;
                }
                return false;
            }
            return true;
        }
    });

...and of course this in the adapter...

private static View mTouchedCell = null;

Upvotes: 0

DrMartens
DrMartens

Reputation: 451

This issue appears beacuse since android 4.0 each onClick performed in a new thread.

How i solved it:

//1. create your own click listener

    public abstract class AbstractCarOnClickListener {
        protected static volatile boolean processing = false;
        protected void executeBlock() {
            ActivityUtil.scheduleOnMainThread(new Runnable() {
                public void run() {
                    processing=false;
                }
            }, 400);
        }
    }

//2. create subclass of your listener

public abstract class AppButtonsOnClickListener extends AbstractCarOnClickListener implements View.OnClickListener {

    public void onClick(View v) {
        if(processing) return;
        try{
            processing=true;            
            onCarButtonClick(v);
        } finally {
            executeBlock();
        }
    }
    public abstract void onCarButtonClick(View v);
}

//3. set listener to your view

    public void onClick(View v) {
         clickListener.onClick(v);
    }

    public OnClickListener clickListener = new AppButtonsOnClickListener(){
        public void onCarButtonClick(View v) {
            hintContainer.setVisibility(View.GONE);
            if (v == cancelButton) {
                listener.onCancelButtonClicked();
            }
        }
    }

Upvotes: 1

Related Questions