Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 883
Foushi Avatar asked Jul 01 '16 07:07

Foushi


1 Answers

Just give Textview parent as:

android:gravity = "center"

Just give Textview as:

android:gravity = "center_vertical|left|start"
android:layout_margin="20dp"
like image 95
Ashish Vora Avatar answered Oct 04 '22 13:10

Ashish Vora