Liondancer Liondancer - 5 months ago 9
Android Question

Aligning button and textview components vertically android

I am trying to make my

Button
component and my
TextView
component aligned vertically and also centered. Also a bit of spacing between each other. Right now they are simply on top of each other.

enter image description here

XML:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dueDateSection"
android:layout_below="@+id/editSection"
android:paddingBottom="@dimen/activity_edit_item_layout_vertical_margin"
android:paddingTop="@dimen/activity_edit_item_layout_vertical_margin"
android:gravity="center_vertical">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnDatePicker"
android:text="Select Date"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btnDatePicker"
android:textSize="22sp"
android:gravity="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_alignBottom="@id/btnDatePicker" />

</RelativeLayout>

Answer

Use LinearLayout with android:orientation="vertical" attribute

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

<Button 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="B1"/>

<TextView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="YOUR TEXT"/>

</LinearLayout>

this may helps you.