Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

android, onClick with onTouch method, doesn't work for first click

Options
  • 30-10-2013 10:23am
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,097 Mod ✭✭✭✭


    Tried development but maybe here is better. My problem is that I have a button that should run code when it is pressed and when it is released, but it only works after the initial press. The first press does not enter the method, apart from when I let go of the button, where it will display: "initial part of method" but will not enter the onTouch method. After this every press works as intended, why is this happening and how can I get the first press to work?



    I have a toggle button defined as such in xml:

    <ToggleButton
    		    android:id="@+id/PTT_button6"
    		    android:layout_width="0dp"
    		    android:layout_height="fill_parent"
    	            android:onClick="pushtotalk6"
    		    android:text="@string/ptt5" 
    		    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"
    			/>      
    

    Which calls this function:
    public void pushtotalk6(final View view) {
     	   Toast.makeText(getApplicationContext(), "initial part of method", Toast.LENGTH_SHORT).show();
               view.setOnTouchListener(new OnTouchListener() {
            	      
             @Override
             public boolean onTouch(View v, MotionEvent event) {
    
    	    	  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: {
                      ((ToggleButton) view).setChecked(true);			   
                      return true;
                  }
                  case MotionEvent.ACTION_UP: {
                      ((ToggleButton) view).setChecked(false);		                     
                      return true;
                  }
                  default:
                      return false;
                  }
             }
    
    		
    		
         });
         }
    


Comments

  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    I will guess that onTouch happens before onClick, thus once the onClick method has been entered, the onTouch has already passed.

    After you first press the button, you touch it, click it, then activate the on touch listener, then after that the ontouch listener is activated for the next use.

    Maybe using onTouchEvent instead of android:onClick="pushtotalk6" will allow you greater scope to use your cases.

    Basically activate the onTouchListener as soon as they touch it, MotionEvent.ACTION_DOWN I guess, rather than when they click it, which is MotionEvent.ACTION_UP I guess.

    Haven't used ToggleButton and there is a lot of guess work in my answer! :pac:


  • Registered Users Posts: 6,177 ✭✭✭Talisman


    onemorechance is correct - You are listening for the onClick event when your function is defined to handle both onTouch and onClick events.
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN: {
        // This is onTouch
        ((ToggleButton) view).setChecked(true);			   
        return true;
      }
      case MotionEvent.ACTION_UP: {
        // This is onClick
        ((ToggleButton) view).setChecked(false);		                     
        return true;
      }
      default:
        return false;
     }
    


Advertisement