After having been introduced to Android development and creating a few basic applications, I have begun splitting activities into fragments for reusability. However one thing that I'm still slightly confused about is how the layout of the main activity(which holds the fragments), is affected by the layouts defined for fragment activities and vice versa.
While I believe that the layouts would affect each other based on definitions of height and width for each fragment, the number of fragments in an activity, etc. However Im not sure if there are other rules I am unaware of and I want to know if the layouts specified for the fragments directly affect the way the way the layout of the main activity displays.
For example, the main activity has a RelativeLayout and contains two fragments which have LinearLayout defined in their own separate layout xml files. Do the LinearLayouts affect the way in which the RelativeLayout would normally display and vice versa?
Fragments are basically similar to other view like RelativeLayout or LinearLayout in some ways. For Example they can also have width and height; Fragments can expand on the basis of size of its children. Rest is up on you, how you design layouts (fragments). Fragment is also similar to Activity in a sense that it has its own xml layout and corresponding java class. Primarily, we use fragments in two ways:
i) One or more fragments capturing separate parts (space) of activity at a time.
ii) A particular fragment in activity which is replaced (add,remove,replace) by many other fragments programmatically
You can use fragments statically in the layout files of your activities and dynamically by adding them in containers which you also define in the layout files of your activities.
The fragment view, will be inflated and be displayed in its container. It all depends on how you define your container. If you make it so it match the width of its parent, the layout of the fragment can have a width as much as the parent of the container. It is like putting views into that container.
Related
im developing an application with multiple screens using activities and fragments. The principle behind my decisions to use one or the other are based on the interaction in the application.
I have come to the conclusion that you should use an activity whenever major UI elements remain present after an event has occurred that forces the views inside the UI to change (An example of this can be a tabhost nested in the toolbar styled as the Google Play store android App with multiple fragments as childs. Another example is fragments representing the different rows or clickable elements on a navigation drawer).
So right now im presented with the dilemma of choosing once again between the two (fragment or activity) for a UI element that is going to be present across my whole application. Its triggered by a floating action button that launches a creation tool for an element inside my application and i needed to be accesible across all of my apps screens.
So to sum things up, what i need to know if its better to use a fragment or an activity for an element that is ever present across my whole application.
Thanks in advance
Use Fragments or a compound Views. But both in the right way.
Fragments are thought for reusable combinations of views. Compound Views are Layouts containing views. They are the right way if you want to create views from more primitive views, for example a View containing TextView and a increment- and decrement-button.
Fragments are more Activity-like. They have a real lifecycle. Your description looks like you want to create acitivity-parts. And thats exactly what fragments do.
A Fragment represents a behavior or a portion of user interface in an
Activity. You can combine multiple fragments in a single activity to
build a multi-pane UI and reuse a fragment in multiple activities.
Taken from Android SDK Documentation: Fragments.
When creating an new Android Activity, it used to create an xml file in the layouts folder where I would define the UI.Now it creats two files:
1.Layout file
2.Fragment Layout File.
Could someone explain the difference between the two? Also when trying to add items as listviews, buttons...etc. in which file should I add them to be called in my activity file.
Starting with Android 3.0, Activities may now host Fragments which can be used to develop portions of the UI, and be displayed in different configurations depending on screen size, orientation, and other factors. It is highly recommended to use Fragments in modern Android applications, but is not required.
You can create an Activity layout which will hold one or more Fragments, and then place your UI components in the Fragment's layout. The Activity will load the Fragment, and then the Fragment will inflate the layout you wish to present inside it. You can also dynamically add/remove/swap out different Fragments inside the same Activity, depending on what you wish to display to the user.
You can read more about how to use Fragments here: Fragments | Android Developers
You can also choose to ignore the Fragment design principle, and continue to place all of your layouts into the Activity layout file. In that case, you can delete the Fragment layout.
The default structure of new Android projects has changed since a recent update of the adt:
How it is now: There will be a Fragment "PlaceHolderFragment" created which uses the fragments layout. The other layout is the one that the Activity uses.
How it was before: No Fragment was generated after creating a new project, so there was also no fragment layout needed.
==> You have to decide if you actually want to use Fragments now. If so use the fragment layout and learn how to use Fragments in Android. If you decide that you don't need to use Fragments for now, then you can just delete the PlaceHolderFragment code & delete the fragments layout.
In short, before, it was an activity per its layout file, but now one activity could have more layouts, but the second layout file is automatically generated as a fragment to avoid collision while the android studio is matching the resources, similarly two or more activities could share the same fragment too.
I'm new to Android and a confused about the options to group Views together.
Let's say I want to create a UI where I have 2 sections of controls (one with Buttons and one with text + Spinners) that are above each other in portrait mode and next to each other in landscape mode, and the same goes for the stuff within those sections. Obviously I would like to dynamically change this when the user changes from one mode to another.
So, do I use Fragments within Fragments or use them only the outer section and then a Compound Control for the inner elements? Or are Fragments even necessary and I should better stick to something else? What is the best practice here?
Thank you in advance!
In your example you would create two layout files. One that contained your buttons and the other that contained your spinners.
You would create a 3rd layout file for the portrait orientation and use the include tag to include your other layouts within the 3rd layout. Similarly for your landscape layout, you would include your inner UI layouts in that.
You could then use an Activity or a Fragment which would use only the main layouts and assuming they were placed into the correct layout folders, the fragment/activity would load the correct one based on your orientation.
Fragments within Fragments should be avoided unless you have a specific need for it. It works but in practice managing the lifecycle becomes tiresome.
Is it possible to have two fragments - one on the left which controls the one on the right, and dock and undock the left fragment such that on docking the one on the left, only the fragment on the right occupies the screen ? If so how?
You can create a horizontal linear layout as your main layout for your activity and within that layout add two linear layouts that will be the place holders for your two fragments ie leftLinLayout and rightLinLayout. When the activity loads add the two fragments dynamically to the two layouts using a FragmentTransaction.
Within the fragments it is possible to get a reference to other fragments since you have method getActivity() then you call the fragment manager and find the fragment you want to manipulate or remove. However this is not desirable. The better solution would be to build a callback interface that the host activity has to implement so that it becomes a listener to your fragment events and then you let the activity add/remove the desired fragments. A good example of this implementation is the newsreader app in the android developer reference http://developer.android.com/training/multiscreen/index.html .
I am thinking of an app in which I will be programatically able to display some fragments according to metadata I have stored somewhere. Up to know, I have been able to find out, that each fragments lies in corresponding FrameLayout, or especially, when I create activity with one FrameLayout, I am able to store there only one Fragment at a time, no matter what kind is it. Problem is, however, with situation, when my metadata declares I have to put 3 fragments into my activity, while there is only one FrameLayout.
I see two possible solutions:
1) making several FrameLayout and in final stage, some of them will be used or not
2) somehow join multiple fragments to fit into one available FrameLayout
I don't like solutuion 1) and I don't know how to achieve 2). How to you see it? Is it possible to dynamically add multiple Fragments into activity with one Frame Layout?
In your situation where you require more number of Fragments to be added to your screen avoid using the FrameLayout, you will not be able to achive it. There are several factors that define how you layout multiple Fragments
Are all the fragments of the same size?
Should the fragments be place in a particular order?
First Create an empty parent layout (Layout class is left to your choice except FrameLayout). Next define your child view for your parent layout using a seperate xml file this file must contain your fragment place holder. Using the inflator inflate your child view and assign a Fragment to the inflated layout and eventually add it to your parent layout, through this way you can active what you are looking for. Just remember as you inflate your layout do mention its layout size so that you achive the kindly of layout you want.