1

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!

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167

5 Answers5

1

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.

RajSharma
  • 1,941
  • 3
  • 21
  • 34
  • Thank you very much! It was exactly what I needed! But... can you please explain me what it's the difference between put here `wrap_content` and `fill_parent`? And why with `wrap_content` it is displayed on the left of the screen? Thanks again! – Francisco Romero Aug 19 '15 at 00:33
  • I am glad to hear that it helped you. Please accept this as an answer if I answered your question correctly. – RajSharma Aug 19 '15 at 00:36
  • fill=parent means to fill the parent i.e a layout with fill=parent attribute will try to fill its parent but in case of warp_content it will try to warp it contents i.e in your case your textview size was small it was trying to warp its layout. – RajSharma Aug 19 '15 at 00:38
  • Can you please put a bit elaborate explanation in your answer instead of a comment? I don't really understand what you mean in that comment and I won't have any problem to mark it as accepted answer. – Francisco Romero Aug 19 '15 at 00:44
  • I edited my answer.if you still don't understand please refer to this.http://stackoverflow.com/questions/432763/whats-the-difference-between-fill-parent-and-wrap-content – RajSharma Aug 19 '15 at 01:02
  • 1
    Good answer. But just an observation. Use `match_parent` instead of `fill_parent` as the last one is deprecated. – Mauker Aug 19 '15 at 01:32
  • Now I understand it better but I have one more question: then `wrap_content` it's always displayed on the left of the screen? I mean, the `TextView` could be so big as the content is (wrapping it), but couldn't be center? And just a note: the `TextView` it's also inside the inner `LinearLayout`. Thanks again! – Francisco Romero Aug 19 '15 at 10:38
0
<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>
N.T.
  • 2,601
  • 1
  • 14
  • 20
0

Change

android:orientation="horizontal"

to

android:orientation="vertical"

Add

android:layout_gravity="center"

in

TextView

Fareya
  • 1,523
  • 10
  • 11
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>
  • I edited my question now because I forgot to put on my question that it was wrapped in another `LinearLayout` but according to this I think it should works but it's only on the left of the screen all the time. – Francisco Romero Aug 19 '15 at 00:26
0
<?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>
Geetha
  • 190
  • 9