20

I am trying to center a TextView in a LinearLayout and it is centering horizontaly but not vertically.

below is my code

 <LinearLayout
        android:orientation="vertical"
        android:layout_width="320dp"
        android:layout_height="50dp"
        android:background="@drawable/rectblack"
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
        android:minWidth="25px"
        android:minHeight="25px">
        <TextView
            android:text="Explode a Vin"
            android:gravity="center"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tvExVin" />
    </LinearLayout>

In the end I would like it to be centered vertically to the left of the Linearlayout, if someone could figure out how to do that, that would be great.

Thanks!

BC2
  • 892
  • 1
  • 7
  • 23
user222786
  • 547
  • 4
  • 7
  • 17

3 Answers3

50

You have the gravity and layout_gravity reversed. You can have multiple values on the gravity attribute separated by "|". Try this:

 <LinearLayout
        android:orientation="vertical"
        android:layout_width="320dp"
        android:layout_height="50dp"
        android:background="@drawable/rectblack"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal"
        android:gravity="center_vertical|left"
        android:minWidth="25px"
        android:minHeight="25px">
        <TextView
            android:text="Explode a Vin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tvExVin" />
    </LinearLayout>

For clarification:

android:layout_gravity="center_horizontal" will center the LinearLayout horizontally in its parent.

android:gravity="center_vertical|left" will center the TextView (and any other children) vertically inside the LinearLayout and align it to the left.

Community
  • 1
  • 1
invertigo
  • 6,336
  • 5
  • 39
  • 64
  • 1
    and just for the sake of sharing knowledge, you could remove the `android:gravity` from the linear layout and put `android:layout_gravity` on the text view, with the same "center_vertical|left" value and get the same result. – invertigo Jul 10 '13 at 20:25
  • so would putting `layout_gravity=center_vertical|left"` in the textview and removing it from the linear layout have the same effect? – user222786 Jul 10 '13 at 20:54
  • 2
    Quick note: Android Studio recommends using "start" instead of "left" on gravities to allow for right-to-left locales. – iandouglas Oct 20 '16 at 16:34
4

Try to change your gravity to

android:layout_gravity="center_vertical"

gravity is used to position the content inside your View and layout_gravity is used to position the View within its parent.

But I'm confused by "In the end I would like it to be centered vertically to the left of the Linearlayout". But from the way it sounds, my answer should give you what you want.

Also, unless there is a very good reason, you shouldn't need to use a LinearLayout, or any parent, for a single child. If you can give a better explanation of what you want or even a screenshot then we may be able to help with a better solution.

This will give you your desired effect

<LinearLayout
    android:layout_width="320dp"       <!-- removed orientation (horizontal by default)  -->
    android:layout_height="50dp"
    android:background="@drawable/rectblack"
    android:layout_marginTop="10dp"
    android:layout_gravity="center"
    android:minWidth="25px"
    android:minHeight="25px">
    <TextView
        android:text="Explode a Vin"
        android:layout_gravity="center_vertical"       <!-- changed gravity here  -->
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"           <!-- change to wrap_content -->
        android:id="@+id/tvExVin" />
</LinearLayout>
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • 1
    Change your `orienation` to "horizontal" (or removing it will do the same thing). I just tried it and it centered it vertically and to the left side of the `LinearLayout` – codeMagic Jul 10 '13 at 20:28
  • What I was asking was I want to put the text view in the center vertically and to the left within Linearlayout – user222786 Jul 10 '13 at 20:31
  • 1
    the `layout_gravity` on the LinearLayout will center that entire layout on whatever parent it is sitting on, which may or may not be your desired effect – invertigo Jul 10 '13 at 20:33
  • @codeMagic oops, when I did that it was centered verticaly but not to the left – user222786 Jul 10 '13 at 20:35
  • What do you mean "not to the left"? You want it outside of the `LinearLayout`? Did you remove `android:gravity="center"`? – codeMagic Jul 10 '13 at 20:35
  • look at my post below. you need to set the width of your textview to wrap_content, and the gravity on your linear layout (or layout_gravity on your textview) to `"center_vertical|left"` – invertigo Jul 10 '13 at 20:37
  • You need to change the `height` to `wrap_content` of your `TextView`. Its in my answer but I forgot to make a comment about it, sorry. I will update. – codeMagic Jul 10 '13 at 20:39
  • @invertigo My apologies, I will try to make my self more clear, I want the Linear Layout to be centered horizontaly on the screen, then the textview within the linear layout centered vertically to the left within the Linear Layout – user222786 Jul 10 '13 at 20:41
  • 1
    to get the LinearLayout centered horizonally, set `android:layout_gravity="center_horizonal"` on the LinearLayout. I've updated my post below per that requirement. – invertigo Jul 10 '13 at 20:43
  • Did you try my update? And what is the parent of the `LinearLayout`? – codeMagic Jul 10 '13 at 20:46
  • here is the docs for gravity, maybe it's a syntax error that you are getting now http://developer.android.com/reference/android/view/Gravity.html#CENTER_HORIZONTAL – invertigo Jul 10 '13 at 20:50
  • @invertigo ThanK! could you just clear up what the difference between `gravity` and `gravity_layout` is for me? – user222786 Jul 10 '13 at 20:50
  • as codeMagic stated "gravity is used to position the content inside your View and layout_gravity is used to position the View within its parent." so setting gravity on the LinearLayout will apply to the children (your textview), while layout_gravity applies to the LinearLayout. – invertigo Jul 10 '13 at 20:52
  • Thanks, its happened to me before on an accepted answer with 4 upvotes. Oh well, just curious – codeMagic Jul 10 '13 at 21:01
0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_horizontal|center_vertical"
    android:gravity="center_vertical|center_horizontal"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="AppLock Demo"
        android:textSize="30dp" />

</LinearLayout>
  • -1 People like that are known for giving code only answers. Its very annoying. They flaunt their code as if it is an art right next to Da Vinci's. I believe it makes them look smart somehow (I don't claim I look smart by posting this rant either). The quality of answers in Android SO is so poor it is becoming more unreliable due to this people giving answers like this. I hope the moderators can address this. "NO CODE ONLY ANSWER PLEASE" – Neon Warge Aug 17 '16 at 04:09