Android: long click on a button -> perform actions

Android

Android Problem Overview


I want to use the same button to perform 2 different methods. One method when user single clicks it and a second method (different) when the user LONG clicks it.

I use this for the single short click (which works great):

Button downSelected = (Button) findViewById(R.id.downSelected);
		downSelected.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				method();
				}
			}
			
		});

I've tried to add a longClickListener but it didn't work.

Appreciate any ideas on how to solve this.

Thanks!

Android Solutions


Solution 1 - Android

I've done it before, I just used:

down.setOnLongClickListener(new OnLongClickListener() {	
		@Override
		public boolean onLongClick(View v) {
			// TODO Auto-generated method stub
			return true;
		}
	});

Per documentation:

> public void setOnLongClickListener > (View.OnLongClickListener l) > > Since: API Level 1 Register a callback > to be invoked when this view is > clicked and held. If this view is not > long clickable, it becomes long > clickable.

Notice that it requires to return a boolean, this should work.

Solution 2 - Android

To get both functions working for a clickable image that will respond to both short and long clicks, I tried the following that seems to work perfectly:

    image = (ImageView) findViewById(R.id.imageViewCompass);
    image.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        	shortclick();
        }
     });
    
    image.setOnLongClickListener(new View.OnLongClickListener() {
    public boolean onLongClick(View v) {
    	longclick();
    	return true;
    }
});

//Then the functions that are called:

 public void shortclick()
{
 Toast.makeText(this, "Why did you do that? That hurts!!!", Toast.LENGTH_LONG).show();

}

 public void longclick()
{
 Toast.makeText(this, "Why did you do that? That REALLY hurts!!!", Toast.LENGTH_LONG).show();

}

It seems that the easy way of declaring the item in XML as clickable and then defining a function to call on the click only applies to short clicks - you must have a listener to differentiate between short and long clicks.

Solution 3 - Android

Initially when i implemented a longClick and a click to perform two separate events the problem i face was that when i had a longclick , the application also performed the action to be performed for a simple click . The solution i realized was to change the return type of the longClick to true which is normally false by default . Change it and it works perfectly .

Solution 4 - Android

Change return false; to return true; in longClickListener

You long click the button, if it returns true then it does the work. If it returns false then it does it's work and also calls the short click and then the onClick also works.

Solution 5 - Android

Try using an ontouch listener instead of a clicklistener.

http://developer.android.com/reference/android/view/View.OnTouchListener.html

Solution 6 - Android

The simplest and updated method is using a long click listener like

someView.setOnLongClickListener {
        //do your work 
        true
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLior IluzView Question on Stackoverflow
Solution 1 - AndroidblindstuffView Answer on Stackoverflow
Solution 2 - AndroidmklopferView Answer on Stackoverflow
Solution 3 - AndroidSaieshView Answer on Stackoverflow
Solution 4 - AndroidVijayView Answer on Stackoverflow
Solution 5 - AndroidFalmarriView Answer on Stackoverflow
Solution 6 - AndroidHarisView Answer on Stackoverflow