How can I select the default title in my PreferenceActivity on tablets?

I work with PreferenceActivity , which will be fully compatible with tablets .

To do this, I will work on the recommendation of Google on this page.

 @Override public void onBuildHeaders(List<Header> target) { loadHeadersFromResource(R.xml.preference_headers, target); } 

My problem is that I would like to be able to select the default header when starting this activity.

For example, I have several headers;

  • General settings
  • User interface settings
  • Network settings

And depending on what activity I came from, I would like to display the correct settings.

Is there any way to achieve this?

+4
source share
3 answers

When creating an Intent to call the PreferenceActivity function, you can add an additional line 'EXTRA_SHOW_FRAGMENT' to indicate which fragment should be initially displayed. You pass the name of the fragment you want to select.

For example, if you want to select the General Settings header (and its contents), you can use the following code:

 final Intent intent = new Intent(this, ExtendedPreferenceActivity.class); // Assume we call it from an other activty intent.putExtra(EXTRA_SHOW_FRAGMENT, GeneralSettingsFragment.class.getName()); startActivity(intent); 

More information about this can be found here: http://developer.android.com/reference/android/preference/PreferenceActivity.html

A problem report on Google says that for Android version 3.0, the correct title will also not be automatically selected. For a problem report and a workaround, see here: release report .

+8
source

Dynamically creating PreferenceHeaders using the PreferenceActivity.Header class http://developer.android.com/reference/android/preference/PreferenceActivity.Header.html

0
source

You can use the default snippet:

Here is what I did:

 public class PreferencesActivity extends SherlockPreferenceActivity { /** Variables **/ /** Constants **/ private static final String CLASSTAG = PreferencesActivity.class.getSimpleName(); /** Class Methods **/ @Override public void onCreate(Bundle savedInstanceState) { Log.v(CLASSTAG, "onCreate"); super.onCreate(savedInstanceState); initializeUI(); } @Override public Intent getIntent() { Log.v(CLASSTAG, "getIntent"); final Intent modIntent = new Intent(super.getIntent()); modIntent.putExtra(EXTRA_SHOW_FRAGMENT, SettingsFragment.class.getName()); modIntent.putExtra(EXTRA_NO_HEADERS, true); return modIntent; } /** Private Functions **/ private void initializeUI() { getSupportActionBar().hide(); } /** Classes **/ public static class SettingsFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.settings_preference); initializeUI(); } private void initializeUI() { } } } 

and default xml (like previous versions of HoneyComb ...):

 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="@string/preferences_category_1"> <com.taptime.ui.preferences.ClickPreference android:key="@string/preferences_conditions_key" android:title="@string/preferences_conditions_title"/> </PreferenceCategory> <PreferenceCategory android:title="@string/preferences_category_2"> <com.newin.android.ui.widget.ClickPreference android:key="@string/preferences_logout_key" android:title="@string/preferences_logout_title" android:summary="@string/preferences_logout_summary"/> </PreferenceCategory> </PreferenceScreen> 
0
source

Source: https://habr.com/ru/post/1391819/


All Articles