Android Gets PreferenceActivity Preference

I would like to get a View instance that is used to display a specific Preference in my PreferenceActivity, so I can change 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 found this method only: getView (view convertView, parent group of ViewGroup) . But it seems confused to me that if I want to get an idea of ​​my preferences, I need to provide view and viewGroup as parameters

Can someone explain how to use this method, or tell me another method to get a view from my Preference instance.

PS: if possible, I would prefer NOT to extend the preference class, but I don't mind if necessary

+8
android view preferenceactivity
source share
3 answers

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

getPreferenceScreen (). RemovePreference (thePreference)

+1
source share

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

EDIT: Here is the code in my formatted comment:

 getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() { // ... put listener code here }); 
+4
source share

To get an idea of ​​the 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 cannot do this in the onCreate method, because it will throw a NullPointerException .

+2
source share

All Articles