Changing layout files for a fragment during runtime - android

I am trying to develop an app in which fragments are involved. There are 2 fragments on screen. The list fragment containing a Start and Stop button and a detail fragment on the right side. On click of the Start button an audio processing code runs in the detail fragment. So depending on the result i get out of the process I want to change the layout of the fragment. Basically I want to change the layout file of a single fragment during run time depending on the results I get while doing some process. How can I achieve it?
Thanks!!

You cannot switch to a different layout file in a fragment, the view you returned from onCreateView cannot be replaced.
However, you have several options:
You can show and hide views at runtime with View.setVisibility().
ViewStubs can be inflated at a later time.
Or you could replace your detail fragment with a new fragment.

Related

How to use multiple Fragment in single activity in different times

I am making an app in which the header and footer would be same through out the app. i can start another activity with the same pattern of design in header and footer but as the other activity starts there comes a delay and we can easily notice that a new activity has pop up.
So what I want :
I want that if user click on any button from the footer the content in the middle between header and footer should be change and user should not see any jump which is made when I move to other activity.
So for this I know that I can use fragments. But for some reasons I can not use fragment activity. All I want to move fragment in the center only. So How can i do that . and How can I initialize other things which are in new fragment let say the upcoming fragment could have button so in this way , I wanted to know would it make my main activity heavy which is calling all these fragment
So please guide me through the demo code .
Note: please do not refer me to a fragment link just past some code to give me some idea.
Will be easiest to have the activity implement the header and footer and the variable content as fragments.
Please refer to this post. It has everything you need
Fixed header and footer in android app

How to alternate between fragment containers easily?

I've got a problem that I'm having problems solving. My app has 2 types of fragments. When the app starts, a fragment with main menu is added to a FrameLayout that I use as a fragment container. This fragment takes up the entire screen. Then, when I choose one of the items in the menu, a corresponding fragment should be loaded into the container, replacing the menu. However, this fragment must only take 1/4 of the screen from the left, and the space outside is to be used by some other fragment.
I was thinking about making 3 FrameLayouts, one for the left side, one for the right and one for the entire screen, but this is going to have problems with fragment transactions, since I would have to keep tabs on which fragments are where and remove them by hand.
Basically what I need is some way to change whether my fragments are loaded into a container that takes up full screen, or a container that takes up only some part of the screen. I probably could do it with tons of trail and error and some code, but I bet there is a really easy way to do this in android that I missed.
Instead of trying to dynamically load these fragments into the various containers, I would suggest having two different Activities.
It sounds like the main menu fragment will only ever appear on its own in full screen. So, make that a full Activity (let's call it MainMenuActivity).
The second activity will have two FrameLayouts as it's contents, with one taking up 1/4 of the screen and the other taking up the remaining 3/4. Load this second activity upon choosing a main menu option and populate the fragments in onCreate() of the second activity.
Hitting the back button from the second activity will return the user to MainMenuActivity.

How to call an activity inside a fragment

I have created a fragment inside an Activity, and now I want to open another application/Activity inside this fragment, Result that I want is, both the activities should be seen on the display (it should not open in another window). Please let me know how can I achieve this.?
Nope you cannot achieve this, you cannot start an activity inside a fragment nor you can show a fragment inside a fragment. For showing multiple layouts in a single screen or activity you need to adjust your activity's layout file and arrange different fragments accordingly ...

Android:Saving Fragment State/contents of each views in each Fragment when using ViewPager

I really,really need a serious help over here.
I am creating an android app which uses PagerAdapter to create Fragments in an activity. Different Fragment consists of different views according to need which are created during run time. In the last fragment, I have created a sort of "Submit button", which when clicked, is supposed to get user entered values from each views(like EditText) from all Fragments.
I am using following command to get the views(int the above mentioned button clicklistener):
EditText e = (EditText) (getActivity().findViewById(i));
But its not geting the view with that ID except of last two fragments.
So, I am assuming that, it is saving the state of only last two fragments in that activity.
So, How can I save the 'view states' of all the fragments in the activity??
And Isn't it so that, when a view is created in a Fragment, and is added in its layout , that view is also added in the main activity layout?? Am I understanding it in the wrong way??Please let me know.
To simplify it, my question is:
How can we save the contents, entered by users, in dynamically created EditText in Fragments, created using the ViewPager (So that it can be accessed later)??
When you add a fragment to a FragmentPagerAdapter (assuming you haven't written a custom pager) it is added to the FragmentManager. The Fragment manager is independent of the activity lifecycle so it retains the fragment state.
If you take a look here: http://code.google.com/p/ratebeerforandroid/source/browse/trunk/JakeWharton-ActionBarSherlock/library/src/android/support/v4/app/FragmentPagerAdapter.java?spec=svn42&r=42
You can get an idea of how it works. If you want to save the fragment state for another session you'll need to pull back each fragment (getItem) and either use the onSaveInstanceState or write your own custom function
Hope that helps

How to show different Activities in Fragment implementation?

It is my first time using android fragment. I am following this tutorial to implement a fragment.
Everything is fine with this tutorial, I successfully get the result like below:
In the tutorial, the DetailsFragment simply shows a TextView containing the text of the currently selected item. That's the right part shows just some texts.
My question is how to show different activities on the right side instead of text views.
What I mean is illustrated in the following image, for example, the area of "1" in the image is an activity. How to show different activities when a list item on the left hand side has selected?
You do not show an activity, you show a fragment. Implement the Fragment class instead of the Activity class. Then you build your View just as you would in an Activity. Remember that for instances when you need access to an activity the Fragment class has the convenient getActivity() method.
Use FragmentManager.beginTransaction() to start FragmentTransaction. With that operation you can hide and show new Fragments. It is also managed with the android history stack.
https://developer.android.com/reference/android/app/FragmentManager.html#beginTransaction()
https://developer.android.com/reference/android/app/FragmentTransaction.html
And here is some code: Android Honeycomb: How to change Fragments in a FrameLayout, without re-creating them?

Categories

Resources