TheLettuceMaster
TheLettuceMaster

Reputation: 15734

How do you save a (user-created) dynamic Layout in Android?

I have a button on my home screen that will add an edit text field every time it is pushed (I limited it to 20 for best practice). It is basically a small budgeting app. I want the user to fill in an X amount of dollars in one of the fields. Then, when they are ready, to come back to the app another time and that created EditText is still there filled with the value. I know how to utilize SharedPerfrences to hold the value. I do not, however, know how to hold that newly created EditText field when the user comes back.

I am acutally going to expand and have a button and a datepicker associated with each EditText field so soon click the button will create three objects, not just one. But I want to get one working first and will apply the principle to the others later. Any suggestions?

Upvotes: 3

Views: 1650

Answers (2)

Arif Nadeem
Arif Nadeem

Reputation: 8604

You would have to use Android Parcelable interface, refer to this to know as of how to implement it, refer to this question which I has asked on StackOverflow, it has a good answer.

Upvotes: 0

Trevor
Trevor

Reputation: 10993

Sounds like a good candidate for a SQLite database.

For instance, I use a SQLite database to retain and recall the co-ordinates and types of widgets that the user has arranged within my application.

Note that you don't ever want to be trying to store and recall the actual EditText View object itself. In your database you need to devise your own table format that suits the kind of information you need to store. If you simply need to retain the states of a series of EditText fields then this could be as simple as having a table with just one column that contains a string value on each row. To build your UI up again from that database, you'd loop through and create a new instance of EditText, and set the text of each one from the table.

Upvotes: 1

Related Questions