Riskhan
Riskhan

Reputation: 4470

Center Align TextView in LinearLayout in android

I want to align textview to vertically centre respect to EditView

enter image description here

my layout as follows

<?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="match_parent"
android:orientation="vertical" >

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"
            android:text="text1"/>

        <EditText
            android:id="@+id/setup_homeid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:maxLength="5"
            android:inputType="number"
            android:hint=""/>           
    </LinearLayout>

    </ScrollView>

how can i achieve it ?

thanks in advance

Upvotes: 4

Views: 30262

Answers (6)

Maypeur
Maypeur

Reputation: 396

In my case, I must set the weight property to 1 else the layout_gravity do not take effect.

android:layout_weight="1"

Upvotes: 2

Silambarasan Poonguti
Silambarasan Poonguti

Reputation: 9442

try this..

      <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/sampleId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Center String"
                android:textColor="#FFFFFF"
                android:textSize="14sp"
                android:typeface="sans" />
        </LinearLayout>

Upvotes: 1

TNR
TNR

Reputation: 5869

Add android:layout_gravity="center_vertical" to your TextView tag. Change your TextView tag as below

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:layout_weight="1"
        android:text="text1"/>

Upvotes: 3

Egor
Egor

Reputation: 40228

Simply change TextView's android:layout_height to match_parent, this should do the job. Hope this helps.

Upvotes: -1

Nermeen
Nermeen

Reputation: 15973

Using center_vertical:

 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left|center_vertical"
        android:layout_weight="1"
        android:text="text1"/>

Upvotes: 2

Andro Selva
Andro Selva

Reputation: 54330

Change this android:layout_gravity="left"

into this,

 android:layout_gravity="left|center_vertical" 

or

android:layout_gravity="center_vertical"

for your TextView.

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical" 
            android:layout_weight="1"
            android:text="text1"/>

Upvotes: 13

Related Questions