In my android app, I need settings to select time range (e.g. 2 to 4 minutes, 8 to 12 minutes etc).
To implement it, I used two instances of ListPreference, one to select minimum time and other to select maximum time.
As maximum time cannot be less than minimum time, I want to update the maximum time list preference whenever minimum time is changed by a user.
To achieve it, I am using Preference.OnPreferenceChangeListner. When user changes minimum time preference, the listener callback function is called and in the callback, I call setEntries(), setEntryValues() and setValueIndex() on maximum time ListPreference instance.
The problem is that GUI of maximum time preference is not updated immediately when I do above. I can still see old entries in the preference. If I exit settings activity and launch the settings again, I can see updated entries and values.
Please let me know how I could solve this issue.
Also, please suggest if there is any better way to handle such settings use case.
Thanks.
After some more debugging, I realized my mistake.
I did not read this document of OnPreferenceChangeListener callback:
"Called when a Preference has been changed by the user. This is called before the state of the Preference is about to be updated and before the state is persisted."
The problem was that I was querying the value of minimum time list preference in the preference change listener callback of it and generating the entries of maximum time list preference on the bases of that. But as the document reads, new value of minimum time list preference was not yet set in the callback, so the entries were generated on the basis of old values. So it was not supposed to work as I was expecting.
Sorry for not posting code as it required more effort to make the code ready for posting here.
Thanks for reading my question.
Related
I've developed an xposed module for whatsapp.
http://forum.xda-developers.com/xposed/modules/mod-whatsapp-extensions-add-extra-t3452784
I wanted to add feature to hide our own last seen still see others or report a fake last seen for eg: 1 Jan 1970.
I made following assumptions:
To do that first I hooked date and System.currentTimeInMillis methods to make whatsapp think its 1 Jan 1970. That worked but still last seen was shown perfectly.
Assumption: The last seen time value is directly taken from the server
Then I looked in the source to find where last_seen preference is referenced. Turns out it is only referenced in SettingsPrivacy activity's class.
Assumption: To hide our last seen and still see others we need to change last seen preference to 'visible to all' and turn that back to off once we get the last seen.
but the problem is it uses onPreferenceChangeListener. We cannot hook a method from the interface directly.
I cannot find the subclass which implements onPreferenceChangeListener as the classes shown in code are synthetic.
Please if anyone can help me with this, it will be great. I need to find which is preferencechangelistener for that preference. Rest I will manage.
This is kind of a brute force trick to get the implementation but I guess you can hook the app ClassLoader.loadClass and for each loaded class check if it implements the interface. If so hook its onPreferenceChangeListener.
I found a way to do it and its working.
http://forum.xda-developers.com/xposed/modules/mod-whatsapp-extensions-add-extra-t3452784
The way to do it is by hooking a method which takes a preference as argument. We create a preference ( com.whatsapp.preference.WAprivacy preference to be precise ) and then pass this preference with last seen set to desired value to the method. And we are done.
It is working so far.
I'm using this library to show the main features of the app, currently I'm using shared preferences variable like this,
1- After installation I set a SharedPreferences variable x to 0
2- First time the user opens the main activity I check the variable x, if it 0 I use ShowCase
view and set x to 1
3- Now every time the user opens the app I will check x, if it's 1 I skip the ShowCase view
I'm new in Android and I'm not if it's good idea to check SharedPreferences on the main thread every time the app is opened, any one thinks I should be doing something else instead? or is this good enough?
It's absolutely ok. Of course it depends on time of retrieving of x variable from SharedPreferences. I suppose if you save few million variables in SharedPreferences then time of retrieving will significantly grow and user will notice it.
When you think about performance just test your case because general recommendations could not work in particular case.
How to start the chronometer with a specific time other than default 00:00? Is it possible to set chronometerObj.setBase(startTime) ?
ch.setBase(SystemClock.elapsedRealtime()-anylongvalue);
ch.start(); can I set start time, if I put anylongvalue?
In general:
mChronometer.setBase(SystemClock.elapsedRealtime() - (nr_of_min * 60000 + nr_of_sec * 1000)))
Chronometer object, when instantiated, defaults to the base time being set to now ('now' as in the value you get from SystemClock.elapsedRealtime()).
You can change the base time (00:00 time) by calling setBase(<some other value>).
Presumably, although I haven't tried the experiment, you could see the elapsed time since last system boot using setBase(0).
So you can use chronometer to see the elapsed time since any arbitrary call you made in the past to SystemClock.elapsedRealtime(). The trick is that you need to have stored that value somewhere you can dependably get it back despite app and phone state changes. (See Android: chronometer as a persistent stopwatch. How to set starting time? What is Chronometer "Base"? for example.)
Many answers suggest persisting that arbitrary time-in-the-past in an intent. but, at best, this only keeps the timer counting up while the phone stays on.
I already am using a database and store my starting time in there. I created a one-column table for it and store a single record in it. My starting time for the chronometer survives a phone reboot.
For a Settings class and associated XML page, I receive NullPointerExeceptions about 50% of the time it is accessed. A typical session attempts to Load the current settings shortly after startup. The user can the proceed to fill out an order, review past orders, or update/view current settings.
Sometimes the settings don't load and other times they won't save.
Loading:
((EditText) parent.findViewById(R.id.txtCompanyName))
.append(companyName);
Saving:
companyName = ((EditText) parent.findViewById(R.id.txtCompanyName))
.getText().toString();
These are the first lines of their respective functions. I am uncertain why they would raise this Exception (mostly the saving function). As near as I can tell, the loading function may be called before the View is fully loaded, however, the save function can only occur after the the View IS fully loaded (it saves on a android:onClick for a Button).
What i would recommend to you is use a SharedPreference to save a persistant state of the user's settings.
This would be a better more efficient way.
And if there isnt anything in the SharedPreference it will never return null unless you set it to do so.
Let me know if you need an example of this.
I'm creating my first android app (or attempting to anyway) and i have a question i can't seem to find the answer to.
I would like to allow users to enter a separate set of preferences for each item in a list view. I have the PreferenceScreen working, but it maintains the same preferences for every item (i.e. changing 1 item results in changes to all others as well).
To give a more detailed example:
Say i have a list of cars:
* Car 1
* Car 2
* Etc...
I would like to be able to click on "Car 1" and get a PreferencesScreen to specify "Make", "Model", "Color" for just that car, instead of having those entries stored globally for the whole app.
Is this possible?
Thanks for your help!
//N
Solved it!!
For any wondering, the solution is that you must call
getPreferenceManager().setSharedPreferenceName(value) with a value unique to the item you'd like to modify the preferences for (in my case, i used the ListView position) prior to calling addPreferencesFromResource.
getPreferenceManager() deprecated on api 11
Now with honeycom and ICS the method getPreferenceManager() is deprecated. Do you know how to do that with the new (PreferenceFragment + Headers)?
Thanks men! It works perfect in earlier versions!
UPDATE: Up to api level 11 is the same method but you have to call it on a Fragment, not on activity.