Home > database >  Align a horizontal line above the view at the bottom of the screen in ConstraintLayout
Align a horizontal line above the view at the bottom of the screen in ConstraintLayout

Time:12-03

I have a ConstraintLayout and I want to show a button at the bottom of the screen. The main area of the screen is a RecyclerView in case it matters.
Anyway I would like to have a horizontal line view just above the button.
I tried the following:

<?xml version="1.0" encoding="utf-8"?>
<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"
    >

    <TextView
       android:id="@ id/text"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       app:layout_constraintTop_toTopOf="parent"
       app:layout_constraintStart_toStartOf="parent"
       android:text="Some text here"    
     />
    

    <androidx.recyclerview.widget.RecyclerView
        android:id="@ id/recycler"
        app:layout_constraintTop_toBottomOf="@id/text"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginTop="12dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_marginStart="16dp"
        android:background="@color/red_color"
        app:layout_constraintBottom_toBottomOf="@id/button"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/recycler"
        app:layout_constraintVertical_bias="0.887" />

    <Button
        android:id="@ id/button"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        android:text="Click Here"
        />
</ConstraintLayout>

But the dummy View is not showing up above the button.
What can I do to fix this?

CodePudding user response:

<?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"
        >
    
        <TextView
            android:id="@ id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:text="Some text here"
            />
    
    
        <androidx.recyclerview.widget.RecyclerView
            android:id="@ id/recycler"
            app:layout_constraintTop_toBottomOf="@id/text"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginTop="12dp"
            app:layout_constraintBottom_toTopOf="@ id/guideline"
            android:layout_width="match_parent"
            android:layout_height="0dp"/>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@color/black"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/recycler" />
    
    
        <androidx.constraintlayout.widget.Guideline
            android:id="@ id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.9" />
        <Button
            android:id="@ id/button"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@ id/guideline"
            android:text="Click Here"
            />
    </androidx.constraintlayout.widget.ConstraintLayout>

Here is the working solution this may helps you.

CodePudding user response:

Check this out

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@ id/text"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some text here" />


    <androidx.recyclerview.widget.RecyclerView
        android:id="@ id/recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/red_view"
        android:layout_below="@id/text"
        android:layout_marginTop="12dp" />

    <View
        android:id="@ id/red_view"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:layout_above="@id/button"
        android:layout_marginBottom="5dp"
        android:background="#ff0000" />

    <Button
        android:id="@ id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Click Here" />

</RelativeLayout>
  • Related