How to set different gravity for TextInputLayout's hint and text in Android
Asked Answered
F

2

13

How to set different gravity for TextInputLayout's hint and text entered in edit text. I want to set the hint gravity to start and edit text's text gravity to center. So how can i achieve this. This is my xml code,

              <android.support.design.widget.TextInputLayout
                        android:id="@+id/email"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:padding="16dp"
                        android:gravity="start">

                        <EditText
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:backgroundTint="@color/grey"
                            android:hint="Email"
                            />
                    </android.support.design.widget.TextInputLayout>
Fissi answered 26/4, 2018 at 6:10 Comment(7)
why you are giving hint in both textInputLayout and Edittext? I think, you should give in edittext only.It work also same.Brainsick
ya i have removedFissi
hint is only for edit text.Fissi
To give, edittext gravity in center please show my below code.Brainsick
it is meaning less to show hint on both, but still you want to show than in edit text give android:textAlignment="center"Duluth
i want to show the hint at top|left and the text typed in edit text to be at center.Fissi
The floating hint's alignment is determined when the EditText is added to the TextInputLayout, which will happen during inflation, in this case. To set a different gravity for the EditText, don't set any gravity attributes in the layout, then set the EditText's gravity programmatically, in your code. That is, after setContentView(), use findViewById() to get the EditText, then call setGravity(Gravity.CENTER_HORIZONTAL) on it.Harrold
B
11

In Android, we can set gravity both programmatically and in XML.
To set edit text hint in the center see the code below in which I use android:gravity="center" in XML.

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:backgroundTint="@color/colorAccent"
    android:hint="Email" />

For showing the hint at top|left and the text typed in edit text to be at center or to set different gravity for TextInputLayout's hint and text in Android, you need to try the code below.

In .xml you need to set EditText's hint gravity top|left.

<android.support.design.widget.TextInputLayout
    android:id="@+id/email"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">

    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="top|left"
        android:hint="Email"
        android:backgroundTint="@color/colorAccent" />
</android.support.design.widget.TextInputLayout>

And in your java (activity) file, set EditText's text gravity center programmatically:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText etName = (EditText)findViewById(R.id.etName);
    etName.setGravity(Gravity.CENTER);
}

See the screenshot with the result:

enter image description here

Brainsick answered 26/4, 2018 at 6:24 Comment(5)
please read the question in want to set different gravity for hint of the edit text and the text which is being typedFissi
hello @Fissi please show my edited answer. It will help you.Brainsick
@Fissi can you try this??Brainsick
Hello @Fissi my code is useful to you?? If yes then accept my answer and if you have any query feel free to ask meBrainsick
Work for Material's TextInputLayout and TextInputEditText in July 2019 as wellPyretic
N
1

Different gravity works implementation 'com.google.android.material:material:1.1.0' and below version but does not work in 1.2.0 or above. To make it work in 1.1.0 or below follow these steps.

  1. set TextInputEditText gravity to start in xml

    <com.google.android.material.textfield.TextInputEditText id="@+id/text" gravity="start"/>

  2. then programmatically set its gravity to center

    text.gravity=Gravity.CENTER

Natashianatassia answered 28/8, 2020 at 3:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.