17

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

Foushi
  • 365
  • 1
  • 3
  • 12

7 Answers7

11

Just give Textview parent as:

android:gravity = "center"

Just give Textview as:

android:gravity = "center_vertical|left|start"
android:layout_margin="20dp"
Ashish Vora
  • 571
  • 1
  • 8
  • 27
  • Can you give me details exactly what you want. So i can give you exact code. Also give me full code for layout. – Ashish Vora Jul 01 '16 at 08:53
  • Look at my main post I've updated it with more visual, hope it will help you to understand what i mean – Foushi Jul 01 '16 at 09:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/116170/discussion-between-ashish-vora-and-foushi). – Ashish Vora Jul 01 '16 at 09:33
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"
abdul khan
  • 843
  • 2
  • 7
  • 24
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"/>
Dhruvi
  • 1,971
  • 2
  • 10
  • 18
1

In your textview change this:

 android:gravity = "center|left|left"

to

 android:gravity = "center"
Parsania Hardik
  • 4,593
  • 1
  • 33
  • 33
  • Thanks for your answer but unfortunately it's not what i'm looking for because the text will not be aligned at left : https://i.gyazo.com/01916284114cc21fa3f396e8c7509fd9.png what i want : http://i.stack.imgur.com/V0QYO.png – Foushi Jul 01 '16 at 09:19
1

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

android:gravity="center|start"
Faris Muhammed
  • 970
  • 13
  • 17
0

please change the RelativeLayout to LinearLayout make gravity and layout_gravity center

jisa
  • 53
  • 5
0

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

chrizonline
  • 4,779
  • 17
  • 62
  • 102