Francisco Romero
Francisco Romero

Reputation: 13189

How can I center a TextView on Android?

I have a simple layout that only has one EditText and one TextView:

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

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

             <TextView
                android:id="@+id/lblName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textSize="25sp"
                android:textStyle="bold" />

             <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textStyle="bold"
                android:textSize="15sp"
                android:layout_marginLeft="5dp"
                android:layout_marginTop="10dp"
                android:id="@+id/txtName"/>
        </LinearLayout>

</LinearLayout>

And I want to center the TextView to the center of the screen of my mobile phone.

I saw that I can replace my LinearLayout to a RelativeLayout as global layout and set the property android:gravity="center" to center it, but I ONLY want to center the TextView and not the EditText, which it's also centered with this function.

Is it possible to center just the TextView without center the EditText?

Note: Of course, I don't want to center it manually with margins.

EDIT: I want that the TextView will be center on the screen and the EditText will be in the nextLine (on the left of the screen). Something like this:

[                WIDHT OF THE SCREEN                ]
                 [TEXTVIEW-CENTER]
[EDITTEXT-LEFT]

Thanks in advance!

Upvotes: 1

Views: 82

Answers (5)

Geetha
Geetha

Reputation: 190

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <TextView
            android:id="@+id/lblName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="25sp"
            android:textStyle="bold"
            android:layout_gravity="center"
            android:text="Sample Text"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textStyle="bold"
            android:textSize="15sp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:id="@+id/txtName"/>
</LinearLayout>

Upvotes: 0

RajSharma
RajSharma

Reputation: 1971

Try changing your inside LinearLayout to something like this.`

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

` It will acquire all the screen space then EditText would be displayed.

If you use wrap_content in your inner Linear_layout then thw width of the inner Linear_layour would be wrapped according to its textView i.e it will act as a parent to textview but child for its outside layout. refer to the image.

enter image description here

but if you chnage its attribute to fill_parent it will fill the width according to its parent. I hope you can understand it now.

Upvotes: 1

N.T.
N.T.

Reputation: 2611

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

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

        <TextView
            android:id="@+id/lblName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="25sp"
            android:textStyle="bold"
            android:text="Test"
            android:layout_gravity="center"/>

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textStyle="bold"
            android:textSize="15sp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:id="@+id/txtName"/>
    </LinearLayout>

</LinearLayout>

Upvotes: 0

Use this :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/hello_world" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10" />

</LinearLayout>

Upvotes: 0

Fareya
Fareya

Reputation: 1563

Change

android:orientation="horizontal"

to

android:orientation="vertical"

Add

android:layout_gravity="center"

in

TextView

Upvotes: 0

Related Questions