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

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.

Related

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"

Show Helper text below EditText along with the Hint

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"

Android Hide password

I want to hide my password as dot and it happens but problem is it hiding after a second i just want to make a dot as i type i should not show the word i typed there....
here is my code
<EditText
android:id="#+id/name2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/name1"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:background="#ff0000"
android:ems="10"
android:hint="Enter Password"
android:inputType="textPassword"
android:password="true" >
I found two different solutions, try both of one:
1) Remove android:inputType="textPassword" from your xml file and instead, in set it in java:
EditText password = (EditText) findViewById(R.id.password_text);
password.setTransformationMethod(new PasswordTransformationMethod());
As you're typing in that edit field, you don't see each character in plain text before it turns into a password dot. Also when making input in fullscreen, the dots will not appear, but the passoword in clear text.
2) Leave android:inputType="textPassword" in your xml. In Java, ALSO set the typeface and passwordMethod:
EditText password = (EditText) findViewById(R.id.register_password_text);
password.setTypeface(Typeface.DEFAULT);
password.setTransformationMethod(new PasswordTransformationMethod());
This approach gave me the hint font I wanted AND gives me the behavior I want with the password dots.
Hope it will help you!...

Warning on EditText

I'm trying to define an EditText but this warning is shown:
This text field does not specify an inputType or a hint.
The code in main.xml is:
<EditText android:id="#+id/input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/textTest" />
How can I fix it?
You could add a hint :
android:hint="This will appear to the user if he didn't enter something yet in the EditText"
and a inputType so you could present to the user a better suited soft keyboard for the text he is suposed to enter in the EditText:
android:inputType="number"
will present a soft keyboard with only numbers and various signs.
Those are just warnings, you could ignore them but is better for the user to implement them.
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
>
I think You should add input type.......
This is a known issue. Sometimes it doest work with changing to:
android:inputType="text"
I got it to work by setting:
android:inputType="textNoSuggestions"
Hope this helps
Above all are the solutions but before that you should understand what does that warnig mean.
Hint:- Hint is the text shown to user before writing anything over.
For eg. if you are putting a edittext where you want user to add his
name then you can write
<Edittext
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="Enter Name"/>
Input Type:- This is the xml tag that is to be used within edittext to let edittext to follow that particular restriction. if you are giving inputtype:number then user would be able to enter only numbers. There are several input type fields are there for that you can refer official developer site.

How to remove the underline from the EditText field in Android?

Whenever a word is typed in the EditText box, I always see an underline under the word being typed. But when I press a space after that word I no longer see the underline.
My reqirement is to remove that underline when the user is typing the message.
Added is the screenshot and we see that Smith is underlined. But I don't want this to happen.
Below is the xml that I use for the AlertDialog box.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/name_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="#string/alert_dialog_name"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/username_edit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:inputType="textPersonName"
android:capitalize="none"
android:gravity="fill_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
android:inputType="textNoSuggestions"
There is a function that removes any composing state. I think if you call it after every time a user types, you will not see the underline. I use it after the user finishes typing, to get the drawing cache of the entire textView (which I need without underline). It's
someTextView.clearComposingText();
You don't have to use any logic to remove the underlines -- just call getText().toString() when you want to use the value. It won't include special formatting or anything.
accepted answer is not given solution, and some other user given answer but no one given full anser, so that's why i write here working solution.
If you want to remove underline then just add below code.
XML
android:inputType="textNoSuggestions"
Java
EditText ed;
ed = findViewById(yourId);
ed.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
If you want to set more than one input type then,
ed.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
May be above information is helpful to others.
The best solution for this is to add on your EditText:
android:inputType="textMultiLine|textVisiblePassword"
Use this from your class, if EditText view is dynamic (created from class file):
EditText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
OR include android:inputType="textNoSuggestions" with your EditText in XML.
Read this if you want to keep emojis and textNoSuggestions doesn't work for you.
textNoSuggestions does not work in every keyboard.
inputType="textVisiblePassword" works but it removes emoji's from most keyboards.
I found that setting inputType="textUri" works and keeps the ability to add emojis.
try:
android:inputType= InputTypes.TextVariationVisiblePassword;
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:imeOptions="actionDone"
android:inputType="textNoSuggestions"
android:maxLines="1"
/>

Categories

Resources