Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix a view from going off of a page when it is constrained to a card view

I have recently converted over an android project into androidx and I am having issues with trying to stop views going off of the page. My layout is as follows, a Constraint Layout that contains a Card View and a Text View. Within this Card View I have a Constraint Layout that contains the Text View. Outside of the Card View I have a Button which is constrained to the Card View.

The issue with this is that when I run the program the Card View goes off of the screen and the button moves all the way to the top even though they are all constrained properly.

I will show below the code I am using to achieve this and an image of my result on the emulator.

Here is the layout in the editor [1]: https://imgur.com/a/0FLa1IK

Here is the layout shown on the Emulator [2]: https://imgur.com/a/SsvKiOG

Any help would be greatly appreciated.

<androidx.constraintlayout.widget.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:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.cardview.widget.CardView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/materialButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="16dp"
                android:layout_marginEnd="16dp"
                android:layout_marginBottom="16dp"
                android:text="Text View Test"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.cardview.widget.CardView>

    <Button
        android:id="@+id/materialButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="256dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

like image 961
OPWagg Avatar asked May 28 '19 15:05

OPWagg


1 Answers

Your layout is getting pushed because of this line:

android:layout_marginBottom="256dp"

Different phones got different screen size, in your layout you are using fixed size on your view ("256dp") and it makes your layout non-responsive.

So what may look good on your preview may not look the same on a real phone.

If you want to place some view anywhere on your screen and keep it responsive I recommend using guidelines, something like this:

<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">

<androidx.cardview.widget.CardView
    android:id="@+id/cv_text"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_margin="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toTopOf="@+id/materialButton"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Text View Test"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.cardview.widget.CardView>

<Button
    android:id="@+id/materialButton"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toTopOf="@+id/guideline13"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline13"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent=".5" />

</androidx.constraintlayout.widget.ConstraintLayout>

And it will look like this:

enter image description here

like image 141
Tamir Abutbul Avatar answered Sep 20 '22 00:09

Tamir Abutbul