Show Helper text below EditText along with the Hint - android

I am trying to make something on these lines:
I am able to show the hint using android:hint="Email Address" but unable to show the helper text - This will be your email username
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="15"
android:hint="Username"
app:et_helper="Username is preferably your registered email"/>
</android.support.design.widget.TextInputLayout>`
What I am getting is only and no Username is preferably your registered email below edittext:
Output:
Any pointers appreciated. Thank you.

The best way is to use TextInputLayout. Google introduced it in new design library. In order to use the TextInputLayout you have to add the following to your build.gradle dependencies:
compile 'com.android.support:design:22.2.0'
Then use it in your xml files:
<android.support.design.widget.TextInputLayout
android:id="#+id/textInputLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter your name"/>
</android.support.design.widget.TextInputLayout>
You can set your text by setting error true. Altough it is for showing errors but it is suitable for your use. You can change color and typeface if you want.
TextInputLayout til = (TextInputLayout) findViewById(R.id.textInputLayout);
til.setErrorEnabled(true);
til.setError("You need to enter a name");

With Design Support Library 28 , an inbuilt helper Text feature is added in TextInputLayout.
implementation 'com.android.support:design:28.0.0'
Now enable error using xml or programmatically
textInputLayout.isHelperTextEnabled=true
textInputLayout.error="Email can not be Empty!!!"
Also , hint and error can together be used now!!!
Example
et.setOnFocusChangeListener { v, b ->
if (b) {
textInputLayout.helperText = "yourhelperText"
} else {
textInputLayout.helperText = null
if(et.text.toString()==""){ // or any other validation
textInputLayout.error="Email can not be Empty!!!"
}
}
TextInputLayout | Android Developers
EDIT Don't forget to enable error and helperText via xml or programatically.

Full XML with TextInputLayout OutlinedBox style and TextInputEditText
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/til_customer_no"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:padding="3dp"
app:helperTextEnabled="true"
app:helperText="* Enter customer number that will be used">
<!--android:maxLength="13"-->
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/et_customer_no"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Customer No"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>

The Helper Text is not provided by TextInputLayout. However, the article below has a working example of it. It uses a class that extends from TextInputLayout by adding HelperText as another indicator to the class, working along side with the ErrorText.
https://medium.com/#elye.project/material-login-with-helper-text-232472400c15#.vm28p662v

So what you want is this:
Helper Text
You can achieve this by using this library:
https://github.com/rengwuxian/MaterialEditText
You can set the "helper text" attribute to show the text below the line and the "hint" to show the text above the line. Below is the sample layout for the attached picture
<com.rengwuxian.materialedittext.MaterialEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
app:met_floatingLabel="normal"
app:met_helperText="Must contain at least 8 characters"
app:met_helperTextAlwaysShown="true"/>

I had similar question asked here.
If you want to keep two EditText views together closed vertically in LinearLayout, I think there is no way. One way, I think, is you can set the android:gravity of top EditText to 'bottom' and 'top' to the lower EditText. But in RelativeLayout, This should be easy.
TextInputLayout can be used if you want to show the hint text (on top of EdiText) even while user typing it. Usually with setting hint to EditText would not work this way. Hint text would disappear after the view is focussed by touching it.

You can simply use app:helperText attribute in TextInputLayout (with library com.google.android.material:material:1.2.1) in this way:
<com.google.android.material.textfield.TextInputLayout
...
android:hint="Email Address"
app:helperText="This will be your email username">
<com.google.android.material.textfield.TextInputEditText
... />
</com.google.android.material.textfield.TextInputLayout>
or in programmatically (kotlin) way:
textInputLayout.helperText = "This will be your email username"

Related

How to set programmatically password toggle state for TextInputLayout?

I am using TextInputLayout from Google material design library (version 1.4.0-alpha02).
If I set this property in the XML:
app:endIconMode="password_toggle"
A button is displayed to toggle between the password being displayed as plain-text or disguised.
Now if I try to do the same programmatically, as reported in the documentation I call
myTextInputLayout.endIconMode = END_ICON_PASSWORD_TOGGLE
The button appears correctly, but tapping it has no effect.
I tried to play a bit with all the methods and I found out that doing this:
myTextInputLayout.clearOnEndIconChangedListener()
myTextInputLayout.endIconMode = END_ICON_PASSWORD_TOGGLE
makes the button work: the content of the editText actually changes from plain-text to disguised, but the button stay the same. The icon is supposed to change depending on the state.
This is the layout I am using:
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/myTextInputLayout"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:textColorHint="?android:textColorSecondary"
app:boxStrokeColor="#drawable/text_input_stroke_selector"
app:boxStrokeWidth="1dp"
app:endIconTint="?android:textColorSecondary"
app:errorIconDrawable="#null"
app:hintTextColor="?android:textColorSecondary">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:textCursorDrawable="#null" />
</com.google.android.material.textfield.TextInputLayout>
Any idea?

TextInputEditText - Make clickable but not editable

I have a layout where I want to show a bunvh of edit texts and a date picker. Since I want to have the same design for all fields, I figured out that I need to user an edit text for date picker too but make it non editable but clickable. Below is the xml code of my edit text that will be used to show the date picker :
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/notification_layout"
style="#style/Theme.Connect.InputFieldLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:startIconDrawable="#drawable/ic_stk_notification_24dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/notification_edit_text"
style="#style/Theme.Connect.InputField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Notification"
android:maxLines="1"
android:lines="1"
android:cursorVisible="false"/>
</com.google.android.material.textfield.TextInputLayout>
I tried many solutions I found on the internet, I did managed to make edit text non editable but without the ripple click effect. Is there a way to make the edit text non edditable but still keep the ripple click effect? Is this the best practice to show a date picker consisting the design of the form layout?
Try this and change the `android:focusable` attribute to `false`
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/notification_layout"
style="#style/Theme.Connect.InputFieldLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:startIconDrawable="#drawable/ic_stk_notification_24dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/notification_edit_text"
style="#style/Theme.Connect.InputField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Notification"
android:maxLines="1"
android:focusable="false"
android:lines="1"
android:cursorVisible="false"/>
</com.google.android.material.textfield.TextInputLayout>
You can simply use a TextView instead of an EditText and you can set an onClickListener() to it. You can also style it the way you want.
notification_textview.setOnClickListener {
// Whatever you want to do after a click
}
You can get a ripple effect on touch by adding these 2 attributes to your textview
<......Textview
android:background=?android:attr/selectableItemBackground
android:clickable="true" />
You can also add styles to a textview that you wanted to use for the editText.

Error drawable and color don't change in textInputLayout

So I'm using a textInputLayout and all I need to do is set a custom drawable for the error icon. Here's my layout and dependency
implementation 'com.google.android.material:material:1.2.0-beta01'
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="#dimen/text_field_height"
android:layout_margin="#dimen/activity_margin"
android:hint="#string/insert_name_hint"
app:boxBackgroundColor="#color/transparent"
app:boxCornerRadiusBottomEnd="#dimen/text_field_corner"
app:boxCornerRadiusBottomStart="#dimen/text_field_corner"
app:boxCornerRadiusTopEnd="#dimen/text_field_corner"
app:boxCornerRadiusTopStart="#dimen/text_field_corner"
app:errorEnabled="true"
app:errorTextColor="?colorAccent"
app:errorIconDrawable="#drawable/ic_alert_black_24dp"
app:errorIconTint="?colorAccent"
app:errorContentDescription="#string/insert_name_hint"
app:boxStrokeErrorColor="#color/gray"
app:startIconDrawable="#drawable/ic_cool_black_24dp"
app:startIconTint="#color/gray"
app:hintAnimationEnabled="true"
app:hintTextColor="?colorAccent">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/nameEvent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:imeOptions="actionDone"
android:maxLength="30"
android:padding="0dp"
android:textAlignment="center"
android:textCursorDrawable="#null"
android:inputType="textCapSentences"/>
</com.google.android.material.textfield.TextInputLayout>
Basically, the drawable and the tints don't work, the default icon and color are used. The rest works as it should. I'm not setting anything programmatically.
Well, the problem was kinda basic, but i found out after a while.
It's not a layout-level problem, but an error management problem. The error must be set on the Input Layout and not on the Input EditText. So here's the difference (i also had to add an id to the Input Layout of course):
Before
customView.nameEvent.error = getString(R.string.invalid_value_name)
After
customView.nameEventLayout.error = getString(R.string.invalid_value_name)
Yep, is that simple. But i think this could be a common error, so i hope this answer can help somebody out there.

inputType = "number|text" not working - Android

I am using inputType = "number|text" inside TextInputEditText when I focus on this, It lets me type number only, It allows to navigate to text keyboard but not letting me type text.
Here is the code snippet:
<android.support.design.widget.TextInputLayout
android:id="#+id/availableRoof"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/radio_groupPcOfInstltion"
android:visibility="visible">
<android.support.design.widget.TextInputEditText
android:id="#+id/avlbleRoofTextField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Available Roof/Ground Area(sq.ft.)"
android:inputType="number|text"
android:text=""
android:textSize="13sp" />
</android.support.design.widget.TextInputLayout>
Please suggest a solution, so that I can type text and number both.
Just use input type text for getting the all possible options and textVisiblePassword for removing the suggestion and show number in one keyboard.
android:inputType="text"
OR
android:inputType="textVisiblePassword"
You need to set the inputType as text, so that you can easily enter text as well as numbers in the TextInputEditText.
Use: android:inputType="text"
Try adding these properties:
android:digits="abcdefghijklmnopqrstuvwxyz1234567890"
android:inputType="textVisiblePassword"

Android: How to set password property in an edit text?

I need to create a login form with 'username' 'password' fields and two buttons 'login' and 'cancel' in my android application.
I am using an alert dialog with edittext inside that.
This is the code I used to create password edittext..
final EditText Password = new EditText(this);
Password.setGravity(Gravity.CENTER);
Password.setHint("Password");
Password.setWidth(200);
Password.setTransformationMethod(new PasswordTransformationMethod());
login_alert.addView(Password);
My issue is that, plain text is shown instead of 'dots' when i open a softkeypad to edit the password. (It is shown as dots when not in softkeypad mode)
Can anyone suggest a solution?
Password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
This one works for me.
But you have to look at Octavian Damiean's comment, he's right.
This is deprecated
In xml of EditText iclude this attribute: android:password="true"
Edit
android:inputType="textPassword"
Here's a new way of putting dots in password
<EditText
android:id="#+id/loginPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="#string/pwprompt" /
add android:inputType = "textPassword"
You need to use PasswordTransformationMethod.getInstance() instead of new PasswordTransformationMethod().
The only way that worked for me using code (not XML) is this one:
etPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);
etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
See this link
text view android:password
This applies for EditText as well, as it is a known direct subclass of TextView.
I found when doing this that in order to set the gravity to center, and still have your password hint show when using inputType, the android:gravity="Center" must be at the end of your XML line.
<EditText android:textColor="#000000" android:id="#+id/editText2"
android:layout_width="fill_parent" android:hint="Password"
android:background="#drawable/rounded_corner"
android:layout_height="fill_parent"
android:nextFocusDown="#+id/imageButton1"
android:nextFocusRight="#+id/imageButton1"
android:nextFocusLeft="#+id/editText1"
android:nextFocusUp="#+id/editText1"
android:inputType="textVisiblePassword"
android:textColorHint="#999999"
android:textSize="16dp"
android:gravity="center">
</EditText>
To set password enabled in EditText, We will have to set an "inputType" attribute in xml file.If we are using only EditText then we will have set input type in EditText as given in below code.
<EditText
android:id="#+id/password_Edit"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="password"
android:imeOptions="actionNext"
android:inputType="textPassword"
android:maxLength="100"
android:nextFocusDown="#+id/next"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Password enable attribute is
android:inputType="textPassword"
But if we are implementing Password EditText with Material Design (With Design support library) then we will have write code as given bellow.
<android.support.design.widget.TextInputLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/txtInput_currentPassword"
android:layout_width="match_parent"
app:passwordToggleEnabled="false"
android:layout_height="wrap_content">
<EditText
android:id="#+id/password_Edit"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="#string/hint_currentpassword"
android:imeOptions="actionNext"
android:inputType="textPassword"
android:maxLength="100"
android:nextFocusDown="#+id/next"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.TextInputLayout>
#Note: - In Android SDK 24 and above, "passwordToggleEnabled" is by default true. So if we have the customs handling of show/hide feature in the password EditText then we will have to set it false in code as given above in .
app:passwordToggleEnabled="true"
To add above line, we will have to add below line in root layout.
xmlns:app="http://schemas.android.com/apk/res-auto"
My search for a similar solution for Visual Studio 2015/Xamarin lead me to this thread. While setting the EditText.InputType to Android.Text.InputTypes.TextVariationVisiblePassword properly hid the password during user entry, it did not 'hide' the password if it was visible before the EditText Layout was rendered (before the user submitted their password entry). In order to hide a visible password after the user submits their password and the EditText Layout is rendered, I used EditText.TransformationMethod = PasswordTransformationMethod.Instance as suggested by LuxuryMode.

Categories

Resources