How to visually show that a preference options are disabled? - android

I have created a 'Preferences' file in which I have a CheckboxPreference . When I uncheck the CheckboxPreference its dependants are getting disabled. However, it doesn't leave any visual clue that these dependants are disabled. How to provide that?

Try to show some alert dialogue or Toast message in onSharedPreferenceChangedListener() in your preference activity.

Try to get a reference to your Preference in your PreferenceActivity using findPreference method then set preference.setShouldDisableView() to true and preference.setEnabled() to false and see if it helps.

Related

SwitchPreference not changing state after setting onPreferenceChangeListener

I have an activity which extends PreferenceActivity and i have a class which extends PreferenceFragment.
After I initiate the switchPreference variable (inside the Fragment) I set OnPreferenceChangeListener to it.
The problem is when I do set a listener I cant change the state of the switch button (it remain at the same position).
If I disable the statement where Im setting the listener the switch button works fine and also the state is saved.
I also have a wrapper class for the sharedPreferences which I want to save a data into it on switchpreference change.
Did anyone encounter such a weird behavior?
Any help will be appreciate.
Well, I found the the "onPreferenceChange" function returned false and when I changed it to true it worked.
Thanks to all who was looking for answering me.

Enabling EditText OnClick when it is already disabled

I've noticed that I cannot use the EditText.setOnClickListener() when EditText.setEnabled(false) has been called before.
Is there any way to enable the EditText after clicking on it?
By calling EditText.setEnabled(false) you are disabling the EditText So what will you do by implementing EditText.setOnClickListener(), You can't do like this
public void setEnabled (boolean enabled) --
Set the enabled state of this view. The interpretation of the enabled state varies by subclass.
See the official Docs --
http://developer.android.com/reference/android/view/View.html#setEnabled%28boolean%29
If you want to use EditText.setOnClickListener()
then write EditText.setEnabled(true) before EditText.setOnClickListener()

CheckBoxPreference Default Value

I have a CheckBoxPreference defined as follows:
<CheckBoxPreference
android:defaultValue="true"
android:key="prefVisible"
android:summary="#string/pref_visible_summary"
android:title="#string/pref_visible" >
</CheckBoxPreference>
My application uses this preference to control the visibility of a view. When I first start my application (on a new wiped emulator) the view is not shown. However, when the I go to the preferences screen (activity) the checkbox is shown as checked.
Does this mean that the defaultValue attribute is not actually setting the preference but rather it's just setting the value of the checkbox if there is no underlying data (as would be the case on a brand new install). And does this also mean that the preference is set only after the user enters/exits the preferences screen (activity) for the first time, otherwise it's undefined?
Note that in order to get my app to work the way I intended it to work I relied on the default value argument to the preferences getter method as follows:
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean isVisible = sharedPrefs.getBoolean("prefVisible", true); // default = true
This leaves me a bit confused as to why there are 2 ways to control the default value of a preference: defining it in the Xml or providing the default value in the getBoolean method.
No the preferences can be set if you call PreferenceManager.setDefaultValues. So if you call this when the first time the app is launched then your view will be shown.
You can read more at http://developer.android.com/guide/topics/ui/settings.html

Hide item in Android preferences menu

my Application has a Preferences menu. there are 2 checkbox: "Enable Music", and "Show Hints". What I want is, There is Music List will show if check "Enable Music", else Music list will not enable. My code is:
boolean OPT_MUSIC_DEF = true;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
if (!PreferenceManager.getDefaultSharedPreferences(this).getBoolean(OPT_MUSIC, OPT_MUSIC_DEF)){
Preference list = findPreference("music_list");
list.setEnabled(false);
}
}
There is no error, but it work incorrect. When I unCheck "Enable Music" Music List did not hide right away, I must back and go to menu again to see Music List is hide.
Any way to make it hidden directly after uncheck "Enable Music" item?
thank for help me!
There is built in functionality to do exactly what you're trying to do. In your preferences.xml file set the dependency for the music_list preference to depend on the music_enable preference.
For example:
<android.preference.CheckBoxPreference
android:key="music_enable"
android:defaultValue="false"
android:title="Enable Music"
/>
<android.preference.ListPreference
android:key="music_list"
...
android:dependency="music_enable"
/>
Whenever the user checks the "music_enable" check box the "music_list" will be enabled; when they uncheck the "music_enable" check box the "music_list" will be disabled.
Make sure to call onContentChanged() right after disabling the preference.
If you want to force refresh the appearance of your preference, just do:
Preference myPref = findPreference("mypref");
myPref.setDefaultValue((int)(Math.random()*1000); // you can also backup the value before and reset it after if you need to keep a default value
Not the perfect solution, but it will surely force Android to refresh your preferences.
BTW, I tried Paul-Jan solution but unluckily it didn't work, it seems that onContentChanged() will just do nothing if no value is changed.

How to create a popup window with spinner for android?

I would like to create a popup window which will appear the first time the user opens the application and ask the user to select the setting within a spinner (example something close like the picture below)
(source: mikesandroidworkshop.com)
Also, I would like it to popup automatically rather then having the need to press on a button. Is it possible to do so?
Please help. thanks a lot. =)
That is called a Dialog. See this page for more info http://developer.android.com/guide/topics/ui/dialogs.html.
To create the one like you showed, look under the Custom Dialog section. Basically create the layout you want to see inside of the dialog in an XML file and use setContentView as you would with an activity.
If you want it to pop up when an activity starts just put the code in the onStart method in your activity.
Just call it onCreate, for example. And use shared preferences to check for first time launch.
private void showSettingsPopUpOnFirstTimeLaunch(){
SharedPreferences settings = this.getSharedPreferences("default", 0);
boolean firstStart = settings.getBoolean("firstStart", true);
if(firstStart){
showPopUp(); //
}
}
And when closing popup just change flag in SharedPreferences (you would probably will want to make popup not-cancelable).
SharedPreferences settings = this.getSharedPreferences("default",
0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstStart", false);
editor.commit();

Categories

Resources