15

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

hendrix
  • 3,364
  • 8
  • 31
  • 46
  • 1
    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: https://developer.android.com/reference/android/preference/Preference#attr_android:dependency – ccpizza Mar 05 '20 at 06:30

3 Answers3

6

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.

Thunder
  • 2,994
  • 1
  • 24
  • 19
  • 2
    I use getListView.getChildCount() which just returns 0. I don't know why. – Allen Vork Jun 08 '17 at 06:24
  • @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 – Rafael Lima Jun 08 '19 at 22:22
4

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 
});
soren.qvist
  • 7,376
  • 14
  • 62
  • 91
  • 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? http://groups.google.com/group/android-platform/browse_thread/thread/b70c317a2804128b – hendrix Dec 08 '11 at 14:21
  • 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. – soren.qvist Dec 08 '11 at 15:41
  • 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 – hendrix Jan 20 '12 at 16:57
  • 1
    View v = (View) getListView().getItemAtPosition(1); // make sure it's a View before doing this. Then v.setVisibility(View.GONE) – slinden77 Apr 27 '13 at 10:00
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)

Patrick Jackson
  • 18,766
  • 22
  • 81
  • 141