Hide item in Android preferences menu - android

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.

Related

How to change state of swipe button from code?

I am using a swipe button from com.ebanx:swipe-button library in my application and I wish to change the state of the swipe button to enable (based on the information recieved via another Bluetooth device) when I open the button's activity. ie: Without any user input I have to change swipe button's state to enable !
You can use toggleState()
SwipeButton mSwipeButton; = findViewById(R.id.my_swipe_button);
mSwipeButton.toggleState();
if you use an older version where toggleState is not available, use collapseButton(); or expandButton(); to collapse or expand the swipe button
There are two issues with the library you're using, first is coding bug, second is wrong documentation, but that's not the case.
to make the button active:
SwipeButton swipe_btn = findViewById(R.id.swipe_btn);
swipe_btn.setEnabled(true);
now by default, the button state is closed and you can change that in the xml file i.e the layout where you created the button, you will see something like below:
<com.ebanx.swipebtn.SwipeButton
app:initial_state="disable" //change to enable will make button open by default
app:has_activate_state="true"
/>
Finally to monitor the state of the button, you will have to listen to the state changes like below:
swipe_btn.setOnStateChangeListener(new OnStateChangeListener() {
#Override
public void onStateChange(boolean active) {
Toast.makeText(MainActivity.this, "IS "+active, Toast.LENGTH_LONG).show();
}
});
if active, the button is open, else it's close.
Note: When I say open, I mean the button is toggledOn, and when I say close, it means the other way round(toggleOff).
The bug here is that when you use swipe_btn.toggleState(); The button will be deactivated, meaning it will not even respond to click event which is not right, so the way around is to use the onStateChangeListener as I use it above so that when the button is open you can do something and when it's close you can still do anything.
Note: library version: 'com.ebanx:swipe-button:0.8.3'

How to visually show that a preference options are disabled?

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.

disable a menu permenantly in 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);

RingtonePreference not saving in PreferenceActivity

I am using PreferenceScreen to set some user preferences in my Android app. It works perfectly for several ListPreference and CheckboxPreference items, however I cannot get RingtonePreference to work properly. The appropriate ringtone dialog displays, and can be selected, but the selection is never saved.
My app only plays the default sound no matter what I select. Whenever I re-open the ringtone dialog (either immediately after making a selection, or after exiting the app and coming back in) it always just has the default item selected. I have a field to display the preference value, and it always shows the default sound is selected, even after changing it on the preferences screen. I also confirmed that the appropriate xml file on my device (in /data/data/myapp/shared_prefs) is not changing when I monitor it with DDMS. If I change other items (such as a CheckboxPreference), I see the shared_prefs file change realtime. I have stripped my PreferenceScreen to the bare minimum and it still behaves the same. I tried changing key names, defaults, and clearing app data on my phone.. nothing seems to work.
I did find 2 similar questions already posted on SO (here and here), but they are several months old, with no answers and/or the person asking the question gave up and tried another method. I'd like to figure out why it does not appear to work as designed.. or at least, find a suitable method to get the same thing done.
The relevant portions of my code are below.. thanks in advance!
/res/xml/preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<RingtonePreference
android:key="alertSound"
android:ringtoneType="notification"
android:summary="Select audio notification sound"
android:title="Alert Sound" >
</RingtonePreference>
</PreferenceScreen>
/src/myapp/EditPrefsActivity.java:
public class EditPrefsActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
settings = PreferenceManager.getDefaultSharedPreferences(this);
addPreferencesFromResource(R.xml.preferences);
}
}
I just found the solution! The answer was recently posted in a similar question: https://stackoverflow.com/a/15887357/1992342
Removing android:launchMode="singleInstance" from the EditPrefsActivity <activity> entry in the manifest solves the problem. Apparently it is a [little known] Android bug.

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