Search code examples
androidontouchlistenermotionevent

Listener for Button press until button is released in android Studio


I am trying to send data via Bluetooth continuously when a button is pressed until it is released, but in my code, I am receiving the data only once when the button is pressed or released.

Here, I am using OnTouchListener with Action_Down and Action_Up. If someone can help me with it, that would be really great, Thank you.

fab1.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    TextView d;

                    switch (event.getAction())
                    {
                        case MotionEvent.ACTION_DOWN:data[0]='H';
                            if(connection_check)
                            {
                                d =findViewById(R.id.datacheck);
                                String str=String.valueOf(data)+"\n";
                                d.setText(str);
                                sendData.write(str.getBytes());
                            }
                            else
                                Toast.makeText(getApplicationContext(), "Bluetooth not connected", Toast.LENGTH_LONG).show();
                        break;

                        case MotionEvent.ACTION_UP:
                            data[0]='L';
                            if(connection_check)
                            {
                                d =findViewById(R.id.datacheck);
                                String str=String.valueOf(data)+"\n";
                                d.setText(str);
                                sendData.write(str.getBytes());
                            }
                            else
                                Toast.makeText(getApplicationContext(), "Bluetooth not connected", Toast.LENGTH_LONG).show();

                            break;


                    }

Solution

  • Your below code to send the data will be called only once when the user presses the button:

          data[0]='H';      
          if(connection_check)
             {
                 d =findViewById(R.id.datacheck);
                 String str=String.valueOf(data)+"\n";
                 d.setText(str);
                 sendData.write(str.getBytes());
             }
          else
            Toast.makeText(getApplicationContext(), "Bluetooth not connected", Toast.LENGTH_LONG).show();
    

    You need to create a handler and runnable to call your send function continuously and start and stop it according to the button events.

    Runnable:

    Runnable dataSender = new Runnable(){
         @override
         void run(){ 
           if(isPressed && connection_check)
           {
               d =findViewById(R.id.datacheck); 
               String str=String.valueOf(data)+"\n";
               d.setText(str);
               sendData.write(str.getBytes());
               handler.postDelayed(this,100) // for 100 milliseconds
           }
          else
             return
         }
      }
    

    Handler:

    Handler handler = new Handler(Looper.getMainLooper())
    

    Now create a global boolean variable isPressed set it to false. Now in your onTouchListener set isPressed accordingly and start the runnable using handler.

    fab1.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        TextView d;
    
                        switch (event.getAction())
                        {
                            case MotionEvent.ACTION_DOWN:
                                 isPressed = true
                                 handler.post(dataSender) // start the handler
                                 break;
    
                            case MotionEvent.ACTION_UP:
                                isPressed = false //to stop the handler
                                data[0]='L';
                                if(connection_check)
                                {
                                    d =findViewById(R.id.datacheck);
                                    String str=String.valueOf(data)+"\n";
                                    d.setText(str);
                                    sendData.write(str.getBytes());
                                }
                                else
                                    Toast.makeText(getApplicationContext(), "Bluetooth not connected", Toast.LENGTH_LONG).show();
    
                                break;
    
    
                        }