Question

I'm working with a Door control unit to control a car window. I have 4 buttons (Auto up and down, and up and down). They are doing a good job, but I want the single up and down buttons to react like in a real car. If I tap long on them they should move as long as the button is clicked.

I tried to implement that with an OnTouchListener and it even works, but only if I move around on the button. If I just click and hold my finger, it does a short single move and then nothing until I move my finger around.

Is there something like an isClicked() method? I tried isActivated, isPressed, isSelected, is Focused, but none of them solved my problem.

Was it helpful?

Solution

You have to program it yourself in your onTouch method: What you describe happen because the touch event is triggered with param Event.DOWN when touched, then with param Event.MOVE when your finger moves and finally with Event.UP when releasing.

So you will have to detect the touch DOWN event, start a timer that will put the windows up every x milli seconds, then stop when detecting the touch UP event

Here is an example:

@Override
public boolean onTouch(View v, MotionEvent event) {

    if(event.getAction()==MotionEvent.ACTION_DOWN)
    {
        startTimer(); // The timer should repeatly call itself

    }
    else if(event.getAction()==MotionEvent.ACTION_UP)
    {
        stopTimer(); // The timer should stop

    }
}

EDIT: If all you need is the equivalent of a isClicked() method, you can juste store a boolean as class variable: in the above code, replace "startTimer()" by "mClicked = true", and "stopTimer()" by "mClicked=false". Then anywhere in your class you will be able to check if the button is currently clicked

OTHER TIPS

use selector for your implementation:

just make one diresctory named "drawable" in that make one xml file like this:

<?xml version="1.0" encoding="utf-8"?>

<item android:state_focused="true"><color android:color="@color/red" />
</item>
<item android:state_pressed="true"><color android:color="@color/red" />
</item>
<item android:state_focused="false"><color android:color="@color/white" />
</item>
<item android:state_pressed="false"><color android:color="@color/white" />
</item>

now in your layout file: give button backgroud of this drawable:

  android:background="@drawable/your_file"

it will work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top