Android get view of Preference in PreferenceActivity

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.


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 
});

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)