disable a menu permenantly in android - android

I would like to be able to disable a menu item after it is chosen. That is, the menu item is available only at the first time it is set. Then onwards it should be completely disabled. How can I do this?
I tried the following:
MenuItem mi=menu.findItem(R.id.item1));
And used this code to disable it after "firstrun"
mi.setEnabled(false);
However, once I force stop the App and go back to it again. The menu item is enabled.
How can I prevent this and disable it permenanly?
Thanks.

You have to persist your data somewhere. In this case, I'd suggest using Shared Preferences which is the preferred way to store simple booleans and other small data, each with a specific key. In your case, once the user has clicked on the menu item, call
// Where this is a Context such as your Activity
SharedPreferences sp = PreferenceManager.getSharedPreferences(this);
sp.edit().putBoolean("MENU_CLICK", true).apply();
// Use commit() in place of apply() if you support pre-Gingerbread devices
Then in your onPrepareOptionsMenu(), retrieve the shared preference by using
SharedPreferences sp = PreferenceManager.getSharedPreferences(this);
// default false to show on first run
final boolean haveClickedMenu = sp.getBoolean("MENU_CLICK", false);
mi.setEnabled(!haveClickedMenu);

Related

SharedPreferences dependencies

I wanted to create ListPreference which, when changed will also reload values of others ListPreference objects. I tried to do that by calling:
if(key.equals("important_pref")) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("some_pref", "some_val");
editor.apply();
}
in onSharedPreferenceChanged function.
It does the work but I need to reload preferences screen to see the efect. Is there a way to avoid that and reload values instantly?
My guess is that I can't do this that way, because the first commit needs to be ended before changing something else.
This is a very common requirement that is unfulfilled I'm afraid. You either have to reload the screen by hacks like setPreferencesScreen(null); or (better) register a SharedPreferencesChanged listener and update the prefs manually. See : update preferences values after changing them programatically
In your case you should add if clauses in your existing SharedPreferencesChanged listener to "hear" the change in the "some_pref" preference and update the displayed value manually.

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();

is it possible to Set selected value of second select based on specific values chosen from first select in android?

I have two Tabbar, First is for the ApplicationTAB View and second is for the SettingTAB.
Now i have to set the Sound On/Off from the SettingAtb. If i have selected the Sound On then on ApplicationTab there is sound to be Play. And if i have Selected Off then the Sound should not be play on the ApplicationTab. So what should i have to do to implement like this.
And if it is possible then let me know how i can able to do this settings.
Please Help me regarding this logic.
Thanks.
As Googling i got the proper word for it.
I want to implement to set the ToggleButton Value from one tab to another tab to be affected.
Means i want to set the Something like Sound On/off from one tab for the another tab.
If there is any Demo Project then let me know.
From what I undertood from your question you are trying to pass a value between 2 activities according to my understanding. And your sound on/off should be stored in shared preferences so that you can later refer them when the app starts the next time.
This is a clear explanation of Shared Preferences from google. This will allow you to store data as long as the app is not deleted.
So when you set the sound on/off flag you can save it in the sharedpreferences and when you go back to the first tab, you can fetch the flag from shared preferences in the onCreate and do what is necessary for the sound.
Here is a simple tutorial from a SaiGeetha's blog
you can try this
getSharedPreferences(Settings.SHARED_PREFS_NAME, MODE_PRIVATE)..getString(PREF_NAME, defaultValue);
or use other type of storage
documentation here
You can have interface defined in the tab host class. Implement the interface and pass it to the settings tab. Call its callback method from the settings tab whenever the setting changes.
In the callback implementation, stop/start the sound.
class tabhost
public interface soundSettingsCallback {
public void onSoundSettingsChanged(boolean bStop);
};
apptab.setSoundSettingCallback(new soundSettingsCallback {
public void onSoundSettingsChanged(boolean bStop) {
// stop/start sounds.
}
});

Categories

Resources