How to Add a Long Click to an AndroidX Preference



This site utilizes Google Analytics, Google AdSense, as well as participates in affiliate partnerships with various companies including Amazon. Please view the privacy policy for more details.

As part of my goals for this year, I’ve been working on a rather simple Android app.

This app is largely done through the AndroidX Preferences API. This Preference API seemed to lack the ability to handle a long click.

At least it appeared that way initially.

Unfortunately, the only way I found to implement a long click was to extend the preference and override its onBindViewHolder method.

To implement a “long-clickable preference”, I extended the Preference class, included a longClickListener listener that does nothing by default, and an onLongClick method that takes an OnLongClickListener:

import android.view.View;
import androidx.preference.*;

public class LongClickablePreference extends Preference {

    private View.OnLongClickListener longClickListener = v -> true;

    public LongClickableEditTextPreference(final Context context) {
        super(context);
    }

    @Override
    public void onBindViewHolder(final PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);

        View itemView = holder.itemView;
        itemView.setOnLongClickListener(this.longClickListener);
    }

    @Override
    public void onLongClick(final View.OnLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }
}

If you look at the methods available in the View, you’ll see that there is a setLongClickable method. This doesn’t need to be explicitly set to true - note that the JavaDoc for the setOnLongClickListener method states that “If this view is not long clickable, it becomes long clickable.”

Unfortunately, you’ll need to make a new class for each type of preference. Preferences that I was able to define that extend Preference include:

  • DialogPreference
  • PreferenceGroup
  • SeekBarPreference
  • TwoStatePreference
  • CheckBoxPreference
  • DropDownPreference
  • EditTextPreference
  • ListPreference
  • MultiSelectListPreference
  • PreferenceCategory
  • PreferenceScreen
  • SwitchPreference
  • SwitchPreferenceCompat

If you don’t want to make a new class file, you could always create an anonymous class:

Preference pref = new Preference(context) {
   @Override
    public void onBindViewHolder(final PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);

        View itemView = holder.itemView;
    itemView.setOnLongClickListener(l -> doTheThing());
    }
};

A couple of sources from Stack Overflow:

  • https://stackoverflow.com/questions/8912388/how-to-add-a-long-click-listener-to-a-preference
  • https://stackoverflow.com/questions/29115216/how-to-make-a-preference-long-clickable-in-preferencefragment

Leave a Reply

Note that comments won't appear until approved.