I'm adding dynamically created EditTexts to a layout (onClick button method). When I rotate the screen the added edittexts disappear. How can I add them to a bundle to put them in the onSavedInstanceState method? Or, is there another way to do this? I know I can save the text, but is there a way to keep the screen layout when I rotate it? If the user presses the button and adds five EditTexts(with or without typing anything) I need to save this layout when the screen is rotated (I basically need to dummy-proof my app :) ).
Thanks in advance!
You cannot add views to a bundle because they are not parcelable / serializable. The only stuff you can and should save into the bundle is the current state of the activity.
You could maintain a list data structure or a counter variable that keeps track of the dynamically created views. Also save the string values of the EditTexts. In onCreate you should interpret that information in order to recreate the views and their states.
you can't save a view. bundle is just a pair name-value. of course you can save the text and then render it again. the way is just you said.
onsaveInsatnceState(bundle save) save the text as save.putString('text1','bla bla') of course you need a loop to save all your edittext.
then in oncreated( bundle save)
you can get what you have added by save.getString('text1')
Views itselfs doesn't represents information that you need to save. Instead you should save their coordinates, width, height, value etc.
Your can save your view data into a class or into a arraylist.
when your fragment activity will restart than you fetch data from saveinstance state in oncreate function.
Use this function setRetainInstance(true). after using thing function your fragment activity will not destroy.
Related
How can I do this When User Click On the Next Button Activity Should not change only data should change for example if there is TextView then its data should change and so on.
I want output like This Gif
If you have any doubt please feel free to ask in the comments
I think this is able to do with the help of animation.
There are a couple of ways you can do this. You will need to have a list of data which will hold the title and the images. Then you can do one of the following
On your button click, change the text of your TextView and set the image resource of your ImageView by fetching the next item of the list manually each time.
You can use a ViewPager and update the current page when you click on the button
You can use a ViewFlipper and manually add Views based on your data and change views on button click
To achieve this view take on int counter with initial value 0 and use CountDownTimer method.
Create list which contains image and text values in model. on CountDownTimer method every tick you can increase the counter and set image and text again from the list index.
So I've been developping an app on Android Studio and at some point I need to display couples of values composed of texts and images but I have some issues with the display.
Let's say here that I want a list of fruit names associated with a picture, I'd like to display it that way :
Example
Each image and its related text is in linearlayout, so it's a list of linearlayouts.
The "add fruit" button open an alerdialog where the text and the image can be chosen, by clicking the 'ok' button in the alertdialog the name and the image are saved in a sqlLite database (which I already have programmed), and the linearlayout has to be added in the current list as the dialog is dismissed.
So the issue here is that:
I don't really know how to dynamically display each one of these linearlayouts in this specific order.
I don't know how to make it so that when a new fruit is added and the dialog dismissed, the list in the activity is refreshed directly.
I've been thinking of using a listview but listviews are only horizontal OR vertical but in this case I kind of need a mix of both.
Another idea was to delete the layout containing all the linearlayouts (all the list) when a new fruit was added, and then take my database to create all the linearlayouts again. But dynamically I don't think it will work, the refresh won't be done (?)
My last idea was to create a fragment and use my database in the onCreateView method to display every elements. And to refresh it, I'll just detach and attach again the fragment to the activity each time I add a new element.
I don't think my ideas are the optimal or even a correct way to do it, I've been struggling with it for a while. If you have any suggestions or ideas to help me, it would be great :)
I am getting text data from database. I am using Textview to show data
![Textview with scrollView]
I want two button at the bottom of activity (next) & (previous) which show data from database in pages style.
--> user click on next button
it shows scrolls data in that activity like pages
Basically i want to remove Scrolling and add pages turn feature in activity ![buttons used][1]
If i understood your question correctly you are trying to break incomming text into pages so that it fits your screen.
Firstly try to check Breaking large text into pages in android text switcher or view flipper
Instead of the viewflipper try using viewswitcher, which allows you to switch views after calling `getNextView() method. You only need to call this method with the "Next" button.
Hope this helps you ...
You can use ViewPager for this purpose, sample tutorial for ViewPager. First create a list containing you database values and then pass the list as parameter to adapter. You can also add next and previous buttons and use setCurrentItem method to navigate to next and previous accordingly. Hope it helps!!!
I hope I can explain this properly.
I'm making an android app that, when you open it, it connects to a JSON server, pulls down the data (GPS coords) and dynamically creates a "menu" based on what it received (View1). This will consist of a few buttons, which when clicked, will load a MapView (View2) with the coords gotten from the JSON represented as markers on the map.
I start off with setContentView(R.layout.menu) then get the data, onbuttonClick I load setContentView(R.layout.map) and draw the markers. The problem is, I have an onLocationChangedListener that goes through the code to set up the menu initially. When it tries to build the menu when the mapView is open, I get a force close. Unfortunately, this code also updates the user location and the locations of the overlays and re-draws the map.
My question is: Can I do a check on a layout to say something like if (isActive) so that I can perform actions only if the current view is in focus?
OR should I scrap the whole thing and start again with a better layout? (Suggestions welcome)
Summary::: I have 2 views (menu,map). Need access to same data across both. Currently works with setContentView() but gives me a Force Close when actions are performed on inactive view.
If I understand correctly, you are using setContentView(someLayoutId) to change each time what the Activity is displaying. This is not the way android apps usually work. When you retrieve a resource, you need a root element to reference it, that's why you get the exceptions when the View is not "active".
You have several other options to evaluate:
Create a new MapActivity to show the map
Create a TabActivity and add the map as a new tab
Use a ViewSwitcher to decide whether to show the map or not.
For my Android application, I want to have a view which allows a user to click a plus button to add EditText fields, and next to the EditText fields, I want to have minus buttons that will remove them from the view. In essence, something that is very similar to adding multiple phone numbers/email addresses in the edit Contact interface on Android.
I imagine I will need to do this by inflating my main view with a separate one that contains the EditText and button I want to add each time. However, I have no idea how I will manage identifying each EditText and button with a unique ID, and thus, I have no idea how I would manage to grab the values of each EditText for saving to my database. Can someone advise me on what I need to do? Thank you.
If you have inflated a sub-layout, then you should now have a View object.
You can then call findViewById(R.id.edit_text_1) on that View — assuming that you supplied IDs in the sub-layout XML.
So long as you keep track of each of the parent Views, you can always use findViewById() on them. Or after inflation, if you really want you can set a new, globally-unique ID on each EditText using setId().
You can assign a view widget dynamically, and generate your own ID as it is assigned.
Button b = new Button(this);
b.setId(myId);
Then you can refer back to that widget.
findViewById(myId);