0

In many examples I see something like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="net.simplifiedcoding.bottomnavigationexample.MainActivity">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="56dp"
        android:text="@string/title_home"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:itemIconTint="@color/colorNavIcon"
        app:itemTextColor="@color/colorNavText"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

Why is FrameLayout here? After all, if you remove it, then bottomNavigation will always remain in place, and fragments will occupy the upper area. So why add FrameLayout almost everywhere? thank

aqellaa
  • 41
  • 6
  • Check this [Why is a FrameLayout used for fragments?](https://stackoverflow.com/q/17495199/7666442) – AskNilesh Jul 08 '19 at 12:29
  • @NileshRathod I read it and did not find the answer to my question. – aqellaa Jul 08 '19 at 12:32
  • Sounds like the original guy wanted to use a vertical orientation LinearLayout with the FragmentContainer taking up `layout_height="0dp" layout_weight="1"` in place of the ConstraintLayout, but didn't know about it. Then he wouldn't need to hack in a 56dp margin bottom. – EpicPandaForce Jul 08 '19 at 12:41

2 Answers2

0

Why is FrameLayout here?

FrameLayout is used here to provide the contents. FrameLayout normally can do the work of other layout plus more and it plays well with Fragments too, that's why FrameLayout is commonly used, but you can use any other layout here.

touhid udoy
  • 4,005
  • 2
  • 18
  • 31
  • Why is there even some kind of layout? Why not use ConstraintLayout itself as a container for a fragment? – aqellaa Jul 08 '19 at 12:34
  • @aqellaa Yes, you can use constraint layout here. All the Layouts were not published at once rather the came gradually, when the need is felt by the community. ConstraintLayout has come around, just 3 years ago roughly, but, now it is the recommended layout for all kind of views – touhid udoy Jul 08 '19 at 12:40
  • @aqellaa because he doesn't want the Fragment to cover his bottom navigation, of course. – EpicPandaForce Jul 08 '19 at 12:43
  • @TouhidulIslam I meant, why add another layout (FrameLayout) to the layout as a fragment container, if the main Layout can be used as a container? – aqellaa Jul 08 '19 at 12:58
  • @EpicPandaForce But the BottomNavigationView remains in place if you add a snippet to the main layout. I declared in ConstraintLayout TextView and BottomNavigationView, and when I added a fragment, the textview disappeared, but BottomNavigationView remained in place – aqellaa Jul 08 '19 at 13:01
0
FrameLayout,

is used when you are add content(views) as fragment.