Lisa Anne
Lisa Anne

Reputation: 4595

How to perform an action as long as a Button is kept pressed

Hi and Thanks for your help.

I need a button to perform the following behavior:

I have tried this, but does not work:

Button left = (Button) findViewById(R.id.left);


    left.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.e("","left");
            return false;
        }
    });

Upvotes: 0

Views: 431

Answers (6)

Ankit Popli
Ankit Popli

Reputation: 2837

I think you are looking for a combination of OnTouchListener and MotionEvent.ACTION_DOWN

Edit:

    switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // TODO start scroll
                break;
            case MotionEvent.ACTION_UP:
                // TODO stop scroll
                break;
            default:
                break;
            }
            return false;

Hope this helps.. :)

Upvotes: 4

Akshat
Akshat

Reputation: 459

Use MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP for detecting action up as well as down

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            //start action here
            break;


        }
        return super.onTouchEvent(event);
    }

Upvotes: 3

Macrosoft-Dev
Macrosoft-Dev

Reputation: 2185

make new scroll view

ScrollView sv=new ScrollView(this);

and when you click/start button. then start thread which increment int value and scroll, and on key action down. stop the thread.

//start thread which incrment value one by one 
sv.scrollBy(x, y); //x  the amount of pixels to scroll by horizontally 
           //y  the amount of pixels to scroll by vertically

Upvotes: 1

Lalit Poptani
Lalit Poptani

Reputation: 67296

You can use MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP for detecting action up and down

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            //start action
            break;

        case MotionEvent.ACTION_UP:
            //stop action
            break;
        }
        return super.onTouchEvent(event);
    }

Upvotes: 1

Viswanath Lekshmanan
Viswanath Lekshmanan

Reputation: 10093

Button button = (Button) findViewById(R.id.button);
button.setOnTouchListener(new View.OnTouchListener() {

@Override public boolean onTouch(View v, MotionEvent event) {
    switch(event.getAction()) {
    case MotionEvent.ACTION_DOWN:
 //Start your work
        break;
    case MotionEvent.ACTION_UP:
 // stop the work
        break;
   case MotionEvent.ACTION_CANCEL:
 // stop the work
        break;
    }
    return false;
}



});

Upvotes: 3

pavanmvn
pavanmvn

Reputation: 757

By default you can use onlongclick listener, But in your case it won't give effect as its max time 1-2 sec, so apply motion event on button and apply your logic..

Upvotes: 1

Related Questions