Elad Benda
Elad Benda

Reputation: 36656

why is my text in editText not centered horizontally?

i have this simple xml block:

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" 
                    android:gravity="center">

                    <EditText
                        android:id="@+id/verificationCodeEditText"
                        style="@style/textOnBg"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@android:color/transparent"
                        android:inputType="phone"
                        android:textColor="#516063"
                        android:textColorHint="#b4c8cf"
                        android:imeOptions="actionSend"
                        android:layout_gravity="center"
                        android:textSize="25dp" >
                    </EditText>
                </LinearLayout>

however, when i put text in this editText the cursor is a bit to the left.

only after filling 80% of the editText the text seems centered.

<style name="textOnBg" parent="android:Widget.TextView">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">#32717f</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">18sp</item>
</style>

update

tried this: but it didn't help

                    <EditText
                        android:id="@+id/verificationCodeEditText"
                        style="@style/textOnBg"
                        android:layout_width="match_parent"

Upvotes: 2

Views: 182

Answers (7)

jakjakjakjakjak
jakjakjakjakjak

Reputation: 114

Use this:

android:layout_centerHorizontal="true"

Upvotes: 0

Vettiyanakan
Vettiyanakan

Reputation: 8476

Try this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <EditText
            android:id="@+id/verificationCodeEditText"
            style="@style/textOnBg"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="@android:color/transparent"
            android:gravity="center"
            android:imeOptions="actionSend"
            android:inputType="phone"
            android:paddingLeft="5dip"
            android:paddingRight="5dip"
            android:textColor="#516063"
            android:textColorHint="#b4c8cf"
            android:textSize="25dp" >
        </EditText>
    </LinearLayout>

</RelativeLayout>

Hope this will help :).

Upvotes: 0

Pihu
Pihu

Reputation: 1039

You can set the gravity like this:

android:gravity="center_horizontal"

Upvotes: 0

Waseem Arain
Waseem Arain

Reputation: 1173

Try that

 <EditText
        android:id="@+id/verificationCodeEditText"
        style="@style/textOnBg"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        android:gravity="center_horizontal"
        android:imeOptions="actionSend"
        android:inputType="phone"
        android:textColor="#516063"
        android:textColorHint="#b4c8cf"
        android:textSize="25dp" >
    </EditText>

Upvotes: 0

Hariharan
Hariharan

Reputation: 24853

Try this..

Give android:layout_width="match_parent" and android:gravity="center" to your EditText

<EditText
    android:id="@+id/verificationCodeEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:background="@android:color/transparent"
    android:imeOptions="actionSend"
    android:inputType="phone"
    android:text="sdfsdfsdf"
    android:textColor="#516063"
    android:gravity="center"
    android:textColorHint="#b4c8cf"
    android:textSize="25dp" >
</EditText>

Upvotes: 2

diordna
diordna

Reputation: 550

Please try

    android:gravity="center_horizontal"

Upvotes: 1

Juan Cort&#233;s
Juan Cort&#233;s

Reputation: 21062

You may need to specify a set width for the EditText before the gravity can have an effect. Set it to fill_parent and see if it has any effect.

Upvotes: 1

Related Questions