SeekBarProperties not working - android

I've put a SeekBarPreference in my PreferenceScreen (xml), but the value isn't saved and also the default value isn't taken in account.
The bar is always at zero.
Here the piece of code:
<PreferenceCategory
android:title="mySettings">
<SeekBarPreference
android:key="m_set"
android:defaultValue="210"
android:title="Transparency"
android:max="255"
android:summary="Adjust..." />
</PreferenceCategory>
Any idea?
Other stuff like CheckBoxPreference works fine.

There is no SeekBarPreference, you must create own class, check:
this tutorial

Related

Android Preference. How to force update summary for the preference with SummaryProvider setted?

I have a modified EditTextPreference with using SimpleSummaryProvider:
<NumberPreference
android:key="pref_key"
android:defaultValue="0"
android:title="Some title"
app:useSimpleSummaryProvider="true"/>
If I change this preference programmatically (for example in the onSharedPreferenceChanged event), then the summary in the graphical interface will not update. Is there any way to notify the Preference widget that it needs to read the value again and display it in the summary? Without manually assigning a specific value to the summary?
I am using PreferenceFragmentCompat and a modified EditTextPreference with a modified EditTextPreferenceDialogFragmentCompat.
I looked at the Android sources (EditTextPreferenceDialogFragmentCompat.java) - how the dialog for changing settings notifies EditTextPreference about a value change. Did as well and it worked :)
if (editTextPreference().callChangeListener(value)) {
editTextPreference().setText(value);
}

How do I set showSeekBarValue to true in Android?

I'm writing my first Android app and it involves a preference where you set an amount of minutes, from 0 to 60. I've used a SeekBarPreference, and it shows up as a simple slider which you can indeed slide around to edit the value. The preference works fine.
I would like to show the selected value next to the SeekBar (or somewhere in the vicinity), as there by default is no way for the user to see what they've actually selected. There are lots of questions on Stack Overflow about similar sliders and preferences, but since version 25.1.0, released last December, there is SeekBarPreference, which has just what I need:
The seekbar value view can be shown or disabled by setting showSeekBarValue attribute to true or false, respectively.
But among the public methods listed below, there is no method for setting showSeekBarValue? There is a method for setting the adjustable attribute, setAdjustable(boolean adjustable), however?
Ideally I'd just write android:showSeekBarValue="true" in my SeekBarPreference tag in preferences.xml, but that (obviously) doesn't work.
So in a nutshell: how do I set showSeekBarValue to true?
In the app's build.gradle, I had to add
implementation 'com.android.support:preference-v7:26.+
Then, modify the PreferenceScreen's xml to include an app namespace:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
Finally, inside the xml, you can add
<SeekBarPreference
android:key="..."
android:title="..."
android:max="100"
android:min="0"
android:defaultValue="30"
android:dependency="..."
app:showSeekBarValue="true"
/>
This compiles without errors. To be fair, this is not a complete answer, because no value is shown in my case, even after these steps. But formally it does let you set the value in some way.
You need to use PreferenceScreen from android.support.v7.preference
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<SeekBarPreference
android:key="size"
android:title="Size"
android:summary="size of progressBar in dp's"
android:max="100"
app:showSeekBarValue="true"
android:defaultValue="25" />
</android.support.v7.preference.PreferenceScreen>
and also PreferenceFragmentCompat from android.support.v7.preference
import android.support.v7.preference.PreferenceFragmentCompat
class SettingsFragment: PreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
addPreferencesFromResource(R.xml.preferences)
}
}

Two problems with the Activity settings in Android

