I have an Activity and two Fragment layouts and classes.
All I need to do is that when I change the screen orientation, reorganize the same content differently.
My question is where should I put my code?
Which class Activity class or Fragment class?
If I code in Fragment class, should I put code in both classes, or if I use main Activity, how would I find objects (like TextViews) from each layout?
You put the code where the views are... It's really that simple.
res/layout-land should keep the exact same view IDs.
For example, MainActivity would load both res/layout/activity_main.xml and res/layout-land/activity_main.xml automatically for you depending on orientation.
findViewById will work appropriately if you don't change any ID values
There is a whole documentation page on this. Notice they use retained Fragments, but that is not necessary.
Also Android Studio: Creating landscape layouts
Based on Comments what i have understood so far.
You have an activity and you have two fragments. These two fragments have different layouts and you want to show these layouts based on the orientation of the device.
Well there are two approach for this problem
1) You want to achieve this using fragments
Create an abstract class for Fragment put common functionality in base class and provide different implementation in different Fragment child class
You can refer this
2) Second approach
It can be achieved in a much simpler way without using fragment.
Create a layout-land directory and put the landscape version of your layout XML file in that directory.
See this thread in stackoverflow
Hope it helps.
Related
I want to divide my screen into four parts and add activities in each part. I am not interested in using fragment. Each activity should behave independent of other. Attached photo is showing what I exactly want to do.
In each child activity I want to add VideoView or WebView depending on the selection from menu item.
How can I do it. I didn't find any way to add activity to an activity.
Thanks :)
PS: Activity means Activity not fragment.
i strongly recommend 'using fragments' in your case as ActivityGroup is deprecated in API 13..
The only way you can do it by using fragments. Here is the simple example on how to add multiple fragments on single activity Link
Edit link
You can achieve this design by using four fragments, one for each child view inside a single activity.
The recommended way is to create a single XML layout for your activity, and create four fragments inside that layout.
Edit: source : http://developer.android.com/training/basics/fragments/creating.html last parargraph says :
Note: When you add a fragment to an activity layout by defining the
fragment in the layout XML file, you cannot remove the fragment at
runtime. If you plan to swap your fragments in and out during user
interaction, you must add the fragment to the activity when the
activity first starts, as shown in the next lesson.
Thanks in advance.
You can use fragments in two ways ,
Static Fragments
Here you can define the fragment in whatever the layout file you need. Only thing is, that defined fragment can not be change at the run time. So, re-usability will be issue here, you cant take the advantage of the re-usability of fragments in this case.
Dynamic Fragments
Here you can define a place holder(frame layout etc) on your layout and you can add/replace whatever the fragment you expect at any time while your activity is running. This ensure the re-usability.
Also you can use backStack if back navigation is required.
So, it depend on your requirement.
If the fragments are defined in Xml you won't be able to change them at runtime; in that case I'd suggest using an <include layout="#layout/my_inner_layout"/> tag, that is not only better performance-wise as it is easier to use (you can access the views defined in my_inner_layout.xml using findViewById() method from the host activity).
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've read Activity and Fragment sections of Android API and many Q&A on these two, but I still don't have a clear understanding of some points.
When android SDK creates an activity for me, it also creates a fragment for it. From what I know I can bind several fragments to one activity and switch them as I like. But I don't understand if I ever have to add any components to activity xml file? I mean all layouting and buttons are in fragment xml. In what situations and why would I need to use activity's xml file? Can I make buttons, for instance, both in activity xml and fragments xmls? Is it a good practice?
What logic should be generally implemented in activity class and what in its fragment? For example, I think that Fragment class is needed only to get data from UI and pass it to activity. Is that right?
Thank you for your patience
An activity is basically a screen in your application (think of it as like a webpage) with all associated logic. A fragment is a sub-activity, a portion of an activity with its own set of logic and UI.
You should use a fragment when either you use the same UI in multiple activities, when you want large parts of your activity's UI to change in and out as people take actions, or when you want to rearrange large parts of your UI in different layouts. When none of those are true you should ignore fragments and just use activities directly. In my experience it ends up being about 80% activities and 20% fragments, but it really depends on what type of apps you're developing- tablet apps use a lot more fragments, for example, because they have more screen real estate.
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.