Here you can see the split screen design view working when I comment out the FragmentContainerView:
And as soon as I uncomment it, the design view breaks
There are no errors as far as I can tell.
The full xml, just showing the structure and not the sibling content:
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activities.ParametersActivity" android:layout_height="match_parent"
android:layout_width="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/colorLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:orientation="vertical">
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
What's interesting is that the FragmentContainerView does seem to actually be working when I build the app. So I bet this is just an Android Studio bug.
What can I do to debug and resolve this?
The layout error prompted by Android Studio is:-
This can be resolved in the following ways:-
If you are using the JetPack Navigation library and have a navGraph layout file, then specify it under the app:navGraph tag as :
<androidx.fragment.app.FragmentContainerView
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="match_parent"
app:navGraph="#navigation/nav_graph" />
If a single fragment is required to be displayed, then the same can be done by specifying its name under the android:name tag as :
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.example.YourFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
In the latter approach, however, as the error image says, a specific layout can not be shown by FragmentContainerView in edit mode. You can have a preview of the Fragment displayed by tools:layout tag.
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.example.YourFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/fragment_your_fragment"/>
This should solve the problem, the layout is clearly visible on my IDE now!
Related
Prerequisites
main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarFadeDuration="2"
android:scrollbars="vertical|horizontal">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.core.widget.NestedScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
recyclerview_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<...>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</LinearLayout>
another_recyclerview_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#drawable/divider_default_margin"
android:orientation="vertical"
android:showDividers="middle">
<...>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/anotherRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<...>
</LinearLayout>
edittext_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/formTextClearButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/textInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<...>
</androidx.constraintlayout.widget.ConstraintLayout>
Problem description
I am adding at runtime a recyclerview_layout.xml to the container inside main_layout.xml. The recyclerview_layout.xml sometimes contains another_recyclerview_layout.xml, which has a RecyclerView that has multiple view types, one of which is represented by edittext_item_layout.xml. When the latter exists in another_recyclerview_layout.xml and recyclerview_layout.xml is populated with some other items, the whole layout has a weird scrolling behavior, at the time the user taps on one of the TextInputEditTexts. It's like it tries to snap to the focused TextInputEditText and the user cannot scroll away from that view anymore. (See gif below)
Scrolling bug
Already tried solutions
Setting android:focusable="true" and android:focusableInTouchMode="true" to the direct child of the NestedScrollView, as you can already see in recyclerview_layout.xml. Also tried to add these properties to all the other containers (cry)
Setting android:descendantFocusability="blocksDescendants" to the parent of anotherRecyclerView. This solution does not work weel for me, since I need the TextInputEditText to be fully functional.
Listening to SoftKeyboard events and clearing focus as soon as the keyboard hides. This solution rises 2 other issues:
loses scroll position
the layout is still buggy, if the user tries to scroll while the keyboard is visible
Libraries
ConstraintLayout: 2.0.4
RecyclerView: 1.2.0
Material: 1.2.1
Help!
I am running out of ideas, any help is highly appreciated. Thank you!
Android dev beginner here. I've followed a tutorial for fragment-based navigation. I'm running into troubles when changing device orientation. It works fine on a real device, but in the emulator it is broker.
I've read some things about the view being re-rendered on device orientation change, but in my case the entire layout gets messed up, including the top bar as well as the bottom bar (back, etc.). A screenshot and code included below.
Note: layout looks completely fine when seeing it in design tab of XML layout file.
This is what it looks like in a vertical orientation:
Code for main activity and main fragment is below:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
and my main fragment is this one:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainFragment"
android:id="#+id/parentLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp"
android:gravity="center">
...some stuff here...
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
=> this looks incorrect:
android:layout_width="0dp"
android:layout_height="0dp"
Try to follow this example :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.news.ArticleListFragment"
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I have something very strange:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<DrawerLayout
android:id="#+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frameLayoutRoot"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical"
android:visibility="invisible"
android:layout_margin="10dp">
<fragment
android:id="#+id/fooFragment"
android:name="net.mydomain.android.FooFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:fitsSystemWindows="false"
app:itemBackground="#drawable/layout_background"
app:menu="#menu/activity_single_view" />
</DrawerLayout>
</layout>
Please note android:visibility="invisible" of FrameLayout. The normal screen is exactly as expected - nothing shows:
However, picture-in-picture shows the following:
It shows the content (the fragment) outside of FrameLayout that is supposed to be invisible.
Could anyone shed some light on this?
The picture-in-picture is done by following the official guideline. Nothing special.
[Edit] 2019-08-26:
I added
android:layout_margin="10dp"
to the invisible FrameLayout. The result is exactly the same. This means the gray box is the root layout.
While designing the main activity I tried to add two fragments.But the original structure of the fragment is not shown in design tab of activity_main, instead shaded regions are shown for each fragment.How to get original structure of fragments during design?
enter image description here
In your activity_main.xml make sure each of your fragment tags specify:
android:name with a path to your fragment
tools:layout with the fragment's layout
If you're using an include tag then you'll need to use something like tools:showIn=".MainActivity" (docs) in your fragments.
Here's an example:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<fragment android:name="com.example.FirstFragment"
android:id="#+id/firstFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout="#layout/fragment_first" />
<fragment android:name="com.example.SecondFragment"
android:id="#+id/secondFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout="#layout/fragment_second" />
</LinearLayout>
fragment_first.xml
<?xml version="1.0" encoding="utf-8"?>
<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=".FirstFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</FrameLayout>
i have the following layout file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/what_did_you_eat"/>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".AddFoodActivity"
tools:ignore="MergeRootFrame"
/>
</LinearLayout>
FrameLayout will contain a dynamic added fragment, is there a way to preview in the editor the fragment ?
Like we can do when using a <fragment> tag with the tools:layout attribute ?
While it would be useful, there's nothing in the current tools docs that suggests this is possible.