Search code examples
androidandroid-layoutandroid-fragmentsandroid-constraintlayoutandroid-relativelayout

How to nest a constraint layout in a frame layout in xml?


Hi I'm new to Android Studio and currently in my activity_main xml I have this constraint layout with a few text views.

<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/country_detail"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="edu.monash.fit2081.countryinfo.CountryDetails">

<TextView
    android:id="@+id/textView"
    android:layout_width="150dp"
    android:layout_height="25dp"
    //some code here

Now I want to change it to a fragment and make that activity as the bottom fragment.I made my fragment like this but how do nest the layout above into my frame layout?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/content_main"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                tools:context="edu.monash.fit2081.database_4.MainActivity">

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:id="@+id/fragment_top"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:background="@drawable/fragment_border"
        />

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                 android:id="@+id/country_detail"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:layout_below="@id/fragment_top"
                 android:background="@drawable/fragment_border"
        />

    </RelativeLayout>

Any help will be appreciated!


Solution

  • The FrameLayout is a ViewGroup just like RelativeLayout:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                     android:id="@+id/country_detail"
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"
                     android:layout_below="@id/fragment_top"
                     android:background="@drawable/fragment_border"
            >
        <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/country_detail"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="edu.monash.fit2081.countryinfo.CountryDetails">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="150dp"
            android:layout_height="25dp"
            />
    
    </FrameLayout>