EditTextPreference Password field - android

I am trying to include an EditTextPreference for a password, and want to hide the characters and show asterisks, but not able to do it.
this is my preference.xml file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Scenes">
<DropDownPreference
android:key="scenario"
android:title="Select scene"
android:summary="%s"
android:entries="#array/scenarios"
android:entryValues="#array/scenarios_values" />
</PreferenceCategory>
<PreferenceCategory
android:title="Demo">
<SwitchPreference
android:key="connect_on_startup"
android:title="Auto Connect"
android:defaultValue="false"
android:summary="Connect automatically at startup" />
<EditTextPreference
android:title="UserName"
android:key="username"
android:summary="Please provide your Username"
android:maxLines="1"
android:singleLine="true">
</EditTextPreference>
<EditTextPreference
android:inputType="textPassword"
android:title="Password"
android:key="password"
android:summary="Please provide your Password"
android:password="true" />
</PreferenceCategory>
What can I be doing wrong?
Thanks!

In Androidx this must be done with the following two step process
EditTextPreference passwordEditText = (EditTextPreference)findPreference("password");
passwordEditText.setOnBindEditTextListener(new androidx.preference.EditTextPreference.OnBindEditTextListener() {
#Override
public void onBindEditText(#NonNull EditText editText) {
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
}
});
passwordEditText.setSummaryProvider(new Preference.SummaryProvider() {
#Override
public CharSequence provideSummary(Preference preference) {
int valueSize= PreferenceManager.getDefaultSharedPreferences(getContext()).getString(preference.getKey(),"").length();
String newSummary="";
for (int i = 0; i < valueSize; i++) {
newSummary+="*";
}
return newSummary;
}
});

android:password="true" is deprecated you should use android:inputType="textPassword" instead.

If you need to hide text shown in the summary, it should be done outside of the xml file. You probably have code template from android studio that contains implementation of Preference.OnPreferenceChangeListener. Inside of it there is callback method that is called when settings are changed. By default it changes summary to the incoming value:
preference.setSummary(stringValue);
Instead of it, you need to check if the preference is your password and skip setting the summary, something like this:
if ("password".equals(preference.getKey())) {
preference.setSummary("***");
} else {
preference.setSummary(stringValue);
}

Related

Show current value of SeekBarPreference

I'm developing an Android app and I have added several SeekBarPreference elements in my settings page to work as sliders. They have different max values, and I think it's a problem that you cannot see what value you selected. So I wonder what's the best way to show the current value of SeekBarPreference in a preferences activity. Right now I think maybe the summary of the seekBarPreference-element is the best way, but I am open to other suggestions.
This is how the settings activity looks like:
Here's the code I tried so far:
public static class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
((SeekBarPreference) findPreference(R.id.seekRounds)).setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
final int progress = Integer.valueOf(String.valueOf(newValue));
preference.setSummary(String.format("Current value: %d", progress));
}
});
}
And currently I get the error Cannot resolve symbol 'seekRounds'.
Here's my root_preferences.xml:
<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory app:title="#string/main_settings_header">
<SeekBarPreference
app:key="seekRounds"
android:title="Number of rounds"
android:summary="This is the summary"
android:max="2"
android:progress="0"
android:theme="#style/Base.Widget.AppCompat.SeekBar"
/>
</PreferenceCategory>
Found the answer. Just add app:showSeekBarValue="true" to your SeekBarPreference.
Like this:
<SeekBarPreference
app:key="seekRounds"
android:title="Number of rounds"
android:summary="This is the summary"
android:max="2"
app:showSeekBarValue="true"
android:progress="0"
android:theme="#style/Base.Widget.AppCompat.SeekBar"
/>

How to combine preference and CardView

