save layout back into xml - android

I am working on a project where I have multiple tabs, and I want the content of each tab to be set dynamically based on what is selected in the previous tab. (For my tabs, I am using the SlidingTabLayout with a ViewPager). I currently have my first tab set up correctly and have it tied to an EventBus that creates a LinearLayout based off of my template from R.layouts.tab_2 and adds the content dynamically. What I can't quite figure out is how to save my LinearLayout back into the tab_2.xml file. I have seen suggestions of saving it with SharedPreferences, but that doesn't allow me to save it as the right file type.
If there is a much easier way to do this, such as updating the tab that is already created, I am open to that instead of saving the layout back into the XML file. I am fairly new at programming in Android Studio and still trying to get the basics down.

Your first tab, as I'm interpreting it, is essentially a settings page. The user does a few things, and as a result of those things - the layout on your second tab looks different.
You need to save some variables based on user interactions. For example, if you have a checkbox the user is going to press - you would set a boolean to true when they've checked it, and false when they've unchecked it. When the user navigates to your second tab, you will check the value of that boolean - and alter that layout accordingly.
Hopefully this concept will, even in its simplicity - help you understand how apps are built to be functional.

Related

Gmail like registration layout in Android

I often put user registration on one xml file with scroll view.
I have learned when a user sees too many fields they opt out of the app so I am planning to upgrade the layout to look like the one for gmail when you sign up on android app. It appears to be using tabbed view but not tabs available and user navigates to the next view by clicking next (refer to attached images).
First page
second page
MY QUESTION
My question is how do I create this views, do I need many xml layout files or put all stuff on one file and use visibility as in once it is filled and user presses next hide the inputs and reveal the next inputs. Note: Gmail appears to be scrolling to another page the way it would appear when using tabs.
You can use one activity to host multiple fragments. The new Android Studio update has Navigation Editor which you can use to achieve similar results.
it is totally up to you how you want to design this but the best thing is you use Fragment and viewpager instead of using two separate layouts.
See Here how to use fragment with viewpager
There are many ways to do this, If you have less content then play with visibility otherwise ViewFipper ( https://abhiandroid.com/ui/viewflipper ) Or can use Fragments

Save text fields content from Android Tabbed view

I am working on a corporate apps for internal use,
one of the sections that is presented to the user is a TabbedView (in fact I made it from the Android Studio "clean sheet", so its Fragment-based and extends AppCompatActivity), each one of the four tabs it is composed of contains a few text fields.
The user is free to navigate back and forth across the tabs and fill in the text fields, then hit "go" at the bottom of the last tab and submit the data from all the text fields.
I am finding an incredibly hard time in doing this, what I would like to do is simply to use a listener telling me when the user is switching from a tab to the next one and save the data in the shared preferences. (being also able to restore it when the user is freely switch between tabs in the editing phase just doing the same thing with the listener for a tab selection)
I was quite surprised to see that the easy old tab system with the super handy listeners for "on tab selected" and "on tab deselected" event is now a thing of the past, so after two days spent trying to figure out how to do such a simple thing as saving a few text fields right before the tab gets killed I am now surrendering and asking for help.
How can I automatically save data from the text fields in a tab before loading one of the others?
thanks! :-)
Read about fragment life cycle on the Android Fragment documentation page.
Save your data in onPause.

Opening Fragment From Inside PreferenceScreen

