Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform both the normal click and long click at button

Tags:

android

button

I have a single button named as CheckIn.Have a look at my code.

    checkIn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

       Toast.makeText(HomeSafeActivity.this, "Normal Press", Toast.LENGTH_LONG).show();

    });


    checkIn.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {

            Toast.makeText(HomeSafeActivity.this, "Long press", Toast.LENGTH_LONG).show();



            return false;
        }
    });

Now when i normal press the button the message shows as Normal press.When i Long press the button the message shows as long press as well as Normal press both. What i want that when i long press the button only long press event should triggered not the normal press event.How can i achieve this??

like image 554
Deepak Sharma Avatar asked Sep 20 '13 07:09

Deepak Sharma


2 Answers

I got the solution of my Question.Return the true instead of false.Just see below:-

    checkIn.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {

            Toast.makeText(HomeSafeActivity.this, "Long preess", Toast.LENGTH_LONG).show();

            return true;
        }
    });
like image 144
Deepak Sharma Avatar answered Oct 03 '22 13:10

Deepak Sharma


onLongClick() - This returns a boolean to indicate whether you have consumed the event and it should not be carried further. That is, return true to indicate that you have handled the event and it should stop here; return false if you have not handled it and/or the event should continue to any other on-click listeners.

like image 43
Sunil Kumar Avatar answered Oct 03 '22 13:10

Sunil Kumar