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, onTouch button doesn't work fro first press

Options
  • 29-10-2013 1:38pm
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,090 Mod ✭✭✭✭


    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: 138 ✭✭KeelanM90


    Is it possible to include something similar to:
    ToggleButton PTT = (ToggleButton)findViewById(R.id.PTT_button6);
    pushtotalk6(PTT);
    

    in your onCreate(), it should fix the issue.
    It looks like the view may not be initialised until you have pressed the togglebutton.


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,090 Mod ✭✭✭✭Tar.Aldarion


    I can not do this no, it will cause a null pointer exception (PTT_button6 is in a different xml file to the one referenced in setContentView() and this causes a lot of issues), that is why I am trying this onClick method from xml, which gives a reference to the button. Seems to work well apart from this initial click.


Advertisement