Crear ventana de configuración con soporte a tabletas y sus secciones
Created
February 27, 2019 15:19
-
-
Save webserveis/7133f6720edeb552d274be9c155bcb97 to your computer and use it in GitHub Desktop.
Ventana de preferencia smartphone y tableta
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto"> | |
<Preference | |
android:key="pref_app_name" | |
android:persistent="false" | |
android:title="@string/app_name" | |
app:iconSpaceReserved="false" /> | |
<Preference | |
android:key="pref_app_version" | |
android:title="@string/pref_about_version_title" | |
app:iconSpaceReserved="false" /> | |
<Preference | |
android:key="pref_send_feedback" | |
android:summary="@string/pref_about_send_feedback_summary" | |
android:title="@string/pref_about_send_feedback_title" | |
app:iconSpaceReserved="false" /> | |
<Preference | |
android:summary="@string/pref_about_rate_app_summary" | |
android:title="@string/pref_about_rate_app_title" | |
app:iconSpaceReserved="false"> | |
<intent | |
android:action="android.intent.action.VIEW" | |
android:data="https://play.google.com/store/apps/details?id=app.defaultappmanager.pro" /> | |
</Preference> | |
<Preference | |
android:summary="@string/pref_about_others_apps_summary" | |
android:title="@string/pref_about_others_apps_title" | |
app:iconSpaceReserved="false"> | |
<intent | |
android:action="android.intent.action.VIEW" | |
android:data="https://play.google.com/store/apps/dev?id=8979891956711794454" /> | |
</Preference> | |
<Preference | |
android:summary="@string/pref_about_license_summary" | |
android:title="@string/pref_about_license_title" | |
app:iconSpaceReserved="false"> | |
<intent | |
android:action="android.intent.action.VIEW" | |
android:data="https://webserveisapps.blogspot.com/p/eula.html" /> | |
</Preference> | |
</PreferenceScreen> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto"> | |
<PreferenceCategory android:title="@string/pref_interface_category"> | |
<ListPreference | |
android:defaultValue="theme_default" | |
android:entries="@array/pref_theme_list_entries" | |
android:entryValues="@array/pref_theme_list_values" | |
android:key="pref_theme" | |
android:negativeButtonText="@null" | |
android:positiveButtonText="@null" | |
android:title="@string/pref_interface_theme_title" | |
app:iconSpaceReserved="false" /> | |
</PreferenceCategory> | |
<!--suppress AndroidElementNotAllowed --> | |
<SwitchPreferenceCompat | |
android:defaultValue="false" | |
android:key="pref_dark_theme" | |
android:summary="@string/pref_interface_dark_theme_summary" | |
android:title="@string/pref_interface_dark_theme_title" | |
app:iconSpaceReserved="false" /> | |
<ListPreference | |
android:defaultValue="dark_mode_auto" | |
android:dependency="pref_dark_theme" | |
android:entries="@array/pref_dark_mode_list_entries" | |
android:entryValues="@array/pref_night_mode_list_values" | |
android:key="pref_dark_mode" | |
android:negativeButtonText="@null" | |
android:positiveButtonText="@null" | |
android:title="@string/pref_interface_dark_mode_title" | |
app:iconSpaceReserved="false" /> | |
<!--suppress AndroidElementNotAllowed --> | |
<SwitchPreferenceCompat | |
android:defaultValue="false" | |
android:dependency="pref_dark_theme" | |
android:key="pref_black_oled" | |
android:summary="@string/pref_interface_dark_mode_is_on_summary" | |
android:title="@string/pref_interface_dark_mode_is_on_title" | |
app:iconSpaceReserved="false" /> | |
</PreferenceScreen> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<preference-headers xmlns:app="http://schemas.android.com/apk/res-auto"> | |
<!-- These settings headers are only used on tablets. --> | |
<header | |
app:fragment="com.webserveis.testtheme.SettingsActivity$GeneralPreferenceFragment" | |
app:icon="@drawable/ic_settings_brightness_black_24dp" | |
app:title="@string/pref_general_header" /> | |
<header | |
app:fragment="com.webserveis.testtheme.SettingsActivity$AboutPreferenceFragment" | |
app:icon="@drawable/ic_info_outline_black_24dp" | |
app:title="@string/pref_about_header" /> | |
</preference-headers> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.webserveis.basewebtool; | |
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.content.res.Configuration; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.preference.PreferenceManager; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.widget.Toast; | |
import com.webserveis.basewebtool.helper.ThemeHelper; | |
import net.mm2d.preference.Header; | |
import net.mm2d.preference.PreferenceActivityCompat; | |
import java.util.List; | |
import java.util.Objects; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.Nullable; | |
import androidx.appcompat.app.ActionBar; | |
import androidx.core.app.NavUtils; | |
import androidx.core.app.ShareCompat; | |
import androidx.preference.ListPreference; | |
import androidx.preference.Preference; | |
import androidx.preference.PreferenceFragmentCompat; | |
public class SettingsActivity extends PreferenceActivityCompat implements SharedPreferences.OnSharedPreferenceChangeListener { | |
private boolean isPreferencesChanged; | |
private static final Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = (preference, value) -> { | |
String stringValue = value.toString(); | |
if (preference instanceof ListPreference) { | |
ListPreference listPreference = (ListPreference) preference; | |
int index = listPreference.findIndexOfValue(stringValue); | |
preference.setSummary( | |
index >= 0 | |
? listPreference.getEntries()[index] | |
: null); | |
} else { | |
preference.setSummary(stringValue); | |
} | |
return true; | |
}; | |
private static boolean isXLargeTablet(final Context context) { | |
return (context.getResources().getConfiguration().screenLayout | |
& Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE; | |
} | |
private static void bindPreferenceSummaryToValue(final Preference preference) { | |
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener); | |
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, | |
PreferenceManager | |
.getDefaultSharedPreferences(preference.getContext()) | |
.getString(preference.getKey(), "")); | |
} | |
@Override | |
protected void onCreate(final Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
new ThemeHelper(this); | |
setupActionBar(); | |
} | |
private void setupActionBar() { | |
final ActionBar actionBar = getSupportActionBar(); | |
if (actionBar != null) { | |
actionBar.setDisplayHomeAsUpEnabled(true); | |
} | |
} | |
@Override | |
public boolean onIsMultiPane() { | |
return isXLargeTablet(this); | |
} | |
@Override | |
public void onBuildHeaders(@NonNull final List<Header> target) { | |
loadHeadersFromResource(R.xml.pref_headers, target); | |
} | |
@Override | |
public boolean isValidFragment(final String fragmentName) { | |
return PreferenceFragmentCompat.class.getName().equals(fragmentName) | |
|| GeneralPreferenceFragment.class.getName().equals(fragmentName) | |
|| AboutPreferenceFragment.class.getName().equals(fragmentName); | |
} | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
PreferenceManager.getDefaultSharedPreferences(this).unregisterOnSharedPreferenceChangeListener(this); | |
} | |
@Override | |
public void onBackPressed() { | |
Toast.makeText(this, "headers" + hasHeaders(), Toast.LENGTH_SHORT).show(); | |
if (hasHeaders()) { | |
if (isPreferencesChanged) { | |
Intent upIntent = NavUtils.getParentActivityIntent(this); | |
NavUtils.navigateUpTo(this, Objects.requireNonNull(upIntent)); | |
} else { | |
super.onBackPressed(); | |
} | |
} else { | |
super.onBackPressed(); | |
} | |
} | |
@Override | |
public boolean onOptionsItemSelected(final MenuItem item) { | |
switch (item.getItemId()) { | |
case android.R.id.home: | |
onBackPressed(); | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
@Override | |
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { | |
isPreferencesChanged = true; | |
} | |
public static class GeneralPreferenceFragment extends CustomPreferenceFragmentCompat { | |
@Override | |
public void onCreatePreferences( | |
final Bundle savedInstanceState, | |
final String rootKey) { | |
addPreferencesFromResource(R.xml.pref_general); | |
setHasOptionsMenu(true); | |
bindPreferenceSummaryToValue(findPreference("pref_theme")); | |
bindPreferenceSummaryToValue(findPreference("pref_dark_mode")); | |
} | |
} | |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) | |
public static class AboutPreferenceFragment extends CustomPreferenceFragmentCompat { | |
@Override | |
public void onCreatePreferences( | |
final Bundle savedInstanceState, | |
final String rootKey) { | |
addPreferencesFromResource(R.xml.pref_about); | |
setHasOptionsMenu(true); | |
findPreference("pref_app_name").setSummary(BuildConfig.APPLICATION_ID); | |
findPreference("pref_app_version").setSummary(BuildConfig.VERSION_NAME); | |
// feedback preference click listener | |
Preference myPref = findPreference("pref_send_feedback"); | |
myPref.setOnPreferenceClickListener(preference -> { | |
ShareCompat.IntentBuilder.from(Objects.requireNonNull(getActivity())) | |
.setType("message/rfc822") | |
.addEmailTo("webserveis@gmail.com") | |
.setSubject("Feedback: " + getText(R.string.app_name)) | |
.setText("Feedback content") | |
//.setHtmlText(body) //If you are using HTML in your body text | |
.setChooserTitle(R.string.pref_about_send_feedback_header) | |
.startChooser(); | |
return true; | |
}); | |
} | |
} | |
private static abstract class CustomPreferenceFragmentCompat extends PreferenceFragmentCompat { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
} | |
//Hack remove margin fragment | |
@Override | |
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
View containerParent = (View) view.getParent(); | |
containerParent.setPadding(0, 0, 0, 0); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.webserveis.testtheme; | |
import android.annotation.TargetApi; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.res.Configuration; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.preference.PreferenceManager; | |
import android.view.MenuItem; | |
import android.view.View; | |
import net.mm2d.preference.Header; | |
import net.mm2d.preference.PreferenceActivityCompat; | |
import java.util.List; | |
import java.util.Objects; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.Nullable; | |
import androidx.appcompat.app.ActionBar; | |
import androidx.core.app.NavUtils; | |
import androidx.core.app.ShareCompat; | |
import androidx.preference.ListPreference; | |
import androidx.preference.Preference; | |
import androidx.preference.PreferenceFragmentCompat; | |
public class SettingsActivity extends PreferenceActivityCompat { | |
private boolean inFragment; | |
private static final Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = (preference, value) -> { | |
String stringValue = value.toString(); | |
if (preference instanceof ListPreference) { | |
ListPreference listPreference = (ListPreference) preference; | |
int index = listPreference.findIndexOfValue(stringValue); | |
preference.setSummary( | |
index >= 0 | |
? listPreference.getEntries()[index] | |
: null); | |
} else { | |
preference.setSummary(stringValue); | |
} | |
return true; | |
}; | |
private static boolean isXLargeTablet(final Context context) { | |
return (context.getResources().getConfiguration().screenLayout | |
& Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE; | |
} | |
private static void bindPreferenceSummaryToValue(final Preference preference) { | |
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener); | |
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, | |
PreferenceManager | |
.getDefaultSharedPreferences(preference.getContext()) | |
.getString(preference.getKey(), "")); | |
} | |
@Override | |
protected void onCreate(final Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
new ThemeHelper(this); | |
setupActionBar(); | |
} | |
private void setupActionBar() { | |
final ActionBar actionBar = getSupportActionBar(); | |
if (actionBar != null) { | |
actionBar.setDisplayHomeAsUpEnabled(true); | |
} | |
} | |
@Override | |
public boolean onIsMultiPane() { | |
return isXLargeTablet(this); | |
} | |
@Override | |
public void onBuildHeaders(@NonNull final List<Header> target) { | |
loadHeadersFromResource(R.xml.pref_headers, target); | |
} | |
@Override | |
public boolean isValidFragment(final String fragmentName) { | |
return PreferenceFragmentCompat.class.getName().equals(fragmentName) | |
|| GeneralPreferenceFragment.class.getName().equals(fragmentName) | |
|| AboutPreferenceFragment.class.getName().equals(fragmentName); | |
} | |
@Override | |
public void onBackPressed() { | |
if (!inFragment) { | |
Intent upIntent = NavUtils.getParentActivityIntent(this); | |
NavUtils.navigateUpTo(this, Objects.requireNonNull(upIntent)); | |
} else { | |
inFragment = false; | |
super.onBackPressed(); | |
} | |
} | |
@Override | |
public boolean onOptionsItemSelected(final MenuItem item) { | |
switch (item.getItemId()) { | |
case android.R.id.home: | |
onBackPressed(); | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
public static class GeneralPreferenceFragment extends CustomPreferenceFragmentCompat { | |
@Override | |
public void onCreatePreferences( | |
final Bundle savedInstanceState, | |
final String rootKey) { | |
addPreferencesFromResource(R.xml.pref_general); | |
setHasOptionsMenu(true); | |
bindPreferenceSummaryToValue(findPreference("pref_theme")); | |
bindPreferenceSummaryToValue(findPreference("pref_dark_mode")); | |
} | |
} | |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) | |
public static class AboutPreferenceFragment extends CustomPreferenceFragmentCompat { | |
@Override | |
public void onCreatePreferences( | |
final Bundle savedInstanceState, | |
final String rootKey) { | |
addPreferencesFromResource(R.xml.pref_about); | |
setHasOptionsMenu(true); | |
findPreference("pref_app_name").setSummary(BuildConfig.APPLICATION_ID); | |
findPreference("pref_app_version").setSummary(BuildConfig.VERSION_NAME); | |
// feedback preference click listener | |
Preference myPref = findPreference("pref_send_feedback"); | |
myPref.setOnPreferenceClickListener(preference -> { | |
ShareCompat.IntentBuilder.from(Objects.requireNonNull(getActivity())) | |
.setType("message/rfc822") | |
.addEmailTo("webserveis@gmail.com") | |
.setSubject("Feedback: " + getText(R.string.app_name)) | |
.setText("Feedback content") | |
//.setHtmlText(body) //If you are using HTML in your body text | |
.setChooserTitle(R.string.pref_about_send_feedback_header) | |
.startChooser(); | |
return true; | |
}); | |
} | |
} | |
private static abstract class CustomPreferenceFragmentCompat extends PreferenceFragmentCompat { | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
((SettingsActivity) Objects.requireNonNull(getActivity())).inFragment = true; | |
} | |
//Hack remove margin fragment | |
@Override | |
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
View containerParent = (View) view.getParent(); | |
containerParent.setPadding(0, 0, 0, 0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment