Z-ordering of child views with respect to their parent - android

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>

Related

ViewBinding - How to switch/choose layout dynamically (<include>)?

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

How to show fragment privew in FrameLayout

I want show a preview of my fragment inside FrameLayout.
In fact I want display it only in android studio, And I do not want to render it.
My solution:
In Xml I load home_fragment preview using include tag:
<FrameLayout
android:id="#+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/nav_view"
android:layout_below="#id/my_toolbar">
<include layout="#layout/home_fragment" />
</FrameLayout>
In onCreate method i remove all child view of FrameLayout:
FrameLayout frameLayout = findViewById(R.id.main_frame);
frameLayout.removeAllViews(); // remove static preview in activity_main.xml
Everything works right now.
But I think there's a better way.
You probably want the tools:showIn attribute.
So if you have your main_frame.xml that <include>s your home_fragment.xml, in home_fragment.xml just point to the container layout:
<FrameLayout
tools:showIn="#layout/main_frame"
android:id="#+id/home_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Other stuff -->
</FrameLayout>
Hope that helps!

set Argument for fragment in xml layout

i used this layout:
<RelativeLayout 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="sanjaqak.com.karaoke.artist.AllArtistActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="sanjaqak.com.karaoke.Srch.SrchSubFrag"
android:id="#+id/fragment2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
this fragment must have argument to work. but i don't know how can initialize argument for this.
i am very sorry for bad speak English.
AFAIK this is not possible with the current Fragment framework.
In such cases I just create the parent ViewGroup (usually a simple FrameLayout) for the Fragment that needs correct runtime arguments to work, and load that Fragment with those Arguments in the Activity's / parent Fragment's setup phase (usually in .onCreate()) into that ViewGroup.

Android: Two FragmentActivities in one layout

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.

Z-Layering Fragments in Android

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.

Categories

Resources