Put Elements in Linear Fashion and at the Bottom of the Screen in Android

The best way to put any elements in a linear fashion in Android is to use LinearLayout. Specify the attribute ‘orientation‘ of the LinearLayout to ‘vertical‘.

Now any number of elements put inside the LinearLayout will be in vertical order.

To align the elements at the bottom of the screen, use ConstraintLayout and add constraints to the LinearLayout to attach it with the bottom of the screen.

Find the sample code below where there are two TextView(s) placed one below the other and the container LinearLayout is attached to the bottom of the screen.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">

<TextView
android:id="@+id/element1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is element 1" />

<TextView
android:id="@+id/element2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is element 2" />

</LinearLayout>

</android.support.constraint.ConstraintLayout>

Leave a Reply

Your email address will not be published. Required fields are marked *