I am trying to make a setting menu with cardView and Preference, but I am not sure how to combine these two... Here's the screenshot and I want to move the ListPreference and PreferenceScreen entries into the setting cardView above.
Here's the preference.xml The layout/setting_group is a cardview
<PreferenceScreen
android:layout="#layout/setting_title_top"
android:title = "Padding" />
<PreferenceScreen
android:layout="#layout/setting_group">
</PreferenceScreen>
<ListPreference
android:id="#+id/list"
android:layout="#layout/setting_menu"
android:dialogTitle = "title one"
android:key = "key_1"
android:title = "ListPreference"
android:summary = "Summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListPreference>
<PreferenceScreen
android:layout="#layout/setting_menu"
android:key="Key two"
android:summary="Summary"
android:title="PreferenceScreen" />
<PreferenceScreen
android:layout="#layout/setting_title"
android:title = "Another Setting" />
The PreferenceScreen is a container, you are opening and closing him with noting inside.
You can create preferences like that:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
android:capitalize="words"
android:defaultValue="#string/default_value" <!--The initial state of the preference -->
android:key="#string/pref_key" <!-- The key for retrieve the preference -->
android:singleLine="true"
android:title="#string/pref_title" /> <!-- The title who is showing above the preference -->
</PreferenceScreen>
Edit: Create a xml file to with the layout of your preferences and create and Activity, example:
public class SettingsActivity extends PreferenceActivity
implements Preference.OnPreferenceChangeListener {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_general);
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_key))); // Is deprecated, but still a good option to no need change the values manually.
}
private void bindPreferenceSummaryToValue(Preference preference) {
preference.setOnPreferenceChangeListener(this);
onPreferenceChange(preference, PreferenceManager
.getDefaultSharedPreferences(preference.getContext())
.getString(preference.getKey(), ""));
}
#Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
preference.setSummary(stringValue);
return true;
}
}

Unable to show Snackbar from PreferenceFragment

I want to use Snackbar onSharedPreferenceChange() inside my PreferenceFragment hosted by a Activity.
preference.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="ll_main">
<PreferenceCategory android:title="#string/push_notification">
<SwitchPreference android:title="#string/transectional_notification"
android:key="trans_not"
android:defaultValue="true"
android:summary="#string/get_notified_about_any_changes_related_to_your_transections"/>
<SwitchPreference android:title="#string/promotional_notification"
android:key="prom_not"
android:defaultValue="true"
android:summary="#string/get_notified_about_referalls"/>
</PreferenceCategory>
<PreferenceCategory android:title="#string/other_notifications">
<SwitchPreference android:title="#string/email_notifications"
android:defaultValue="true"
android:key="email_not" />
<SwitchPreference android:title="#string/sms_notifications"
android:defaultValue="true"
android:key="sms_not"/>
</PreferenceCategory>
<PreferenceCategory android:title="#string/recommendation_setting">
<com.til.recharger.widgets.PhoneEditTextPreference
android:title="#string/mobile_no"
android:inputType="number"
android:summary="8587035342"
android:key="mob_no_edit"/>
<ListPreference android:title="#string/operator"
android:summary="Airtel"
android:key="operator_list"/>
<ListPreference android:title="#string/circle"
android:summary="New Delhi"
android:key="circle_list"/>
<ListPreference
android:title="Plan type"
android:entries="#array/listentries"
android:entryValues="#array/listentries"
android:summary="Postpaid"
android:defaultValue="Prepaid"
android:key="plan_key"/>
</PreferenceCategory>
<PreferenceCategory android:title="#string/plan_info">
<com.til.recharger.widgets.NumberPickerPreference
android:title="#string/bill_cycle"
android:summary="2"
android:persistent="true"
android:key="bill_cycle_edit"/>
<com.til.recharger.widgets.CustomEditTextPreference
android:title="#string/monthly_data_limit"
android:summary="%s"
android:inputType="number"
android:key="monthly_limit_edit"/>
<ListPreference
android:title="#string/preffered_network_type"
android:entries="#array/mobiledataoptions"
android:entryValues="#array/mobiledatavalues"
android:summary="3G"
android:defaultValue="3G"
android:key="pref_data_list"/>
</PreferenceCategory>
</PreferenceScreen>
My function for showing snackbar is :
public void hideKeyWithError(final String error) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
AppUtils.hide_keyboard(getActivity());
Snackbar.make(view,error,Snackbar.LENGTH_LONG).show();
}
}, (300));
}
I don't know what to pass as 'view' to snackbar to show it.
PS : I tried passing PreferenceScreen to Snackbar, But didn't work out. Thanks in Advance.
Try this for get view
View view = getView();
So the code is.
public void hideKeyWithError(final String error) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
View view = getView();
AppUtils.hide_keyboard(getActivity());
Snackbar.make(view,error,Snackbar.LENGTH_LONG).show();
}
}, (300));
}
View param of Snackbar.make() means a view on which Snackbar can find parent to display Snackbar on the top of screen
Snackbar will try and find a parent view to hold Snackbar's view from the value given to view
Link

