I can't seem to understand why I keep getting the "default" string show up when I grab input from the EditTextPreference.
<EditTextPreference
android:title="#string/settings_title_signature"
android:summary="#string/settings_enter_signature"
android:singleLine="true"
android:key="edit_signature_key"
/>
I never seem to get what the textfield has using Sharedpreferences. It just defaults to the "default", and not what should be in the key.
SharedPreferences myPreference = PreferenceManager.getDefaultSharedPreferences(this);
String sig = myPreference.getString("edit_signature_key", "default");
Make sure you are using the EditTextPreference inside of a Preference Screen or Preference Fragment. Sounds like you are just using a standalone EditTextPreference.
If you are using it in the right container, please update your post with all of your code and I will be able to help.
(had to put this as an answer instead of a comment because I don't have enough rep)
Related
all! I'm currently working on an assessment that's due in by tomorrow wherein I need to create a working application in Android Studio which features, amongst other things, a menu-adjustable option for the size of the text. The app itself is a 'quote-of-the-day' style application. Below is the code I've written up for the actual menu option.
<EditTextPreference
android:defaultValue="14"
android:key="edit_text_preference_1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="Text Size"
android:inputType="number"/>
While this works, and I am able to input a number into the settings area, I have no idea of how to link the value inputted through to adjust the size of my Text in the Textview window.
I apologize that this doesn't exactly follow the normal criteria, but I honestly have no results to show per se on this point. Any help would be appreciated.
You can use SharedPreference for saving some value that will be used in another activity.
Here example based from documentation
To save:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.yourKeyStringName), yourValue);
editor.commit();
To read:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValueIfKeyDoesntExist = getResources().getInteger(R.string.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.yourKeyStringName), defaultValueIfKeyDoesntExist);
https://developer.android.com/training/data-storage/shared-preferences.html
android:inputType is not working on EdidtTextPreference from support library. Is changing the input type even possible with this preference? I want a preference with a numeric keyboard. More specifically I will want to set a custom Keyboard, but I can't seem to even open the default one with this preference. If it is not possible with EditTextPreference, can someone give me an idea how I would go about doing what I desire?
<android.support.v7.preference.EditTextPreference
android:key="key"
android:title="title"
android:maxLines="1"
android:dialogMessage="title"
android:inputType="number" />
Actually, the solution is quite easy. Since android:inputType="number" didn't seem to work, I went with this:
PreferenceFragment.java
#Override
public void onResume() {
EditTextPreference preference = (EditTextPreference) findPreference("key");
EditText et = preference.getEditText();
if (et != null) {
et.setInputType(InputType.TYPE_CLASS_NUMBER);
//in fact, you can do whatever you can normally with an EditText here
}
}
Although I'm not sure if this is how it works with the latest androidx Preference.
I'm playing in the settings someone else made for my app and I want to uncheck a CheckPreferenceBox when the user cannot properly connect to dev servers.
I call my CheckPreferenceBox this way:
CheckBoxPreference mCheck = (CheckBoxPreference) findViewById(R.id.prefs_dev_mode);
mCheck.setChecked(false);
And here is the part in my .xml file related to it:
<CheckBoxPreference
android:id="#+id/prefs_dev_mode"
android:key="SP_DEV_SERVER"
android:title="#string/prefs_server_title"
android:summary="#string/prefs_server_summary"
android:defaultValue="false"/>
Do you see what could be the error?
Thanks
The problem that you face, is that CheckBoxPreference is not actually a View. It is a Preference. Therefore the findViewById function will not work for you.
You cannot interact with this element like a View.
If you want to set it unchecked, you should so something like this:
CheckBoxPreference pref = (CheckBoxPreference) getPreferenceScreen().findPreference("SP_DEV_SERVER");
pref.setChecked(false);
I am working on a small project that requires a details screen where the user inputs his details and they are permanently stored. The user must have the option to change the details as well if he needs to do so. I looked into the saved preferences library however it does not seem to offer such functionality.
To give a visual idea of what is required, something like this screen should be fine:
http://www.google.com.mt/imgres?start=97&num=10&hl=en&tbo=d&biw=1366&bih=643&tbm=isch&tbnid=aQSZz782gIfOeM:&imgrefurl=http://andrejusb.blogspot.com/2011/10/iphone-web-application-development-with.html&docid=YpPF3-T8zLGOAM&imgurl=http://2.bp.blogspot.com/-YRISJXXajD0/Tq2KTpcqWiI/AAAAAAAAFiE/-aJen8IuVRM/s1600/7.png&w=365&h=712&ei=rbX6ULTDCOfV4gTroIDoCg&zoom=1&iact=hc&vpx=834&vpy=218&dur=2075&hovh=314&hovw=161&tx=80&ty=216&sig=108811856681773622351&page=4&tbnh=155&tbnw=79&ndsp=35&ved=1t:429,r:4,s:100,i:16
Any help is much appreciated. Thanks in advance
You could easily use Shared Preferences to store user's details. Everytime the Preference screen is opened the stored data can then be extracted from the Shared Preferences and presented to the user for edit. ONce the edit is done the new data can be updated back in the the Shared Preferences.
Also look at this thread to see how this can be done.
Using SharedPreferences would be perfect for this kind of small amount of data which you want to store persistently.
// 'this' is simply your Application Context, so you can access this nearly anywhere
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
To obtain from the preferences:
// You can equally use KEY_LAST_NAME to get the last name, etc. They are just key/value pairs
// Note that the 2nd arg is simply the default value if there is no key/value mapping
String firstName = prefs.getString(KEY_FIRST_NAME_CONSTANT, "");
Or to save:
Editor editor = prefs.edit();
// firstName being the text they entered in the EditText
editor.putString(KEY_FIRST_NAME_CONSTANT, firstName);
editor.commit();
You can achieve such functionality using SharedPreferences Class in android.
public void onCreate(Bundle object){
super.onCreate(object);
// Initialize UI and link xml data to java view objects
......
SharedPreferences myPref = getPreferences(MODE_PRIVATE);
nameView.setText(myPref.getString("USER_NAME", null));
passView.setText(myPref.getString("PASSWORD", null));
}
public void onStop(){
super.onStop();
if (isFinishing()) {
getPreferences(MODE_PRIVATE).edit()
.putString("USER_NAME", nameView.getText().toString())
.putString("PASSWORD", passView.getText().toString())
.commit()
}
}
I use CheckBoxPreference in my PreferenceActivity to set a value. Later on, I want to check that value from Receiver and/or Service. findPreference() method is not available from that context. I know, that this preference value is stored in SharedPreferences anyway, but what is the key? How could I get the value of the checkbox?
I know, that this preference value is stored in SharedPreferences anyway, but what is the key?
Whatever value you have for android:key in your preference XML.
How could I get the value of the checkbox?
Call PreferenceManager.getDefaultSharedPreferences() to get the SharedPreferences, then call getBoolean() with the key you used in android:key.
In your preferences XML you'll have something like this:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference android:enabled="true"
android:title="#string/s_pref" android:key="#string/pref"
android:defaultValue="#string/d_pref"></CheckBoxPreference>
</PreferenceScreen>
Your strings.xml would have something like this:
<string name="pref">my.package.PREF</string>
<string name="s_pref">Prompt</string>
<string name="d_pref">true</string>
Your Activity's onCreate() would have something like this:
prefs = PreferenceManager.getDefaultSharedPreferences(this);
pref = prefs.getBoolean(getString(R.string.pref), true));
If you want to do something when someone changes the preferences, add an onActivityResult() to your activity and start the preferences activity with startActivityForResult(). When onActivityResult() is invoked with whatever result code you want to indicate a change in preferences, you can do another getDefaultSharedPreferences().
The shared preferences framework automatically persists the data... you don't have to actively deal with it yourself, though you can if you wish with an OnPreferenceChangeListener in the preferences activity
The only thing I would add to CommonsWare's answer is, since you mentioned a service, you can put whatever preferences the service needs to know about in its Intent extras. For example:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
Intent intent = new Intent(this, MyService.class);
intent.putExtra("mypref", prefs.getString("mypref", ""));
startService(intent);
try to write this in your service
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplication());
and here specify the key that you have used in xml
if(preferences.getBoolean(your key ,true))
{
hope this help .