Timotej Leginus
Timotej Leginus

Reputation: 294

How do I send a command after I clicked a button on Android?

This is my code:

    up.setOnClickListener{GlobalScope.launch{cor.TCP(toWrite="Up")}}
    down.setOnClickListener{GlobalScope.launch{cor.TCP(toWrite="Down")}}
    left.setOnClickListener{GlobalScope.launch{cor.TCP(toWrite="Left")}}
    right.setOnClickListener{GlobalScope.launch{cor.TCP(toWrite="Right")}}

It sends a TCP request to the pre-defined socket and port. There are 4 buttons: up, down, left and right. I want to send a command after the button is pressed, because I am controlling a robot using this, so when I send "Up", it is moving upwards indefinitely, and when I stop pressing the button, I want to send a stop command. So, how do I send a command after I click a button? Thank you very much!

Upvotes: 0

Views: 156

Answers (4)

Giddy Naya
Giddy Naya

Reputation: 4658

Here is the kotlin version of @cesarmarch's answer to be more descriptive in your usecase

Firstly

Your class needs to implement View.OnTouchListener

Secondly

You need to override the onTouch method

override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
    when (view) {
       up -> {
        when (motionEvent.action){
            MotionEvent.ACTION_DOWN -> {
              GlobalScope.launch{cor.TCP(toWrite="Up")}
            }
            MotionEvent.ACTION_UP -> {
                //... Stop the robot here
            }
        }
    }
    down -> {
        when (motionEvent.action){
            MotionEvent.ACTION_DOWN -> {
              GlobalScope.launch{cor.TCP(toWrite="Down")}
            }
            MotionEvent.ACTION_UP -> {
                //... Stop the robot here
            }
        }
    }
    left -> {
        //... Do similar motion check as above
    }
    right -> {
        //... Do similar motion check as above
    }
  }
   return true
}

Finally

Set the listener on the buttons

 ...
 up.setOnTouchListener(this)
 down.setOnTouchListener(this)
 left.setOnTouchListener(this)
 right.setOnTouchListener(this)
 ...

Upvotes: 1

urukh
urukh

Reputation: 380

You could try using View.onTouchListener instead of onClickListener. OnTouchListener gives you access to the MotionEvent of the view, in your case, you'll probably need MotionEvent.ACTION_UP and MotionEvent.ACTION_DOWN, use that to filter what action should be triggered.

You can always refer to the docs:

View.OnTouchListener

MotionEvent

Upvotes: 1

Ilia Kuzmin
Ilia Kuzmin

Reputation: 165

You cannot stop coroutine in GlobalScope. Define custom scope in your class

Upvotes: -1

cesarmarch
cesarmarch

Reputation: 637

If I understand you well, you should use a OnTouchListener instead.

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // Button pressed - send whatever move command
    case MotionEvent.ACTION_UP: 
        // Button unpressed - send STOP command
    }
    return true;
}

You can use the View to detect which button has been touched

Upvotes: 1

Related Questions