Skip to content Skip to sidebar Skip to footer

Ontouchlistener Togglebutton, Ignores First Press?

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,

Solution 1:

Your in_call_card.xml contains the ToggleButton. According to the link of the source code, the in_call_card.xml is inflated in InCallCard.java. InCallCard is a custom view defined by the application.

In order to attach the listener, you need to see where the InCallCard is being used within the InCallActivity. And you find it in the getView method (near the end of the file).

Based on the above observations, your problem can be resolved by:

1) find the button within the InCallCard view

2) Attach the OnTouchListener programatically as I have shown below:

@Overridepublic View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null) {
        convertView = newInCallCard(InCallActivity.this, null);
    }

    if(convertView instanceof InCallCard) {
        InCallCardvc= (InCallCard) convertView;
        vc.setOnTriggerListener(InCallActivity.this);

        // set the touch listener here.. // (1) get the button within the InCallCard view (vc)// (2) set the onTouchListenerfinalViewview= vc.findViewById(R.id.PTT_button5);
        view.setOnTouchListener(newView.OnTouchListener() {
            @OverridepublicbooleanonTouch(View v, MotionEvent event) {
                //if more than one call, change this codeintcallId=0;
                for (SipCallSession callInfo : callsInfo) {
                    callId = callInfo.getCallId();
                    Log.e(TAG, "" + callInfo.getCallId());
                }
                finalintid= 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);
                        returntrue;
                    }
                    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);
                        returntrue;
                    }
                    default: returnfalse;
                }
            }
        });

        SipCallSessionsession= (SipCallSession) getItem(position);
        vc.setCallState(session);
    }

    return convertView;
}

Note: This is not the most efficient way of doing things (as we do a findViewById), but will sure resolve your problems. If you want to improve, read about the ViewHolder pattern in android and use it here.

Also, you should have posted the src code link earlier. Everyone here was giving the right answer but no one was sure of what your particular scenario was.

Solution 2:

Another approach is to use a gesture detector rather than onTouch:

privateclassCustomGestureListenerextendsGestureDetector.SimpleOnGestureListener {
    privateboolean down = false;

    @OverridepublicvoidonShowPress(MotionEvent e) {
        down = true;
        doDownActions();
    }

    publicbooleanonSingleTapUp(MotionEvent e) {
        if (down) {
            down = false;
            doUpActions();
        }

        // The gesture has been used so return true. If you wish to pretend it hasn't// return falsereturntrue;
    }

    // Strictly you may also need to catch some of the other events to// guarantee correct termination of the sequence
}

Then, this listener needs to be created and added to the view:

// Create it.finalGestureDetectormyGestureListener=newGestureDetector(getApplicationContext(), newCustomGestureListener());

// Set it up for use:
view.setOnTouchListener(newOnTouchListener() {
    publicbooleanonTouch(View v, MotionEvent event) {
        myGestureListener.onTouchEvent(event);
        // Pass it on to the system. Be cleverer about this if needed!returnsuper.onTouchEvent(event);
    }
});

// And put an onClick method it to force it to work (this shouldn't be necessary but// it seems like sometimes it is)
view.setOnClickListener(newOnClickListener() {
    @OverridepublicvoidonClick(View v) {
    }
});

Solution 3:

Getting the toggle button would be better if you used the layout. Its easier for me at least

LayoutInflaterinflater= getLayoutInflater();
ViewotherLayout= inflator.inflate(R.layout.toggle_layout, null);
ToggleButtontoggle= (ToggleButton) otherLayout.findViewById(R.id.toggleID);
toggle.setOnTouchListener(newOnTouchListener() {
    @OverridepublicbooleanonTouch(View v, MotionEvent event) {
           switch(event.getAction()) {
             case MotionEvent.ACTION_DOWN:
                 //your codebreak;
             case MotionEvent.ACTION_UP:
                 // your codebreak;
           }
           returnfalse;
    }
});

Try that and let me know. I have the same thing for a button but I use onClick instead of onTouch.

Solution 4:

I just changed your logic a big and it works well for me. You can put your telephony related stuff back and test. And you don't need onClick listener because onTouch listener replaces it completely.

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_touch);

    finalViewview= findViewById(R.id.PTT_button5);
    view.setOnTouchListener(newOnTouchListener() {
        @OverridepublicbooleanonTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {  //press
                    System.out.println("talk");
                    ((ToggleButton) view).setChecked(true);
                    returntrue;
                }
                case MotionEvent.ACTION_UP: { //release
                    System.out.println("don't talk");
                    ((ToggleButton) view).setChecked(false);
                    returntrue;
                }
                default: returnfalse;
            }
        }
    });
}

Solution 5:

is happend because you are seeting listner when you clicked on your button so you have to set listner in onCreate just replace below code with your.

publicvoidOnCreate(Bundle savedInstanceState)
{
          //setContetView//other intlization goes here

          view.setOnTouchListener(newOnTouchListener() {
            @OverridepublicbooleanonTouch(View v, MotionEvent event) {
                //if more than one call, change this codeintcallId=0;
                for (SipCallSession callInfo : callsInfo) {
                    callId = callInfo.getCallId();
                    Log.e(TAG, "" + callInfo.getCallId());
                }
                finalintid= 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);
                        returntrue;
                    }
                    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);
                        returntrue;
                    }
                    default: returnfalse;
                }
            }
        });

}




publicvoidpushtotalk3(final View view) {
        ((ToggleButton) view).setChecked(true);
        ((ToggleButton) view).setChecked(false);


}

Post a Comment for "Ontouchlistener Togglebutton, Ignores First Press?"