How to set hint of TextInputEditText on top of view.
I have the following edit text that user will type description of the product, but hint is on center of view.
Here is the XML code.
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/interestDescriptionView"
style="#style/TextInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:maxLines="5"
app:layout_constraintBottom_toTopOf="#+id/tagsView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/titleView"
app:layout_constraintVertical_chainStyle="packed">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/interestDescriptionEditTextView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:textAlignment="viewStart"
android:hint="Descrição *"
android:layout_centerHorizontal="true"
android:gravity="start|top"
android:imeOptions="actionDone"
android:inputType="textMultiLine|textCapSentences"
android:importantForAutofill="no" />
</com.google.android.material.textfield.TextInputLayout>
You can set android:minLines to 2 or greater .. this will make it work
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/interestDescriptionView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Descrição *"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textview"
app:layout_constraintVertical_chainStyle="packed">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/interestDescriptionEditTextView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:gravity="top"
android:imeOptions="actionDone"
android:importantForAutofill="no"
android:inputType="textMultiLine|textCapSentences"
android:maxLines="5"
android:minLines="2"
android:textAlignment="viewStart" />
</com.google.android.material.textfield.TextInputLayout>
Also you can check other workarounds from here
Related
I want to align my second hint(email) to match the position of the first one(nickname), and I can't seem to do it.
Please check my layout from this link
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="347dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColorHint="#000000"
app:counterEnabled="true"
app:counterMaxLength="20"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.32999998">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/nickname"
android:maxLength="21"
/>
</com.google.android.material.textfield.TextInputLayout>
<EditText
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:id="#+id/editTextTextEmailAddress"
android:layout_width="342dp"
android:layout_height="70dp"
android:layout_marginTop="31dp"
android:ems="10"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.463"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textInputLayout"
app:layout_constraintVertical_bias="0.062"
android:hint="#string/prompt_email"
android:textColorHint="#000000"
/>
please note that I am a beginner in android, so take it easy on me.
I have an inputText field(TextInputLayout's EditText) of fixed height.
I want to align hint text to top left of the view.
Problem: hint text is always displayed at center_verticle but I want to display it at top left corner.
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/aboutWrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="About Sponsor">
<EditText
android:id="#+id/et_about_sponsor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="250dp"
android:textAlignment="viewStart"
android:layout_centerHorizontal="true"
android:gravity="start|top"
android:imeOptions="actionDone"
android:inputType="textMultiLine|textCapSentences"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="#dimen/font_medium" />
</com.google.android.material.textfield.TextInputLayout>
As per this github issue you need to set android:minLines to at least 2 along with android:gravity="top".
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/txtInput"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Hint">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/et_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="top"
**android:minLines="2"**
/>
</com.google.android.material.textfield.TextInputLayout>
You can use:
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="About Sponsor">
<com.google.android.material.textfield.TextInputEditText
android:minHeight="250dp"
android:textAlignment="viewStart"
android:layout_centerHorizontal="true"
android:gravity="start|top"
android:imeOptions="actionDone"
android:inputType="textMultiLine|textCapSentences"
.../>
</com.google.android.material.textfield.TextInputLayout>
Try to add style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" style to the TextInputLayout
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/aboutWrapper"
android:layout_width="match_parent"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_height="wrap_content"
android:hint="About Sponsor">
<EditText
android:id="#+id/et_about_sponsor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="250dp"
android:textAlignment="viewStart"
android:layout_centerHorizontal="true"
android:gravity="start|top"
android:imeOptions="actionDone"
android:inputType="textMultiLine|textCapSentences"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="#dimen/font_medium" />
</com.google.android.material.textfield.TextInputLayout>
You can use
<EditText
android:id="#+id/editTextTextPersonName5"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:ems="10"
android:hint="Message"
android:gravity="left|top"
android:inputType="textAutoComplete"
android:textAlignment="textStart" />
Here gravity attribute will help in aligning the text to whichever you want.
You can Refer Here
I'm using TextInputEditTexts as text entry, and when I select them, the hint moves up as it should, but is covered by the outline of the box. Does anyone know why that would be happening?
Photo:
Code:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/title_layout"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/grey"
android:theme="#style/OutlinedEditText"
app:boxStrokeColor="#color/blue"
app:hintTextColor="#color/blue"
app:layout_constraintTop_toBottomOf="#id/cover_art">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:hint="#string/title"
android:inputType="textCapWords" />
</com.google.android.material.textfield.TextInputLayout>
Remove in the TextInputEditText
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
In this way you are adding a margin between the main container (TextInputLayout) and the EditText
Use:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/title_layout"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:hint="...."
...
>
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="textCapWords" />
</com.google.android.material.textfield.TextInputLayout>
You are placing android:hint="#string/title" attribute in com.google.android.material.textfield.TextInputEditText.
The android:hint="#string/title" should be placed in com.google.android.material.textfield.TextInputLayout
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/title_layout"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/grey"
android:theme="#style/OutlinedEditText"
app:boxStrokeColor="#color/blue"
app:hintTextColor="#color/blue"
android:hint="#string/title"
app:layout_constraintTop_toBottomOf="#id/cover_art">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginStart="24dp"
android:layout_marginEnd="24dp"
android:inputType="textCapWords" />
</com.google.android.material.textfield.TextInputLayout>
I want to show the label "Km" with a white background with the same height as that of the edit text field.
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayoutInputDist"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="wrap_content"
android:layout_height="42dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/inputDist"
android:layout_width="80dp"
android:layout_height="match_parent"
android:background="#drawable/bg_left_corner_shape"
android:backgroundTint="#android:color/white"
android:fontFamily="#font/roboto_regular"
android:imeOptions="actionDone"
android:inputType="numberDecimal"
android:maxEms="4"
android:padding="4dp"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:id="#+id/label_km"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="4dp"
android:background="#drawable/bg_right_corner_shape"
android:backgroundTint="#android:color/white"
android:fontFamily="#font/roboto_regular"
android:gravity="center_vertical"
android:text="#string/km"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textInputLayoutInputDist"
app:layout_constraintTop_toTopOf="#+id/textInputLayoutInputDist" />
</androidx.constraintlayout.widget.ConstraintLayout>
But the TextInputLayout is taking some more height that the edit text field, I cant align the edit text and the "Km" label vertically the same levels.
As I understand your question check below answer
Give kmLable top and bottom constraint to edit text. Here I give fix width for a temporary purpose (change according to your requirement).
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayoutInputDist"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_margin="15dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/inputDist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/dividerColor"
android:fontFamily="#font/roboto_bold"
android:imeOptions="actionDone"
android:inputType="numberDecimal"
android:maxEms="4"
android:padding="4dp"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:id="#+id/label_km"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:background="#color/dividerColor"
android:fontFamily="#font/roboto_medium"
android:gravity="center_vertical"
android:text="km"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="#+id/textInputLayoutInputDist"
app:layout_constraintStart_toEndOf="#+id/textInputLayoutInputDist"
app:layout_constraintTop_toTopOf="#+id/textInputLayoutInputDist" />
</androidx.constraintlayout.widget.ConstraintLayout>
try
setHintEnabled(false)
in your xml like this:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayoutInputDist"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_margin="15dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:hintEnabled="false">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/inputDist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/dividerColor"
android:fontFamily="#font/roboto_bold"
android:imeOptions="actionDone"
android:inputType="numberDecimal"
android:maxEms="4"
android:padding="4dp"
android:textColor="#color/colorPrimaryDark"
android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout>
I am using Android TextInputLayout with password type. I see a mask/unmask icon. How can I set the text to unmasked by default?
<android.support.design.widget.TextInputLayout
android:id="#+id/textInputLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textInputLayout"
app:passwordToggleEnabled="true"
app:theme="#style/EditTextMaterialTheme">
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textVisiblePassword"
android:maxLines="1"
android:singleLine="true"
android:text=""
android:textColorLink="#android:color/darker_gray" />
</android.support.design.widget.TextInputLayout>
// When you define your edittext in oncreate method, that time put below line
youreditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
Your XML should be like this
<android.support.design.widget.TextInputLayout
android:id="#+id/textInputLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:passwordToggleEnabled="true"
app:theme="#style/EditTextMaterialTheme">
<EditText
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:text=""
android:textColorLink="#android:color/darker_gray" />
</android.support.design.widget.TextInputLayout>
And in your java class write these lines
TextInputLayout textInputLayout = findViewById(R.id.textInputLayout2);
textInputLayout.passwordVisibilityToggleRequested(true);