It's is possible to "stack" fragments on top of each other?
Just tried to do this without really thinking about it and everything got kinda crazy.
I can (and probably should) create a ViewGroup instead if it's not possible, but I was just wondering.
Yes, it's possible. E.g. by wrapping them inside a FrameLayout like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.package.FragmentOne"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<fragment android:name="com.package.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
The z-index depends on the order of the children within the layout. In this example FragmentTwo is above FragmentOne because it's the second child.
Related
I was planning on having two layout versions for the profile fragment, one for new user and one for logged in user.
How would I dynamically switch/choose the necessary layout in the Fragment's onCreateView() method?
The most straight-forward idea that comes to mind is to use two <include> layouts and hiding one of them in onCreateView() depending on variables. But I was wondering if there is a smarter approach.
Current layout xml:
The main content is inside the second FrameLayout and I'd like to have two versions to choose from.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.MainActivity">
<ImageView
android:id="#+id/profile_monk_head_imageview"
android:layout_width="match_parent"
android:layout_height="200dp"
android:contentDescription="#string/profile_monk_image_content_desc"
android:src="#drawable/monk_head" />
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- This is what I want to switch out -->
<FrameLayout ... >
</androidx.core.widget.NestedScrollView>
</FrameLayout>
Use DataBindingUtil version inflate if layoutId is unknown in advance ( a.k.a dynamic binding). Otherwise, use the generated Binding's inflate method to ensure type-safe inflation.
Crosslink reference
In my application I need to use the RecyclerView in two different activities.
I was thinking to extend the layout, because the base layout (the recycler view) is the same for the two activities, but I don't know if it's possible.
With "to extend a layout" I mean something like this:
If this is my base_layout.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recyclerView">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
I would like to do something like this in my child_layout.xml:
include "base_layout.xml"
<Button
android:id="#+id/btnAddVehicle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:onClick="launchAddVehicleActivity"/>
so, the activity which uses chid_layout.xml will have got a RecyclerView and a Button inside.
Is it possible to do something like that?
You could create a generic layout, then add it on your layout's activities.
Generic Layout
my_generic_layout.xml
<RelativeLayout
android:id="#+id/generic_layout">
<RecyclerView/>
</RelativeLayout>
Layout's Activities
<RelativeLayout
android:id="#+id/layout_1">
<include layout="#layout/my_generic_layout" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/layout_2">
<include layout="#layout/my_generic_layout" />
</RelativeLayout>
just use this <include layout="#layouts/chid_layout" />
If you want to add the button inside the layout you need to write a Custom Component. See Android Documentation here https://developer.android.com/guide/topics/ui/custom-components.html#compound
Basically you could create a class which inherits from RelativeLayout which inflates base_layout.xml
Background
In my app I have a lot of fragments that share the same static overlay. Each fragment can be resized dynamically (i.e. its weight may change) so I want the size of my overlay to be in sync with it. Therefore it makes sense to either make them siblings or, as it occurred to me, put one inside another. The first approach works fine but it implies introducing one extra ViewGroup which seems redundant (or does it not?). The latter is the one I'm having problems with.
Problem
Consider these two layouts.
Container R.id.container is where I put my fragment in runtime with FragmentManager.
Fragment R.id.overlay is my overlay.
The difference is which one is the parent.
Layout A.
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment class="com.example.OverlayFragment"
android:id="#+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Layout B.
<fragment class="com.example.OverlayFragment"
android:id="#+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</fragment>
In both cases my overlay ends up below container (by z-axis). That is, container keeps overlapping it regardless of its role in hierarchy.
What are the rules for defining views's z-order in situations like this? Is it just the order of their initialization? I couldn't google it out.
Thanks.
From documentation:
When the system creates this activity layout, it instantiates each
fragment specified in the layout and calls the onCreateView() method
for each one, to retrieve each fragment's layout. The system inserts
the View returned by the fragment directly in place of the
element.
So I think you should use something like this:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<fragment class="com.example.OverlayFragment"
android:id="#+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
I'm trying to include the following layout twice:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.viewpagerindicator.TabPageIndicator
android:id="#+id/indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
like the following
<include
android:id="#+id/include1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="#layout/view_pager" />
<include
android:id="#+id/include2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="#layout/view_pager" />
Actually, the second view pager does not work if I do it like that... The second view pager always stays empty... If I copy my view_pager layout and change the ids in this layout (pager to pager2 and indicator to indicator2) everything works fine. Is there a better way to do that? Copying layouts to achieve that seems to make the include useless for multiple includes of the same layout....
I'm getting the references correctly I think, but though it just does not work if I include the same layout...
pager1= (ViewPager)(findViewById(R.id.include1).findViewById(R.id.pager));
pager2= (ViewPager)(findViewById(R.id.include2).findViewById(R.id.pager));
Everything works perfectly if I copy the layout...
Edit:
I think it has to do with the FragmentManager, because the view pagers have the same id... But I don't know how to solve that correctly...
Yes it can be done. You can inflate the layout many times, but you have to make the inclusion programmatically. See the answer to same kind of question.
Is it somehow possible to host two FragmentActivities in one layout?
For example, if I have following layout, can I host one FragmentActivity in container1 and one FragmentActivity in container2?:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<FrameLayout
android:id="#+id/container1"
android:layout_width="match_parent"
android:layout_height="150dp" >
</FrameLayout>
<FrameLayout
android:id="#+id/container2"
android:layout_width="match_parent"
android:layout_height="150dp" >
</FrameLayout>
If this is not possible, can I have one FragmentActivity that loads two different fragments and displays one in container1 and one in container2?
You are a bit confused. Layouts doesn't contain activities, activities contain a layout. In your layout you can put Views and fragments, so yes, it is possible to load two fragments in one FragmentActivity, or rather it is the only way to achieve what you want.