Reputation: 27
I have a PreferenceActivity in which I would like to have the option to change/lock the screen orientation.
The orientation should lock only the Main Activity screen orientation
I not sure were and how I should call the change listener. On the onCreate and onResume of the Main Activity seems right but I can't get it to work.
settings.xml
<ListPreference android:title="Screen orientation"
android:summary="current: %s"
android:key="rotation"
android:defaultValue="1"
android:entries="@array/listArray"
android:entryValues="@array/listValues" />
array.xml
<resources>
<string-array name="listArray">
<item>Auto rotate</item>
<item>Portrait</item>
<item>Landscape</item>
</string-array>
<string-array name="listValues">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
The Preference Activity
public class MyPreferencesActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
}
public static class MyPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
Links I already looked
How to create a Listener to Preferences changes in Preferences activity?
Adding listeners to individual ListPreference items
How to listen for preference changes within a PreferenceFragment?
OnSharedPreferenceChangeListener not registering change in preference
http://envyandroid.com/android-detect-preference-changes/
Upvotes: 1
Views: 455
Reputation: 2835
After a long time but it's helpful for other developers. You need to implement Preference.OnPreferenceChangeListener
to listen dialog of radio buttons or enteries
in Dialog. Then onPreferenceChange
param newValue
give select ration button value.
class SettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
val modePrefs :ListPreference = findPreference("rotation")!!
modePrefs.onPreferenceChangeListener = this
}
override fun onPreferenceChange(preference: Preference?, newValue: Any?): Boolean {
if ()
return true
}
}
Upvotes: 1