Android - Center Textview in LinearLayout
Asked Answered
S

3

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!

Streamliner answered 10/7, 2013 at 20:13 Comment(1)
remove these two lines from linearlayout android:minWidth="25px" android:minHeight="25px"Bur
K
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.

Kerianne answered 10/7, 2013 at 20:21 Comment(3)
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.Kerianne
so would putting layout_gravity=center_vertical|left" in the textview and removing it from the linear layout have the same effect?Streamliner
Quick note: Android Studio recommends using "start" instead of "left" on gravities to allow for right-to-left locales.Resistor
N
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>
Norvall answered 10/7, 2013 at 20:19 Comment(14)
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 LinearLayoutNorvall
What I was asking was I want to put the text view in the center vertically and to the left within LinearlayoutStreamliner
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 effectKerianne
@Norvall oops, when I did that it was centered verticaly but not to the leftStreamliner
What do you mean "not to the left"? You want it outside of the LinearLayout? Did you remove android:gravity="center"?Norvall
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"Kerianne
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.Norvall
@Kerianne 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 LayoutStreamliner
to get the LinearLayout centered horizonally, set android:layout_gravity="center_horizonal" on the LinearLayout. I've updated my post below per that requirement.Kerianne
Did you try my update? And what is the parent of the LinearLayout?Norvall
here is the docs for gravity, maybe it's a syntax error that you are getting now developer.android.com/reference/android/view/…Kerianne
@Kerianne ThanK! could you just clear up what the difference between gravity and gravity_layout is for me?Streamliner
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.Kerianne
Thanks, its happened to me before on an accepted answer with 4 upvotes. Oh well, just curiousNorvall
P
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>
Pasho answered 30/7, 2015 at 10:24 Comment(1)
-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"Hepatitis

© 2022 - 2024 — McMap. All rights reserved.