Any examples for "onDialogClosed"? - android

Can anyone give me an example or explain me how i subscribe to the
onDialogClosed method of an EditTextPreference?
http://developer.android.com/reference/android/preference/EditTextPreference.html#onDialogClosed%28boolean%29
I want to know when a dialog had its OK button clicked
and then retrive the information from the EditTextPreference.
Is there any examples/tutorials of this available or can anyone point
me in the right direction?
Thank you.

Just for completion: Since EditTextPreference is a Preference, you can use a OnPreferenceChangeListener. That will be called when the preference is changed. Check for the EditTextPreferences key in the callback and retrieve the new value to act on it. This is especially useful when the preference can be changed in more than one place or will be changed in the background by your app (e.g. writing defaults back when clicking a "default settings" button), since every change will trigger that callback (when your register it global on your SharedPreferences). The onDialogClosed will only be triggered when the user closed the actual dialog.
If you want to watch the single preference you can also use the EditTextPreference.setOnPreferenceChangedListener() function to assign a listener to that preference only.
OnPreferenceChangeListener documentation

You can make something like that :
#Override
protected void onDialogClosed(boolean positiveResult) {
if (positiveResult) {
String text=MyEditText.getText();
}
else {
// cancel hit
}
}

Related

Modifying a Preference value in PreferenceActivity has no effect before Activity is closed

I am just trying to change the value of a Preference when the PreferenceActivity has been opened. As there is no "setValue" or similar on a Preference, I try
My code:
long value = System.currentTimeMillis()/1000;
PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().putString("test",""+value).apply();
getPreferenceScreen().findPreference("test").setSummary(""+value);
My XML:
<EditTextPreference
android:key="test" />
What I expect:
When clicking on my Preference, it should display the value of time (same than summary) and let me edit it.
What happen:
The value is only changed after I closed the Activity. Next time I open the screen, the value is correct (but as in fact already changed to the next one)
First attempt:
Let s say value is 1521143527. Correctly written in the summary, but when I click on the Preference, the popup display an empty value.
Second attemp:
Summary has changed to 1521143540. When I click on Preference, I can edit the previous value (1521143527)
Third attempt:
New Summary, but Preference value is not changed and is still: 1521143540
etc...
Any idea what is wrong?
DIRTY WORKAROUND:
setPreferenceScreen(null);
addPreferencesFromResource(R.xml.preferences);
Will now force the preference to update, but that's really dirty, and I still don't understand...
If you look into the PreferenceFragment source code ,you can see that there is a method called bindPreferences() which binds the preference values to Views . Only in 2 scenarios this method is called,
When Activity created onActivityCreated(#Nullable Bundle savedInstanceState)
When addPreferencesFromResource() called. there is a handler which triggers the bindPreferences()
Other than this there is no way the views are updated. bindPreferences() is a private method , so you can't call this method outside of the class. So you should update your preferences before either those events.
You mentioned, As a workaround solution, you should update your preference first then call addPreferencesFromResource() . Like below
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
long value = System.currentTimeMillis()/1000;
getPreferenceManager().getSharedPreferences().edit().putString("test",""+value).commit();
addPreferencesFromResource(R.xml.pref_general);
getPreferenceScreen().findPreference("test").setSummary(""+value);
}

Is it possible to have listener on EditTextPreference?

I currently have a Settings Preference Activity consisting of one EditTextPreference. I would like to have an listener when the Dialog closes, so I could run some code
I've tried some of the methods listed here: http://developer.android.com/reference/android/preference/EditTextPreference.html,
but to no luck.
Am I missing something?
EDIT:
protected void onDialogClosed (boolean positiveResult)
{
Log.d("tag", "dialog");
}
I used this method for example wich should print the tag each time the dialog is closed, but it is not working like I thought it would
You should listen to the preference change, not to the dialog close event.

Run some code when user closes a particular preference screen?

Is there a way to find out when the user closes a particular preference screen? I need to do some updating when the user has changed the preferences in one of my preference screens. Not when the user clicks an actual checkbox, but rather when they press the back button and are done editing the preferences.
So far, all I've been able to find is that I can override my PreferenceActivity's onPause() event, which seems to happen when the user closes the preferences. It works, but I'm not sure this is the best way of going about it. Any suggestions?
EDIT
I couldn't figure out how to actually call a method of my main activity from within the PreferenceActivity. Tried playing around with putExtra() and all sorts of stuff. Eventually i figured - why not do the completely opposite? So instead I implemented the onResume() method in my main activity. Works great for doing stuff when the user closes the preferences, and I can live with the fact that my update method runs every time I resume my app as well.
Well I have been searching for a solution of this issue for weeks up to 2 min ago...
i think i found the way.
Preference myPrefScreen = findPreference("myPrefScreen");
myPrefScreen
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference prefScreen) {
// TODO Auto-generated method stub
Dialog prefScreenDialog = ((PreferenceScreen) prefScreen)
.getDialog();
prefScreenDialog
.setOnDismissListener(new OnDismissListener() {
#Override
public void onDismiss(DialogInterface arg0) {
// TODO Auto-generated method stub
///HERE YOU CAN EXECUTE WHATEVER YOU WANT...
}
});
return false;
}
});
You need OnPreferenceClickListener to correctly instantiate the PreferenceScreen Dialog generated only once the PreferenceScreen entry has been tapped on the screen and the new window with the preference children are shown. Otherwise you always getDialog() returns null and there's no way to attach the OnDismissListener to it.
Once you set the OnDismissListener the trick is done!!
I hope this could help.
Happy coding to all!!
That's the best way to go about it: overriding the onPause() method. It's called right before your activity leaves the foreground.
onPause is the right place to handle and update.

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.
}
});

ListPreference with a max number of selectable options

I want to have an element in my preference menu that does the following:
Show a list of options.
Many are selectable
Maximum amount of options to be chosen 2.
Possibilities I thought of:
Doing a separated PreferenceScreen and showing options as checkBoxes, but I don't know where to place the logic of max 2 options.
Extending DialogPreference and doing it by hand.
What's the best way?
Extending DialogPreference would get you the closest in terms of look-and-feel; the Preference classes are fairly unflexible and un-extendable in my experience.
I can't remember too much about PreferenceScreen, but I imagine it's similar.
In an app I worked on, we ended up using separate activities, launched via Intent from a Preference item onClick. This allowed us to easily develop preference screens that require validation logic a bit more complex than the usual.
You can put the logic of maximum two options in a OnSharedPreferenceChangeListener.
So you just listen to all the preferences as they change and update them if an invalid combination is selected.
So your code would be something like the following:
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
//Code to calcuate how many are selected
int code = numberSelected();
if (count > 2) {
sharedPreferences.edit().putBoolean(key,false).commit();
Toast.makeText(this,"Can't select more than two!",Toast.LENGTH_LONG).show();
}
}
If you create your own PreferenceActivity that implements OnSharedPreferenceChangeListener you can enable the listener to be listening only when required doing something like this:
#Override
protected void onResume() {
super.onResume();
//Register the listener
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
#Override
protected void onPause() {
super.onPause();
// Unregister the listener
getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
}

Categories

Resources