I'm writing an android application and on one of my pages, I have a text field, a spinner, and a date picker that need to be saved/updated in a SQLite database. All three have default values on creation, and the database needs to update any time a field is changed (there's no 'Save' button or anything like that). I've looked into using an onFocusChangeListener() for this, but it seems like the application only changes focus when using text fields, so it wouldn't work with a spinner or datepicker. I've also tried splitting the components into fragments and overwriting the onPause() method with update functionalities thinking that the fragments paused when the user moved on to the next one but I think that it still only checks when the user leaves the screen. Any help/advice would be greatly appreciated. Thanks!
For Spinner you can try OnItemSelectedListener, for Datepicker, try OnDateChangedListener
Related
So at the moment the user will select a colour, shape and time. When the app first starts up there is a default for each. What I want to happen is to keep the users selection for all 3 of these even after they close the app while it runs in the background.
If you need me to post anything to help ask away. Thanks.
After writing out what the problem was, it's not that they're not being saved it's the fact that the selections ARE being saved but not being displayed properly. I.E by a border or the dropdown menu results back to it's default value instead of the users selection.
So the values are being saved(my bad) but the app overwrites these saves and doesn't use the border or beings the dropdown menu back to it's default value.
circularImageView = (CircularImageView)findViewById(R.id.activity_main_silver_color_button);
circularImageView.setBorderColor(getResources().getColor(R.color.unselected_border));
circularImageView = (CircularImageView)findViewById(v.getId());
circularImageView.setBorderColor(getResources().getColor(R.color.selected_border));
These work independent of each other but when the border is selected, if a another color was selected then it becomes an unselected border. But what happens if "selected_border" state is never saved.
public static void saveLockScreenDuration(Context context, int duration){
getStoredPreferences(context).edit().putInt(SELECTED_DURATION, duration).apply();
public static int getLockScreenDuration(Context context){
return getStoredPreferences(context)
.getInt(SELECTED_DURATION, DEFAULT_DURATION);
}
This is where all the prefereces are stored. DEFAULT_DURATION and SELECTED_DURATION are in a class called constants.
Upon selection you can store selected values in Shared Preferences. And upon launching it again you can check if those values are null or not. And display accordingly. Read how to use Shared Preferences here.
if you talking about for activity restore data.
then see here.
https://developer.android.com/guide/components/activities/activity-lifecycle.html#saras
Correct me if i am wrong. I think you can use PreferenceManager.getDefaultSharedPreferences(Context context)
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
which is shared across all your Activity and Service classes. If you set an accessibility service to get the event when app is in background and used defaultsharedprefrences to store i think it will work for your case.
so I have recyclerview populated with data from database. Each row contains two buttons one for play and second for stop time. So what I'm trying is to save state of buttons inside recyclerview. And I used
PreferenceManager.getDefaultSharedPreferences(context).edit().
putInt(Constants.numberOfbutton, getAdapterPosition()).apply();
to save particular position. I have inserted that line of code inside button click and I checked it's saving correct position. What I'm trying to achieve is, when user enters the app after closing if he lefts any of the buttons in play mode to restore that state and continue timer. But problem is when user enters the app all buttons are running. I'm using chronometer for presentation of the time. I think here is the problem in this method:
private void startTime(int position ) {
Data data = datas.get(position);
chronometer.setBase(SystemClock.elapsedRealtime() + Long.parseLong(data.getTime()));
chronometer.start();
}
UPDATE EXPLANATION:
So I can load from database for each item of recyclerview it's time, date, name etc. But I can't figure it out how to play chronometer of just particular item when user enters the app again. NOTE AGAIN: I'm performing correct saving getAdapterAtPosition and loading data from PreferenceManager. I've checked that. Even I tried putting the number of one of rows but same issue appears.
Any help very appreciated. Thanks.
I have found the answer. I save the boolean in database actually I used 1 or 0 than parse that to true or false acordingly is button play pressed or not. Thank you all for your comments and trying to help.
Try to use handler or alarm service instead of chronometer
So i'm planning to develop an android application. In one activity, if i were to click a button of add, then it will prompt a user input and store the data. After that, thare would be another "list of item" button, where users can see which item they added and the information they input earlier.
I was thinking of using if.. else statement to check whether the button is click by setting a variable of default status=0 and change to 1 when clicked.
But how am i going to make sure that the functions won get messed up? is there any better suggestion for this? Im a beginner who just started to explore android studio.
Okay, so it is very simple and logical, and here is my best explanation. It's a rather long explanation, but I made it as thorough as possible...If you need any more help, just let me know:
You don't need to do any of that status=0 stuff. Just have an onclick listener:
(androidfromhome.com)
And in the listener, you want to go to another screen with a text input. So, just have an intent in the onClick which takes user to another screen:
(www.devcfgc.com)
Okay, so now you got your second screen right? You're going to want a textview (Change this in the xml) in the middle of the screen, where the user can enter data:
String stuff = TextView.getText();
Note, you will need to initialize the textview:
(androidfromhome.com)
So, now you got the text that the user entered. Just save it in SharedPreferences, and that's it, you have it saved!
http://developer.android.com/reference/android/content/SharedPreferences.html
Read about SharedPreferences in the link above.
So now, you have it saved. If you are going to get a lot of data, you can also save it in a database, which is more advanced stuff, that I don't know how to do. Now, display the saved data on the screen (A new screen, again, using an intent) using the method:
setText();
on your textView object for the next activity. That's it!
guys,
I've been working on a native Android application for some time and now I am at the end of it there is still a problem I need help with.
My project at github
There is the following problem - when user starts the application for the first time the app will ask him "When does your diet cycle starts?" with a datepicker popup. I am using SharedPreferences to store the result user has picked from the popup. I have separate DatePickerActivity from the MainActivity one that takes care of this datepicker that I start with startActivityForResult(). The DatePickerActivity passes the result to the MainActivity using an Intent.
When I debug the app I see finish() in DatePickerActivity is started twice and the MainActivity is started more than one this cause the datepicker popup to show once again.
Once the start date is set in the system there is no problem with these activities and application works fine.
Then comes the moment when user wants to reset the date - using the basket icon with text like "Изчисти" and the datepicker once again appear twice.
I hope I've been clear enough with my explanation and I am looking fowrard to hearings from you.
Best Regards,
Mihail
I managed to find a work around my problem. I guess I caused it because I need my main layout redraw after user picks a date from the picker and I doing
finish();
startActivity(getIntent());
Here should be the reason for multiple MainActivity instances in the app. Second thing I guest is that committing SharedPreferences to the OS is async and by the time I check for selected date it has not been written yet.
I solved the problem by saving picked date in static instance and when I redraw the main layout I already have the result.
Thank to those who lost some of their time trying to fix my problem!
I'm fairly new to Android programming and I've got this project I need to finish and I'm currently stuck.
I've got a standard listview in a Menu class with an array containing around 20 different elements. So what I want to do is load images in an imageview depending on which item in the listview I click, and since I'm a beginner the only idea I had was to make a new activity for each imageview which seems like a pretty bad way to do it since I'd need about 20-30 new activities..
To sum things up what I want is:
Code for making ONE activity that will display a different image depending on which item in the listview I click, probably pretty basic coding I want as simple solution as possible.
If possible I'm also looking for a solution that includes an SQLite database that stores the URL of an image and then display it in a single activity, also depending on which item I press in my current listview.
(I hope you understand my needs, and if you need I can also post my current code for the Menu class if it helps you help me) Or you can just show me a different way to make this work, I appreciate every answer! Thanks in advance!
NOTE
And please keep in mind, I'm a noob at Java and Android so keep it rather simple or at least explain what you do.
When you click on a list item, display the image in another view in the same layout, unless you want the image to take up the entire screen real estate. If you want it in the entire screen, go to a new Activity by sending the activity an Intent.
Activities are the "controller" of your application. They interact with the visible UI and the input from the user. You don't need a separate activity for each image, just an activity that's associated with a "place" in the UI (an ImageView) where you'll display the image.
I'd start by adding the images as resources under res/drawable before going on to databases.
You are going to have to do most of this yourself. There really isn't any substitute for taking the time to learn Java and Android. There are several tutorials and Android University classes under the Resources tab in the Developers Guide; I suggest you do all of them.