kTekkie kTekkie - 3 months ago 28
Android Question

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

Answer

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"/>