In my program i create buttons programmatically on OnTouch event but when i reopen the app or activity get restarted these button do not appear
Can any one suggest me how can i create buttons programmatically those will stick to the layout even after the activity gets restart or app gets restart
Use SharedPreferences to store coordinates value.
As suggested in Comments saving state in shared preferences is good option .
So you can go with this links to know more about this topic, I think it will be more clear when you will look into all this .
Save instance of dynamically generated views when we switch back and forth between activities
How do you save a (user-created) dynamic Layout in Android?
android save programmatically created views
Android - How to save a programmatically created view and restore it on app restart
Related
I am currently working on Android app which is like a simpler version of Photoshop in functionality.
So, I created a CustomView called DragAndDropContainer which handles all the drag and drop operations. The user can drag other CustomViews in it like Text, etc.
The problem is that whenever user clicks on Done, I want to save the layout and restore it later on when user reopens the app.
I researched and I was not able to find the best way to save and restore a layout. The DragAndDropContainer is a CustomView where user can drag anywhere, so I need to understand how to implement a method to save its final state and restore later.
Thanks in advance!
How to save a dynamically created button in android. My button dissapears when the app is restarted.
Thanks in advance.
The idea behind dynamically creating buttons is that they won't be created when the app is initially opened and will be created only when required.
If you require the buttons to be created everytime, just call your dynamic button creation code in onCreate.
In case you create multiple dynamic buttons, you'll need to store the data associated with the buttons by some means, retrieve the data when the app is opened and then recreate the buttons based on the data.
I have a very simple question about loading layout in main activity. I have a simple layout defined in activity_main.xml which is loaded in MainActivity's OnCreate() using
setContentView(R.layout.activity_main).
Then on a button click, I add another view item (button) which shows up correctly.
But when the app is closed and opened again, I need to retain what was added last time when the app closed. How do I do that?
I searched some questions here, but most of them talk about saving values using SharedPreferences or saving state, but it is not clear if the layout can be saved as well.
Thanks in advance.
I think the best solution is saving latest state values by using SharedPreferences, because it is a good way to save any simple data for long term.
Saving state via using saveInstanceState is a short term solution and if you close your app completely, saved instance will be gone forever.
Possible solution:
First, you cannot save a layout as you think, but you can get the layout's parameters and other features as variables, then you can save these by using SharedPreferences.
Secondly, you should check if there is any saved layout states when you start the activity. If any, you can use the pre-saved parameters for adding your layout dynamically/programmatically.
When app is running, users adds some views in the Layout. I want to store this Layout, so that when i reopen the app it should return the same state.
How can I achieve that?
Use onSaveInstanceState() and onRestoreInstanceState() callbacks to save and restore your views.
More at Managing the Activity Lifecycle and Recreating an Activity.
[EDIT] - As per #kadatt comment.
I think what you are trying to do is possible duplicate of android-saving-state-of-dynamically-changed-layout.
I dont think there is any standard way of doing this. You will have to implement your own functionality to save state of dynamic UI in some form which can be loaded again.
I'm working on the preference activity. The matter is about the possibility of add a button that allow to confirm, and eventually (that would be nice) a discard button in order to discard the changes applied at the preferences.
Let me explain better.
As far as I've seen it is possible using the common techniques that i've found in the tutorial to set different preferences using checkboxes and so on. The normal use case that involve preferences require that the user performs selections and after that click the BACK button in order to return to the old view.
However in the usability test i've done, it seems that this step is not always straightforward for all the users, and moreover a lot of them are not sure about the fact that the changes on the preferences are comfirmed.
Now we arrive at the question. is it possible somehow to have in the preference view selections (in particular one just composed by a group of checkboxes) to have an OK and eventually CANCEL button?
You have 4 options:
1) Design a layout and use a normal activity similar to the preferences screen where you give the user 2 buttons: Save and Discard.
2) Add a menu to the preferences activity with save/discard (and of course you will have to save the previous state and revert back to it if the user decided to discard).
3) Handle back button press on the preference activity where you popup a dialog asking if they want to save the changes
4) Add 2 "actions" to the preferences where 1 is save and 1 is discard and each goes to its own activity... Complicated and ugly in my opinion...
On a side note, I really believe users are familiar with Android's UI as 99% of the apps using preferences don't have this save button so it should be straightforward to the users that when they click on a checkbox - it is saved.