Raj
Raj

Reputation: 2398

Aligning baseline of two text views in Android

please find below my layout & its result ; I need the text 'Text message' to align on the baseline of the text 'Header'(Please find below the code and snapshot). Would be glad if someone can give me direction

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1.0"
android:baselineAligned="true"
>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.2"
    android:background="@color/custom_orange"
    android:gravity="center"
    >


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Header"
        android:textSize="25sp"
        android:id="@+id/title"
        android:gravity="center"
        android:background="@color/custom_red"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Text message"
        android:textSize="10sp"
        android:id="@+id/language"
        android:gravity="center"
        android:background="@color/custom_green"/> 
</LinearLayout>

</LinearLayout>

This is the result

enter image description here

This is what I need

enter image description here

Upvotes: 23

Views: 21303

Answers (4)

Francis
Francis

Reputation: 7104

For ConstraintLayout just use this to align to another TextView's baseline:

app:layout_constraintBaseline_toBaselineOf="@id/textView"

Upvotes: 8

Md Raihan Ahmed
Md Raihan Ahmed

Reputation: 81

On Component tree select both component you want to be in same base, right click and then select show baseline. Just drag baseline of two components.

Upvotes: 2

Thiago Souto
Thiago Souto

Reputation: 771

Just use the android:gravity="bottom" on LinearLayout(The linear with the both textviews) and android:layout_height="wrap_content" in both TextViews.

Upvotes: 4

x11
x11

Reputation: 674

Use RelativeLayout as a container for TextViews and add the attribute

android:layout_alignBaseline="@+id/title"

to id/language TextView

Upvotes: 38

Related Questions