Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7075881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:13:52+00:00 2026-05-28T06:13:52+00:00

I have an PreferenceActivity where I would like to add Preferences dynamically. On a

  • 0

I have an PreferenceActivity where I would like to add Preferences dynamically.

On a long click, these shall do something, however OnPreferenceClickListener only supports normal clicks, no long clicks.

Is there a way to implement this feature, did I miss something?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:13:52+00:00Added an answer on May 28, 2026 at 6:13 am

    In the event that the link dies, here is the main body of the post at that link. Note: I did not author anything below.

    The built-in Preference class has a method to receive clicks, onClick, but no method to receive long clicks. In my current project, I actually have a need for this, and found a way to implement it.

    PreferenceActivity is actually a ListActivity, with a special adapter behind the scenes. The usual (not long) clicks are processed by using the usual ListView mechanism, setOnItemClickListener. The code to set this up is in PreferenceScreen:

    public final class PreferenceScreen extends PreferenceGroup implements AdapterView.OnItemClickListener.... {
        public void bind(ListView listView) {
            listView.setOnItemClickListener(this);
            listView.setAdapter(getRootAdapter());
    
            onAttachedToActivity();
        }
    
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            Object item = getRootAdapter().getItem(position);
            if (!(item instanceof Preference)) return;
                         
            final Preference preference = (Preference) item;
            preference.performClick(this);
        }
    }
    

    It would be really easy to subclass PreferenceScreen and override bind to add a long-item-click listener to the list view, except this class is final. Because of this, I ended up adding the following code into my PreferenceActivity subclass:

    public class BlahBlahActivity extends PreferenceActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
     
            addPreferencesFromResource(R.xml.account_options_prefs);
     
            ListView listView = getListView();
            listView.setOnItemLongClickListener(new OnItemLongClickListener() {
                @Override
                public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                    ListView listView = (ListView) parent;
                    ListAdapter listAdapter = listView.getAdapter();
                    Object obj = listAdapter.getItem(position);
                    if (obj != null && obj instanceof View.OnLongClickListener) {
                        View.OnLongClickListener longListener = (View.OnLongClickListener) obj;
                        return longListener.onLongClick(view);
                    }
                    return false;
                }
            });
        }
    }
    

    Now I can have a Preference subclass that implements View.OnLongClickListener, which is automatically invoked for long clicks:

    public class BlahBlahPreference extends CheckBoxPreference implements View.OnLongClickListener {
        @Override
        public boolean onLongClick(View v) {
            // Do something for long click
            return true;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add a support.v4.app.Fragment to a PreferenceActivity header, like so: <header android:fragment=com.example.SupportFragmentSubClass
I would like to be able launch a 2nd Preference screen from my PreferenceActivity.
I have pretty unassuming preferences screen based on PreferenceActivity . (You can see it
I have 1 PreferenceActivity in preferences.xml is <CheckBoxPreference android:id=@+id/pushCB android:checked=true android:defaultValue=true android:key=PushService android:summary=Set to
I have an EditTextPreference in the PreferenceActivity . When user click the EditTextPreference will
I have the same problem like this question: Custom title bar in PreferenceActivity? After
I have a PreferenceActivity that loads its preferences from an XML file which looks
I have used PreferenceActivity to have preference in my android application. I want one
I have an application that uses the new features of honeycomb for the PreferenceActivity.
I have developed an android app that mainly targets smartphones. However in tablet emulator

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.