How to save a dynamically created button in android - android

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.

Related

How to save and recreate layout in Android?

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!

Saving/loading an Android layout with previously programmatically added buttons

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.

Android save programmatically created button

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

How can I set the visibility of a button from a custom surfaceview?

So basically I'm making a really simple game on android. Within my Activity I have a SurfaceView and some Buttons labelled play and restart. I set the visibility of the Buttons accordingly so that when the game is in the menu state it sets the play button to VISIBLE and in the game state it's set to GONE. The restart button is set to GONE throughout until the player loses the game. My problem is, when game is in it's lose state, how can the SurfaceView access the restart button from the main Activity to set it to VISIBLE? Is this how it's supposed to work, or is there another way? I'm kinda new to android, but I think this is pretty important so the user can really navigate through the app, especially in games. Maybe there's some beginner stuff I have to go back to?
Depending on your class structure, set a member variable to hold the reference to your button, or, if you are holding a context or the main layout, use Activity.findViewById(ButtonID) or a parent views View.findViewByID(ButtonID). Or you can introduce a new e.g. OnGameState interface whose implementations get notified when the game state changes. One of those implementations would then set the button's state.
write a new restart button programmatically, add it to your "lost/game-over" activity's layout . Write the appropriate code for its onClick()(something like finish() the present activity and launch the "new-game" activity) .

Dynamically load layout in Android

My application requires 2 screens and for this I have created two different XML layout files using RelativeLayout. One layout file loads whenever I run my Activity. Now I want to load the second layout on to the same Activity , when user click on a button in OptionsMenu and also when user press Back button the first screen loads instead of exiting the application. So that i don't need to create another Intent in my application.
Ideally there should be two different activities present in your application.
You can add or remove a view component in a view but if you are looking for two completely different screens then i would suggest you to go for a new activity.
I dint get what you meant by "and also when user press Bakc button the first screen loads instead of exiting the application"
If you dont want to show the first screen just finish() the activity.
Did you try re-calling the setContentView?
Or you may prefer using the ViewFlipper,
Good example here.
I would suggest a rather simpler means.
Put both your layouts in the single XML and show/gone them appropriately as need be. I dont think u need anything more complicated :-)

Categories

Resources