I have two roughly connected issues in my new Android app regarding the Settings. The former is the need to retrieve the information from the preferences without displaying the specific setting activity by performing some:
addPreferencesFromResource(R.xml.preferences);
in another activity.
Is it possible to do it or how else may I retrieve the data without displaying the activity, or is it possible to start the activity without displaying it?
The latter more distressing one is that even in the SettingActivity, when I call that function I recently started getting a crash complaining:
java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.String
Like if something internal were set somehow expecting something. What may I do and in particular how may I reset this sort of hidden structure?
This is my xml file: if I just keep the entries in the former category it does not crash, if I add any field in the latter it does:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/switches"
android:key="switches">
<EditTextPreference
android:key="night_switch_start"
android:summary="#string/ext_night_switch_start"
android:defaultValue="22"
android:title="#string/night_switch_start" />
<EditTextPreference
android:key="day_switch_start"
android:summary="#string/ext_day_switch_start"
android:defaultValue="6"
android:title="#string/day_switch_start"/>
</PreferenceCategory>
<PreferenceCategory
android:title="#string/tokens"
android:key="tokens">
<EditTextPreference
android:key="token_day"
android:summary="#string/ext_token_day"
android:defaultValue="3"
android:title="#string/token_day" />
</PreferenceCategory>
</PreferenceScreen>
Thanks, Fabrizio
For you first question, you can retrieve a preference saved in a PreferenceActivity by calling PreferenceManager#getDefaultSharedPreferences(Context). This is a static method, so you can have in your Activity something like:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
About your second question, I'm not sure what is happening. Try cleaning the project before you run it again.
I answer here to be able to show my code better: this is the first summarizing part of the xml file I suspect might have problems:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="#string/switches"
android:key="switches">
<EditTextPreference
android:key="night_switch_start"
android:summary="#string/ext_night_switch_start"
android:defaultValue="22"
android:inputType="numberDecimal"
android:title="#string/night_switch_start" />
<EditTextPreference
android:key="day_switch_start"
android:summary="#string/ext_day_switch_start"
android:defaultValue="6"
android:inputType="numberDecimal"
android:title="#string/day_switch_start"/>
</PreferenceCategory>
<PreferenceCategory
android:title="#string/tokens"
android:key="switches">
<EditTextPreference
android:key="token_day"
android:summary="#string/ext_token_day"
android:defaultValue="3"
android:inputType="numberDecimal"
android:title="#string/token_day" />
</PreferenceCategory>
</PreferenceScreen>
My code is very simple: the crashing setting part is the following.
public class SettingsActivity extends PreferenceActivity {
private static final String LOG_TAG = "preferences";
public static class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
...
}
While the code loading the string and finding "mostly" empty strings is:
sharedPref= PreferenceManager.getDefaultSharedPreferences(Dashboard.dashboard);
startDayTime=Integer.parseInt(sharedPref.getString("day_switch_start", "")); //correct value
startNightTime=Integer.parseInt(sharedPref.getString("night_switch_start", "")); //empty string thereafter rising an exception.
Also, when I tried leaving just the first two EditText, so not to have the crash, the Setting activity had the correct value for day_switch_start and again an empty string in the night_switch_start field.
When I do getAll() I get the correct values:
{start_night_time=6, day_switch_start=6, notifications_new_message_ringtone=content://settings/system/notification_sound, notifications_new_message=true, second_rate_limit=24.0, third_rate_fare=1.6, token_day=6.0, token_night=6.0, night_switch_start=22, token_holiday=6.0, start_day_time=6, start_night_switch=John Smith, example_checkbox=true, example_text=John Smith, notifications_new_message_vibrate=false, example_list=-1, first_rate_fare=1.1, first_rate_limit=11.0, max_speed_for_time=20.0, sync_frequency=180, second_rate_fare=1.3, timed_fares=27.0, cooperative=}
Yet, when I try to get the value of them either with:
startDayTime=Integer.parseInt(sharedPref.getString("night_switch_start", "")); or
startNightTime=sharedPref.getInt("night_switch_start", 0);
I retrieve an empty string in both cases.
Half of the answer: clearing the cache of the phone removes all the funny fields and does not crash any longer the setting activity, nor the loading of its values by:
startNightTime=Integer.parseInt(sharedPref.getString("night_switch_start", ""));
The old form:
startNightTime=sharedPref.getInt("night_switch_start", 0);
still crashes, notwithstanding the values seem numeral. Not a big deal, though.
The unanswered problem is how to load the settings from the xml file without entering the SettingsActivity performing:
addPreferencesFromResource(R.xml.preferences);
Is it possible to run the function from outside that activity or is it possible to start the activity without visualizing it to the user?

Leaving PreferenceActivity, calling Activity still reads old preference?

I feel I'm missing something obvious, but search took me to several different hits, all of which don't directly access my odd issue.
Have an app with a main activity and a preference activity. Add to that a 'preference' class, which simplifies reading and setting preferences. The main activity has an option menu to get to the preference activity:
Preferences class (included for relevance, same thing happens if I don't use this class to read settings).
public class Preferences
{
public static SharedPreferences getPrefs(Context context)
{
SharedPreferences retCont = PreferenceManager.getDefaultSharedPreferences(context);
return retCont;
}
/* Map Page: Show Satellite */
public static boolean getMapShowSatellite(Context context)
{
return Preferences.getPrefs(context).getBoolean(Preferences.getString(context, R.string.option_showSatellite), false);
}
public static void setMapShowSatellite(Context context, boolean newValue)
{
Editor prefsEditor = Preferences.getPrefs(context).edit();
prefsEditor.putBoolean(Preferences.getString(context, R.string.option_showSatellite), newValue);
prefsEditor.commit();
}
}
PreferencesActivity:
public class AppSettings extends PreferenceActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.app_preferences);
ListPreference stationType = (ListPreference)this.findPreference(this.getString(R.string.option_filterStationType));
stationType.setOnPreferenceChangeListener(this.stationOrderEnable());
}
[...]
}
The last two lines hook up an event to enable/disable other preferences based on one's selection. That works, as expected.
The simple main activity, and related functions:
public class MainMapScreen extends MapActivity
{
private void launchSettings()
{
Intent prefsIntent = new Intent(this.getApplicationContext(), AppSettings.class);
this.startActivity(prefsIntent);
}
#Override
protected void onResume()
{
super.onResume();
Preferences.getMapShowSatellite(); // <-- Returns previous value.
// Re-start the MyLocation Layer from tracking.
this._mapView.requestLayout();
}
[...]
}
Okay, so what happens is, let's say we run the app. At app load, the getMapShowSatellite() returns True. Go into the PreferenceActivity, and change that option to False. Exit the PreferenceActivity by hitting the Back button. At this time, the main activity's onResume() is called. Getting the getMapShowSatellite() at this point returns the previous setting of True. Exiting and relaunching the app will then finally return the False expected.
I'm not calling .commit() manually - and don't think I need to, sicne the setting IS saving, I'm just not getting update values.
What'm I missing? :)
--Fox.
Edit 2: Small update. I thought the issue may be the static calls - so temporarily I changed over my Preferences class (above) to be a instantiated class, no more static. I also added the following code to my onResume() call in the main activity:
//Try reloading preferences?
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String test = sp.getString(Preferences.OPTION_FILTERSTATIONTYPE, "---");
Log.e("BMMaps", test);
What is logged at this point, from leaving the PreferenceActivity, is the old setting. Manually reading the preferences file shows me that the .xml file is getting updated with the user's new setting.
Since it's not obvious, I am hooked into Google's Maps API. Because of this, I had to specify two ifferent processes - one for the Main activity (this one) and another for an activity not related to this issue. All other activities, including the PreferencesActivity have no specified android:process="" in their definition.
Edit 3:
As requested, here's the data preferences file:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<string name="com.tsqmadness.bmmaps.filterStationType">V-?</string>
<boolean name="com.tsqmadness.bmmaps.deviceHasLocation" value="false" />
</map>
And here is the Preference storage XML file:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Map Options">
<CheckBoxPreference android:key="com.tsqmadness.bmmaps.mapShowSatellite" android:order="1" android:summary="Whether or not to show satellite imagery on the map." android:summaryOff="Standard road map will be shown." android:summaryOn="Satellite imagery will be show." android:title="Show Satellite Layer?" />
<CheckBoxPreference android:key="com.tsqmadness.bmmaps.mapShowScale" android:order="2" android:summary="Whether or not to show the distance bar on the map." android:summaryOff="The distance bar will not be shown on the map." android:summaryOn="The distance bar will be shown on the map." android:title="Show Map Scale?" />
<CheckBoxPreference android:defaultValue="false" android:key="com.tsqmadness.bmmaps.useMetric" android:order="3" android:summary="Whether to use Metric os SI values." android:summaryOff="SI units (mi/ft) will be shown." android:summaryOn="Metric units (km/m) will be shown." android:title="Use Metric?" />
<ListPreference android:dialogTitle="Station Load Delay" android:entries="#array/static_listDelayDisplay" android:entryValues="#array/static_listDelayValues" android:key="com.tsqmadness.bmmaps.mapBMDelay" android:negativeButtonText="Cancel" android:order="4" android:positiveButtonText="Save" android:summary="The delay after map panning before staions are loaded." android:title="Delay Before Loading" />
</PreferenceCategory>
<PreferenceCategory android:title="Control Station Filter">
<ListPreference android:dialogTitle="Station Type" android:entries="#array/static_listStationTypeDisplay" android:entryValues="#array/static_listStationTypeValues" android:key="com.tsqmadness.bmmaps.filterStationType" android:negativeButtonText="Cancel" android:positiveButtonText="Save" android:summary="The station type to filter on." android:title="Station Type" android:order="1" />
<ListPreference android:dialogTitle="Select Station Order" android:entries="#array/static_listStationHOrderDisplay" android:entryValues="#array/static_listStationHOrderValues" android:key="com.tsqmadness.bmmaps.filterStationOrder" android:negativeButtonText="Cancel" android:positiveButtonText="Save" android:summary="Station Order to filter by." android:title="Station Order" android:order="2" />
<ListPreference android:dialogTitle="Select Station Stability" android:entries="#array/static_listStationStabilityDisplay" android:entryValues="#array/static_listStationStabilityValues" android:key="com.tsqmadness.bmmaps.filterStationStability" android:negativeButtonText="Cancel" android:positiveButtonText="Save" android:summary="Station stability to filter by." android:title="Station Stability" android:order="3" />
<CheckBoxPreference android:key="com.tsqmadness.bmmaps.filterNonPub" android:summaryOff="Non-Publishable stations will not be shown." android:defaultValue="false" android:summaryOn="Non-Publishable stations will be shown on the map." android:order="4" android:title="Show Non-Publishable" />
</PreferenceCategory>
<Preference android:key="temp" android:title="Test" android:summary="Test Item">
<intent android:targetClass="com.tsqmadness.bmmaps.activities.MainMapScreen" android:targetPackage="com.tsqmadness.bmmaps" />
</Preference>
</PreferenceScreen>
When changing the filterStationType parameter, and hitting the back button out of PreferenceActivity changes the preferences file from the above from V-? to H-?, as it should. However, reading the value from the SharedPreferences on the main activity still gives the V-?, until app restart. Ah, also, I have a OnPreferenceChangeListnener() in the PreferenceActivity, and that is called when the value changes.
Final Edit: Apparently, it's the use of named android:process for the given activity. This is needed for Google maps API to allow two separate MapActivitys in the same app use different settings. If the PreferenceActivity is moved to the same named-process, then the code above, reading the setting in the onResume() returns the correct value.
Since everything checks out, my guess is that you have a typo, or incorrect or undefined, key in your PreferenceActivity's app_preferences.xml file for the key R.string.option_showSatellite (whatever that string is). This would result in two keys with [unbeknownst to you] different names that you think point to the same value. Really, the prefs activity is using one key and your Preference class is using the other -- resulting in two different keys and two different values.
Double check your keys. Make sure you are not also using the literal "R.string.options_showSatellite" as the key in the xml file, but rather, the actual string. If you want to use the localized version then #string/options_showSatellite would work for the key. However, keys need not be localized.
If you're curious, this can be double checked by opening the preference file that is created by the preference manager in your app's data directory in a standard text editor.
onResume you have to get a fresh reference to SharedPreferences, otherwise you're just using an object that is already in memory (and has your old values)
EDIT:
Rather than down-voting answers that you don't understand, why not ask for clarification instead?
What I mean is that you should pull your Prefs the correct way (rather than how you're doing it)...
SharedPreferences sp = getSharedPreferences(getPackageName(), MODE_PRIVATE);
And then see what happens.

Android - How to change texts in the preferences activity dynamically?

Hello fellow programmers, I have a little problem with Preferences activity.
http://developer.android.com/reference/android/preference/PreferenceActivity.html
I've got just one preference category and a listPreference:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceCategory android:title="#string/basic_settings" >
<ListPreference
android:defaultValue="70"
android:entries="#array/listArray"
android:entryValues="#array/listValues"
android:key="updates_interval"
android:persistent="true"
android:summary="#string/SOME_SUMMARY"
android:title="#string/SOME_TITLE" />
</PreferenceCategory>
I need to have the selected value (the default one or the user defined one) written in the summary of the listPreference, for example:
We will have at least 70 characters.
How can I do this from the code?
Any help is appreciated
Try like this..
Preference customPref = (Preference) findPreference("updates_interval");<-- your preferences key
customPref.setSummary("desired string");
here is a short example:
Preference etp = (Preference) findPreference("the_pref_key");
etp.setSummary("New summary");
This requires that you display your preferences either from a PreferenceActivity or from a PreferenceFragment, since findPreference() is a method of these classes. You most likely do that already.
To change the summary every time the user changes the actual preference, use a OnPreferenceChangeListener and check if the relevant key changed in the callback. After it has changed, just edit the summary like above.
You can create a subclass of ListPreference in which you set an OnPreferenceChangedListener from which you will have access to the new value, and set the text on your ListPreference. I think the setSummary() function on the ListPreference will update the text under the name of the preference. If that doesn't work you can also override getView() to implement your own custom view for the Preference on which you can set the text directly.

Categories

Resources