Set inputType for an EditText Programmatically?
According to the TextView docs, the programmatic version of android:password is setTransformationMethod(), not setInputType(). So something like:
mEdit.setTransformationMethod(PasswordTransformationMethod.getInstance());
should do the trick.
Programmatically change input type of the EditText from PASSWORD to NORMAL & vice versa
Add an extra attribute to that EditText
programmatically and you are done:
password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
For numeric password (pin):
password.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
Also, make sure that the cursor is at the end of the text in the EditText
because when you change the input type the cursor will be automatically set to the starting point. So I suggest using the following code:
et_password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
et_password.setSelection(et_password.getText().length());
When using Data Binding, you can make use of the following code:
<data>
<import type="android.text.InputType"/>
.
.
.
<EditText
android:inputType='@{someViewModel.isMasked ?
(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD) :
InputType.TYPE_CLASS_TEXT }'
If using Kotlin:
password.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
How to programmatically set EditText's InputType to integer or decimal?
You can use this code:
EditText edt = new EditText(context);
edt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); //for decimal numbers
edt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED); //for positive or negative values
If together:
edt.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
How to set InputType 'time' programmatically?
For time
field:
setInputType(InputType.TYPE_CLASS_DATETIME |InputType.TYPE_DATETIME_VARIATION_TIME);
datetime has dot(.), slash(/) which I do not want to show in keyboard
You should try with
setKeyListener(DigitsKeyListener.getInstance("0123456789:"));
Programmatically created EditText's Input Type Password not working
Just replace edittext.setSingleLine(true);
with edittext.setMaxLines(1);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
final EditText edittext = new EditText(ChildMainScreen.this);
edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
edittext.setMaxLines(1);
alert.setMessage("Please enter password to continue");
alert.setView(edittext);
alert.show();
How to properly set InputType for EditText?
use just input type instead
input.setInputType(InputType.TYPE_CLASS_NUMBER);
Set EditText Digits Programmatically
Try this:
<EditText
android:inputType="number"
android:digits="0123456789."
/>
From Code:
weightInput.setKeyListener(DigitsKeyListener.getInstance("0123456789."));
But, it allows the user to include several "."
See JoeyRA's answer for real numbers.
Related Topics
How to Set Up Android Sdk for Command Line Development on Linux
Customize Android Facebook Login Button
Android How to Create My Own Activity and Extend It
Recognizing Multiple Keywords Using Pocketsphinx
Windowsoftinputmode="Adjustresize" Not Working with Translucent Action/Navbar
How to Save Custom Arraylist on Android Screen Rotate
Getview Returning Null When Fragment Has Been Created from an Activity
Android Custom Dropdown/Popup Menu
Reading an Image File into Bitmap from Sdcard, Why am I Getting a Nullpointerexception
Help with Passing Arraylist and Parcelable Activity
Android Ndk: Getting the Backtrace
Detect a New Android Notification
Android: Speech Recognition Without Using Google Server
Error: Fix the Version Conflict (Google-Services Plugin)
Android SQLite Db When to Close
How to Handle Cookies in Httpurlconnection Using Cookiemanager