getString from SharedPreferences always return default value for one value

i'm using this code in an activity
protected void onResume() {
super.onResume();
prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
System.out.println(connections);
String dir = prefs.getString("download_directory", "Download");
System.out.println(dir);
}
My preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Downloads">
<EditTextPreference
android:key="download_directory"
android:title="#string/download_destination"
android:summary="#string/download_description"
android:defaultValue="FacebookPhotos"
android:dialogTitle="#string/download_destination" />
<CheckBoxPreference android:key="subdir_album"
android:title="#string/download_album"
android:summary="#string/download_album_description" />
</PreferenceCategory>
<PreferenceCategory android:title="Gallery">
<EditTextPreference
android:key="connections"
android:title="Concurrent connections"
android:numeric="integer"
android:maxLength="2"
android:defaultValue="7"
android:dialogTitle="Concurrent connections" />
</PreferenceCategory>
</PreferenceScreen>
edited by
public class Preferences extends PreferenceActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
The problem is, when i get "connections" value it gives me the correct int, instead the getString for key "download_directory" always gives me the default value "Download"
getString for key "download_directory" always gives me the default
value "Download"
This can only happen if "download_directory" is not set at all!

UI refreshing at PreferenceActivity

I have a PreferenceActivity where I represent a setting UI, when I press the first checkbox then the second checkbox becomes visible. On the other hand, when the first checkbox is unchecked the second checkbox is hidden(and gets a default value to false).
The problem is that I need my UI screen to be refreshed the moment I check(or uncheck) the first checkbox because right now with that code I need to go to another Activity (e.g when I press the back button) and then get back to the PreferenceActivity again in order to see the changes on my UI.
Code for PreferenceActivity:
public class SetPreference extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.checkboxpref);
}
#Override
protected void onStart() {
super.onStart();
Preference checkbox = findPreference("checkBox_Schedule");
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
if (prefs.getBoolean("checkBox", true)) {
} else {
((PreferenceGroup) findPreference("category_second"))
.removePreference(checkbox);
SharedPreferences.Editor geted = prefs.edit();
geted.putBoolean("checkBox_Schedule", false);
geted.commit();
}
}
#Override
protected void onResume() {
super.onResume();
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
}
}
Code for XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:summary="Username and password information"
android:title="Login information" >
<EditTextPreference
android:key="username"
android:summary="Please enter your login username"
android:title="Username" />
<EditTextPreference
android:inputType="textPassword"
android:key="password"
android:summary="Enter your password"
android:title="Password" />
</PreferenceCategory>
<PreferenceCategory
android:summary="Username and password information"
android:title="Settings"
android:key="category_first" >
<CheckBoxPreference
android:key="checkBox"
android:summary="On/Off"
android:title="Keep me logged in" />
<ListPreference
android:entries="#array/listOptions"
android:entryValues="#array/listValues"
android:key="listpref"
android:summary="List preference example"
android:title="List preference" />
</PreferenceCategory>
<PreferenceCategory
android:summary="schedule"
android:key="category_second"
android:title="Schedule" >
<CheckBoxPreference
android:key="checkBox_Schedule"
android:summary="On/Off"
android:title="Keep me logged in" />
</PreferenceCategory>
</PreferenceScreen>
Thank you for your time.
Ok then just restart the activity when the checkbox is selected/deselected:
startActivity(new Intent(PreferencesActivity.this, PreferencesActivity.class));
finish();
Consider using onPreferenceSelectedListener for the first checkbox to detect the act of checking/unchecking the preference.

Categories

Resources