I need to set default value for the ListPreference that is created dynamically. It's dynamically created because it's a preference for camera focus modes and each device can have different set of focus modes. listPreferenceFocusMode.setDefaultValue(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
does not work, or How to set the Default Value of a ListPreference accepted answer in this link does not work, and it' is not right because it overrides user's selection.
// Preview Focus Mode
int indexContinuous = 0;
if (focusModes != null && focusModes.size() > 0) {
entries = new String[focusModes.size()];
entryValues = new String[focusModes.size()];
for (int i = 0; i < focusModes.size(); i++) {
entryValues[i] = entries[i] = focusModes.get(i);
if (entryValues[i].equals(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
indexContinuous = i;
}
}
listPreferenceFocusMode.setEntries(entries);
listPreferenceFocusMode.setEntryValues(entryValues);
if (listPreferenceFocusMode.getValue() == null) {
listPreferenceFocusMode.setValueIndex(indexContinuous);
}
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
listPreferenceFocusMode.setDefaultValue(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
} else {
listPreferenceFocusMode.setDefaultValue(Camera.Parameters.FOCUS_MODE_AUTO);
}
} else {
listPreferenceFocusMode.setEnabled(false);
listPreferenceFocusMode.setSummary("Focus Modes are not supported");
}
I tried setting defaultValue or getting value using listPreferenceFocusMode.getValue()
but it does not work also because listPreferenceFocusMode.getValue()
is not null
. Thanks in advance.
I solved it using a boolean flag to check that if it's the first start of the application. If it is, set
listPreferenceFocusMode.setValueIndex(indexContinuous);
and set true and save isFirstStart flag to sharedPrefrerences to invoke setValueIndex method only once. It would be better iflistPrefrence
remembered it's dynamic default value but it does not.