Allay Khalil
Allay Khalil

Reputation: 693

How to set text in center of text view

I am making the list view in which I have a image view and to its left I have to align the TextView which should have text in the center of it not the bottom of it. I have set the gravity but its only looking good in the design but when I run my app and data is fetched in list view then the text gets set in the bottom .

I will show you what I mean by this.But first let me show you how I am doing to align this

<RelativeLayout
                android:id="@+id/block_address"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="2dp"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/iv_address"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/pin" />

                <TextView
                    android:id="@+id/row_address"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_alignBaseline="@+id/iv_address"
                    android:layout_alignBottom="@+id/iv_address"
                    android:layout_marginLeft="3dp"
                    android:layout_toRightOf="@+id/iv_address"
                    android:gravity="center|center_vertical"
                    android:text="Address Goes here"
                    android:textColor="#AAAAAA" />


            </RelativeLayout>

and this is how I am getting this

enter image description here

And this is what I want it to be

enter image description here

as you can see in the second picture I want this type of adjustment. and in design preview it is showing me design just like it is expected. But When I run the app it just showing me text like i showed you in picture one.

Please tell me what i am doing wrong and How to constrict this not to get out of allignment.

Upvotes: 0

Views: 1021

Answers (5)

pratham kesarkar
pratham kesarkar

Reputation: 3818

Use LinearLayout Instead of RelativeLayout as acheiveing something like this is pretty easy in LinearLayout

<LinearLayout android:layout_width="match_parent"
              android:layout_height="150dp"
              android:orientation="horizontal">
    <ImageView
        android:id="@+id/iv_address"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:src="@drawable/andro"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="7"
        android:text="Hello World"
        android:gravity="center"/>
</LinearLayout>

Upvotes: 0

Viveka Patel
Viveka Patel

Reputation: 666

Try LinearLayout instead of RelativeLayout. because its easy to align.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/block_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android">

<ImageView
    android:id="@+id/iv_address"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/logout" />

<TextView
    android:id="@+id/row_address"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginLeft="3dp"
    android:gravity="center|center_vertical"
    android:text="Address Goes here"
    android:textColor="#AAAAAA" />

</LinearLayout>

Upvotes: -1

Android Geek
Android Geek

Reputation: 9225

Replace whole code with :

  <TextView
                        android:id="@+id/row_address"
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:android:drawableLeft="@drawable/pin"
                        android:drawablePadding="10dp"
                        android:gravity="center|center_vertical"
                        android:text="Address Goes here"
                        android:textColor="#AAAAAA" />

Upvotes: 4

Yasir Tahir
Yasir Tahir

Reputation: 800

Replace your TextView with this:

<TextView
                android:id="@+id/row_address"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignTop="@+id/iv_address"
                android:layout_alignBottom="@+id/iv_address"
                android:layout_marginLeft="3dp"
                android:layout_toRightOf="@+id/iv_address"
                android:gravity="center|center_vertical"
                android:text="Address Goes here"
                android:textColor="#AAAAAA" />

Upvotes: 2

FunkyMan
FunkyMan

Reputation: 309

You can add a drawable icon

<TextView
    android:drawableLeft="@drawable/..."/>

Upvotes: 2

Related Questions