Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Android, onTouch button doesn't work fro first press

  • 29-10-2013 12:38PM
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,119 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, Registered Users 2 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,119 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