Android get view of Preference in PreferenceActivity

12,650

Solution 1

I'm not sure on how to get the view for a preference, but if you want to remove the view from the screen (set visibility to View.gone) you can use the following:

getPreferenceScreen().removePreference(thePreference)

Solution 2

To get the view of a desired preference you can use this:

@Override
public void onWindowFocusChanged(boolean hasFocus)
{
    super.onWindowFocusChanged(hasFocus);
    Preference preference=findPreference(“preferenceKey”);
    View preferenceView=getListView().getChildAt(preference.getOrder());
    //Do your stuff
}

Note: You can not do this in the onCreate method because it will throw a NullPointerException.

Solution 3

PreferenceActivity inherits the ListActivity class. ListActivity has a method called getListView() which returns the ListView that displays your preferences.

EDIT: Here is the code in my comment formatted:

getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
       // ... put listener code here 
});
Share:
12,650
hendrix
Author by

hendrix

2007-2012 student at Comenius University in Bratislava - Faculty of Mathematics, Physics and Informatics

Updated on July 19, 2022

Comments

  • hendrix
    hendrix almost 2 years

    I would like to get View instance that is used to display specific Preference in my PreferenceActivity, so i can modify its properties, for example:

    public class SettingsActivity extends PreferenceActivity {
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            addPreferencesFromResource(R.xml.preference);
            Preference pref = findPreference("key");
            pref.getView().setVisibility(View.GONE);
                //not necessarily setVisibility, i hope you get my point
        }
    }
    

    I only found this method: getView (View convertView, ViewGroup parent). But it seems confusing to me, that if i want to get View of my preference, i need to provide view and viewGroup as parameters??

    Could someone explain how to use this method, or point me to another method to get View from my Preference instance.

    PS: if possible, i would rather NOT extend Preference class, but i dont mind it if necessary

    • ccpizza
      ccpizza over 4 years
      if instead of hiding you simply want to disable a preference widget based on the state of another preference then you can do that in XML with android:dependency="pref_key_another_key". See also: developer.android.com/reference/android/preference/…
  • hendrix
    hendrix over 12 years
    ok i see your point and considering this, is there any better way of accessing single Preference view (not whole list view) that the one described here? groups.google.com/group/android-platform/browse_thread/threa‌​d/…
  • soren.qvist
    soren.qvist over 12 years
    Absolutely, ListView inherits AdapterView. So you can attach an AdapterView.OnItemClickListener using setOnItemClickListener(). So you could do something like: getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { // ... put listener code here }); I've formatted the code and put it in my edits above.
  • hendrix
    hendrix over 12 years
    hello, sorry to reply with such a delay. Anyways i dont think you fully understood what i wanted to achieve: Preference activity has listView. This listView represents list of preferences. I understand it, that ListView consists of several views, each view representing single list item. For example, i have 3 preferences in list (PreferenceActivity), and i want to set visibility of view representing first preference to INVISIBLE. For this purpose i need instance of View class, that is assigned to that single preference (not ListView). Bottom line - this does not have anything to do with onClick
  • slinden77
    slinden77 about 11 years
    View v = (View) getListView().getItemAtPosition(1); // make sure it's a View before doing this. Then v.setVisibility(View.GONE)
  • Allen Vork
    Allen Vork about 7 years
    I use getListView.getChildCount() which just returns 0. I don't know why.
  • Rafael Lima
    Rafael Lima about 5 years
    @AllenVork, because this code doesn't work... preferencescreen extends from ListView but it doesn't store its views... the custom adapter create new views at every onBind and doesn't keep them so you CAN'T ACCESS IT THROUGH ANY PUBLIC METHOD