sorry if this is a stupid question.
So, I've got a settings.xml defining my preferences, and at some point in the tree I have:
<PreferenceScreen [...] android:fragment="path.to.fragment" [...] />
And when I tap that thing, I go to my fragment.
That's working.
My fragment is a ListFragment, because I am showing a list of Bluetooth devices on this screen and can't just declare them statically.
My question is, when a user clicks one of the list items in my ListFragment, I want to open a details screen where they can set a preference with that selected device, but I can't figure out how to programatically create and launch this next fragment while still keeping things in the nice tree the preferences provides.
For now I'm just creating an activity so that I can move past that part of the issue, but I have to figure something out before this is done.
Is there an easy way to do this, or am I taking the wrong approach entirely?
Thanks!
EDIT: Picture
So, Page 1 is my settings fragment, which is built using the XML with the highlighted option being generated using PreferenceScreen as above.
Page 2 is my listFragment, and because those items come from Bluetooth, it can't just be another settings xml entry.
Page 3 is what I get when I pick an option (WIP). To get to this point I had to make it an activity and use startActivity, but the other two were fragments. I'm asking if there's a way to programmatically do the same thing the PreferenceScreen option is doing in the xml and launch a fragment with the top bar maintained and stuff.
Alright, here's what I've got.
I realized that at the top level I had a PreferenceActivity, and it has the method startPreferenceFragment.
So, I do:
((PreferenceActivity) getActivity()).startPreferenceFragment(new MyFragment(), true);
Which works for me.
It doesn't feel like a solid solution, but it has the effect I want and I have to move on.

Modifying application workflow to use TabActivity

This question actually has two parts.
The first part:
I've been developing my first app for a couple of weeks now. I have 5 screens and everything seems well. However, I'm considering changing the app's navigation to a TabView.
I haven't delved much into it, but I'm hoping someone can save me a little bit of time. It seems that people don't generally place Activities inside each tab. They simply point the tab content to a View. This is where my major setbacks are. 1) I already have Activity classes full of code and 2) I can't quickly guess how the structure of an app using TabView looks. For example, where do I put the handler code for clicking a button on a View? Does it all just get dumped into the TabView Activity somehow?
What I would like is if you could please give me a quick synopsis of what I'm looking at doing, answers to any questions you think I may have, and point me toward some resources for creating TabView applications. A quick Google search really just shows me how to create a TabView Activity and add a couple tabs to it. The code doesn't go any deeper. For example, say I have a layout xml to show in one of my tab's content pane, where does the code go for clicking a button I have in that layout?
The second part:
I've added a TabActivity to wrap the Activities I currently have in. At the moment I have Activities populating the content of my tabs (though ultimately I'd like to do this in the most efficient fashion, which doesn't seem to be having Activities be tab content). I've noticed something rather annoying. My MAIN Activity is an Activity I wrote for my user to log in to their account. After logging in, they are taken to my Tab Activity. Here is what happens:
When I am on my Tab Activity and I "minimize" the app by clicking the Home button and then launch it again, I don't get taken back to the Tab Activity. I get taken to my log in Activity. Why? I don't have the launchMode of my Tab Activity set to singleInstance... or is it singleInstance by default? How can I make the app re-launch showing the Tab Activity (ideally by setting some parameter, assuming I'm doing something wrong, and not having to save this data off somewhere and reading it and programmatically telling it what to go to)?
Thank you for all your time and help
I don't have a comment on the advisability avoiding the use of sub-activities in TabActivity. As for handlers -- if you aren't going to embed views instead of activities, then all the android:onclick type handler settings in your layout XML will call methods on the TabActivity. This is because they go to methods on the views' Context, which is the generally the nearest containing Activity. If you want to split your code up further without using Activities, I believe you'll have to use findViewById calls on the tab content views after you've set them up, and bind the handlers manually from there in your code.

Replace tab with another view

I have a TabActivity with a tab, which shows a list. I'd like to display a WebView whenever a list item is clicked. I tried calling listSpec.setContent(R.id.details); but it would not find the resource.
Any clues what I am doing wrong?
UPDATE: I found the following clue from Mark in the mailing list
You don't change the View inside of a tab. You cause the existing View
to show something else (e.g., use FrameLayout and make one layer VISIBLE
and another layer GONE, or use ViewFlipper to swap between contents).
You might be able to get setContent() to replace the current View by
passing in another layout ID or TabContentFactory, but I'm skeptical
this will work.
The tab widget framework in Android is designed for simple stuff. You
may wish to grab the source code for the relevant classes out of Android
and make your own version that handles your own scenario.
--
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy
I don't know if I'd trust the guy you got your update from. He's...he's...he's...he's balding!
;-)
The most common pattern on Android is for clicking a list item to bring up another activity. If for some reason you don't want that, then the advice I gave in your update is still valid -- you can achieve your effect by hiding the ListView and showing the WebView by one means or another.

Categories

Resources