Search code examples
javaandroidandroid-constraintlayoutandroid-relativelayout

RelativeLayout doesn't locate well in ConstraintLayout


I think I did everything true but the RelativeLayout in ConstraintLayout don't do what i command. When i add something under the RelativeLayout it supposed to be smaller and make room to other items but it doesn't matter what i do, it always push them out of the screen.

enter image description here

The left and right arrows should be below of RelativeLayout and adView should be below of arrows.

<?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="wrap_content"
    android:id="@+id/relativeLayout"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingTop="25dp"
    android:paddingBottom="25dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp">


    <RelativeLayout
        android:id="@+id/container_main"
        android:paddingBottom="16dp"
        android:background="@drawable/card_bg"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_margin="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">
        <TextView
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/title_text_color"
            android:id="@+id/activity_main_text_view_id"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="40dp"
            android:layout_marginStart="40dp"
            android:layout_marginTop="20dp"
            android:text="simple Title Text"/>

        <TextView
            android:padding="10dp"
            android:gravity="center"
            android:textColor="#000000"
            android:lineSpacingExtra="8dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/factTextView"
            android:textSize="18dp"
            android:layout_alignStart="@id/activity_main_text_view_id"
            android:layout_alignLeft="@id/activity_main_text_view_id"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="8dp"
            android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."/>

        <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000"
            android:id="@+id/activity_main_did_u_know"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:text="Category"
            android:layout_marginTop="20dp"/>
        <ImageView
            android:layout_marginLeft="25dp"
            android:id="@+id/activity_main_image_view"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:clickable="true"
            android:src="@drawable/ic_share_black_24dp" />

        <ImageView
            android:layout_marginRight="25dp"
            android:id="@+id/activity_main_fav_button"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:clickable="true"
            android:src="@drawable/ic_favorite_border_black_24dp"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/LeftRight"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@+id/container_main"
        app:layout_constraintBottom_toTopOf="@+id/adView"
        >
        <ImageView
            android:layout_marginLeft="25dp"
            android:id="@+id/activity_main_left_button"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:clickable="true"
            app:layout_constraintRight_toLeftOf="@+id/activity_main_right_button"
            android:src="@drawable/ic_left" />

        <ImageView
            android:id="@+id/activity_main_right_button"
            android:layout_width="30dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginRight="25dp"
            android:clickable="true"
            android:src="@drawable/ic_right"
            app:layout_constraintLeft_toRightOf="@+id/activity_main_left_button" />

    </RelativeLayout>

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        ads:layout_constraintBottom_toBottomOf="parent"
        ads:layout_constraintTop_toBottomOf="@id/LeftRight"
        ads:layout_constraintRight_toRightOf="parent"
        ads:layout_constraintLeft_toLeftOf="parent"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>
</androidx.constraintlayout.widget.ConstraintLayout>

I am open the any kind of advice. Thanks.


Solution

  • I prefer to use whole Constraint Layout in your xml. Checkout below xml, hope the same what you want to design.

    <?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="match_parent"
        android:padding="16dp">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/container_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="16dp"
            android:background="@drawable/card_bg"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <TextView
                android:id="@+id/activity_main_text_view_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="16dp"
                android:text="simple Title Text"
                android:textColor="@color/title_text_color"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
            <TextView
                android:id="@+id/activity_main_did_u_know"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="16dp"
                android:text="Category"
                android:textColor="#000000"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
            <TextView
                android:id="@+id/factTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:lineSpacingExtra="8dp"
                android:padding="10dp"
                android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
                android:textColor="#000000"
                android:textSize="18dp"
                app:layout_constraintBottom_toTopOf="@id/activity_main_image_view"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/activity_main_did_u_know" />
    
            <ImageView
                android:id="@+id/activity_main_image_view"
                android:layout_width="30dp"
                android:layout_height="40dp"
                android:layout_margin="16dp"
                android:clickable="true"
                android:src="@drawable/ic_share_black_24dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/factTextView" />
    
            <ImageView
                android:id="@+id/activity_main_fav_button"
                android:layout_width="30dp"
                android:layout_height="40dp"
                android:layout_margin="16dp"
                android:clickable="true"
                android:src="@drawable/ic_favorite_border_black_24dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toBottomOf="@id/factTextView" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/LeftRight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toTopOf="@id/adView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/container_main">
    
            <ImageView
                android:id="@+id/activity_main_left_button"
                android:layout_width="30dp"
                android:layout_height="40dp"
                android:layout_margin="25dp"
                android:clickable="true"
                android:src="@drawable/ic_left"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
            <ImageView
                android:id="@+id/activity_main_right_button"
                android:layout_width="30dp"
                android:layout_height="40dp"
                android:layout_margin="25dp"
                android:clickable="true"
                android:src="@drawable/ic_right"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintLeft_toRightOf="@+id/activity_main_left_button"
                app:layout_constraintTop_toTopOf="parent" />
    
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
        <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
            ads:layout_constraintBottom_toBottomOf="parent"
            ads:layout_constraintLeft_toLeftOf="parent"
            ads:layout_constraintRight_toRightOf="parent"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    For more see below view .

    enter image description here