Android : Align a text at left and center the textview in his parents view
Asked Answered
E

7

18

Hello i'm facing a little problem

I'm using GRAVITY LEFT to make my text an aligment for the left side of a view but I want to center inside the textview but also align on the left part

Here's what I have now:

            ___________________________________
_ _ _ _ _ _| aaaaaaaaaaaaa                     |_ _ _ _ _ _
_ _ _ _ _ _| aaaaaaaa                          |_ _ _ _ _ _
_ _ _ _ _ _| aaaaaaaaaaaaaa                    |_ _ _ _ _ _
            -----------------------------------

What I want is:

            ___________________________________
_ _ _ _ _ _|           aaaaaaaaaaa             |_ _ _ _ _ _
_ _ _ _ _ _|           aaaaaaaa                |_ _ _ _ _ _
_ _ _ _ _ _|           aaaaaaaaaaaaaa          |_ _ _ _ _ _
            -----------------------------------

Where:

_ _ _ _ = outside of textview

| = the TextView edge

I tried android:gravity = "left|center" but it doesn't work, any idea ?

Thanks

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/image_button_wrapper"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_centerVertical="true">

    <ImageView
        android:id="@+id/left_button_image"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="@dimen/spacing_normal"/>

    <Textview
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/left_button_image"
        android:gravity = "center|left|left"
        android:clickable="false"
        android:focusable="false"
        app:font_name="Roboto-Light.ttf"/>

What i've (text align left but not centered)enter image description here

What i want (text align left and centered) (I add a padding manually to get the render but it's dirty and will not adapt to all text) : enter image description here

Ellinger answered 1/7, 2016 at 7:9 Comment(6)
Show the xml pleaseAfghanistan
what is the parent of the textView?? if you can post the code part which contain parent of textview and the textview, it will be easy to resolve your problem.Coyotillo
I add to my main post the xmlEllinger
if your textview width is wrap content then you must use layoutGravity. And if your textview width is matchParent then use gravity only. This will resolve your problem.Vanhomrigh
@Ellinger Did you give any width to your textview in dp?Inspan
Simply take a LinearLayout and move your TextView to that LinearLayout then set android:gravity="center" for LinearLayout and android:gravity="left" for TextViewYseult
S
11

Just give Textview parent as:

android:gravity = "center"

Just give Textview as:

android:gravity = "center_vertical|left|start"
android:layout_margin="20dp"
Sclerite answered 1/7, 2016 at 7:12 Comment(3)
Can you give me details exactly what you want. So i can give you exact code. Also give me full code for layout.Sclerite
Look at my main post I've updated it with more visual, hope it will help you to understand what i meanEllinger
Let us continue this discussion in chat.Sclerite
I
2

The problem is with your textview width, which just wrap its self around the text and setting its gravity to center just center it in its wrap area.

Change width to match-parent and gravity to center. This may help you

android:layout_width="match_parent"
android:gravity = "center"

Alternative method: Since the boundary lines are aligned center which are separate from Text-view. You just add the following line in your Text-view.

  android:layout_centerHorizontal="true"
Irritation answered 1/7, 2016 at 9:21 Comment(0)
I
2

You will get what you want if you change your textview width to match_parent or give some fixed width:

<TextView
    android:layout_width="100dp"
    //android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Hi! This is amazing!!"
    android:gravity="center_horizontal"/>
Inspan answered 1/7, 2016 at 11:26 Comment(0)
G
1

In your textview change this:

 android:gravity = "center|left|left"

to

 android:gravity = "center"
Glyceryl answered 1/7, 2016 at 9:16 Comment(1)
Thanks for your answer but unfortunately it's not what i'm looking for because the text will not be aligned at left : i.gyazo.com/01916284114cc21fa3f396e8c7509fd9.png what i want : i.sstatic.net/V0QYO.pngEllinger
C
1

For starting the text from left and gravity centered. Please use gravity property like this,

android:gravity="center|start"
Commander answered 2/4, 2018 at 6:25 Comment(0)
T
1

I'm using constraintLayout and this works for me.

Take note of layout_width="wrap_content" for the textView.

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent">


        <TextView
            android:id="@+id/headerTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal|center_vertical"
            android:text="Sign Up"
            android:textSize="25sp"
            app:layout_constraintBottom_toTopOf="@id/subHeaderTitle"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

        <TextView
            android:id="@+id/subHeaderTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1.Apple\n2.Orange\n3.Pear"
            android:textSize="16sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here

Tatar answered 2/2, 2021 at 6:52 Comment(0)
S
0

please change the RelativeLayout to LinearLayout make gravity and layout_gravity center

Seem answered 15/6, 2017 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.