onTouchListener togglebutton, ignores first press?

Posted by Paul on Stack Overflow See other posts from Stack Overflow or by Paul
Published on 2013-10-25T13:39:17Z Indexed on 2013/11/04 21:54 UTC
Read the original article Hit count: 285

Filed under:

I have a togglebutton that should run code when I press it down and more code when I let go. However the first time I press and let go nothing happens. Every other time it is fine, why is this? I can see the method only runs the first time when I let go of the button (it does not trigger any onTouch part of the method though), how can I get around this, and have it work for the first press?

public void pushtotalk3(final View view) {
    ((ToggleButton) view).setChecked(true);
    ((ToggleButton) view).setChecked(false);
    view.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //if more than one call, change this code
            int callId = 0;
            for (SipCallSession callInfo : callsInfo) {
                callId = callInfo.getCallId();
                Log.e(TAG, "" + callInfo.getCallId());
            }
            final int id = callId;
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {  //press
                    ((ToggleButton) view).setBackgroundResource(R.drawable.btn_blue_glossy);
                    ((ToggleButton) view).setChecked(true);
                    OnDtmf(id, 17, 10);
                    OnDtmf(id, 16, 9);
                    return true;
                }
                case MotionEvent.ACTION_UP: { //release
                    ((ToggleButton) view).setBackgroundResource(R.drawable.btn_lightblue_glossy);
                    ((ToggleButton) view).setChecked(false);
                    OnDtmf(id, 18, 11);
                    OnDtmf(id, 18, 11);
                    return true;
                }
                default: return false;
            }
        }
    });
}

EDIT: the xml for the button:

<ToggleButton
        android:id="@+id/PTT_button5"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:text="@string/ptt5" 
        android:onClick="pushtotalk5"
        android:layout_weight="50"
        android:textOn="Push To Talk On"
        android:textOff="Push To Talk Off"
        android:background="@drawable/btn_lightblue_glossy"
        android:textColor="@android:color/white"
        android:textSize="15sp"
        />      

EDIT: hardware problem, can't test solutions atm.

© Stack Overflow or respective owner

Related posts about android