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>
sharath
  • 131
  • 1
  • 6
  • why you are giving hint in both textInputLayout and Edittext? I think, you should give in edittext only.It work also same. – Janvi Vyas Apr 26 '18 at 06:21
  • ya i have removed – sharath Apr 26 '18 at 06:21
  • hint is only for edit text. – sharath Apr 26 '18 at 06:23
  • To give, edittext gravity in center please show my below code. – Janvi Vyas Apr 26 '18 at 06:24
  • it is meaning less to show hint on both, but still you want to show than in edit text give android:textAlignment="center" – mitesh viradiya Apr 26 '18 at 06:24
  • i want to show the hint at top|left and the text typed in edit text to be at center. – sharath Apr 26 '18 at 06:29
  • 1
    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. – Mike M. Apr 26 '18 at 06:59

2 Answers2

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

rekire
  • 47,260
  • 30
  • 167
  • 264
Janvi Vyas
  • 732
  • 5
  • 16
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

Rasel
  • 5,488
  • 3
  • 30
  • 39