How do I show the number keyboard on an EditText in android?
You can configure an inputType
for your EditText
:
<EditText android:inputType="number" ... />
How to open numeric keyboard when click on EditText?
add android:inputType="number" to your edittext in your xml, it will automatically open numeric keyboard when you will click inside edittext.
How do you set the EditText keyboard to only consist of numbers on Android?
After several tries, I got it!
I'm setting the keyboard values programmatically like this:
myEditText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
Or if you want you can edit the XML like so:
android: inputType = "numberPassword"
Both configs will display password bullets, so we need to create a custom ClickableSpan
class:
private class NumericKeyBoardTransformationMethod extends PasswordTransformationMethod {
@Override
public CharSequence getTransformation(CharSequence source, View view) {
return source;
}
}
Finally we need to implement it on the EditText
in order to display the characters typed.
myEditText.setTransformationMethod(new NumericKeyBoardTransformationMethod());
This is how my keyboard looks like now:
TextInputEditText to show numeric keyboard but also allow /
Removing android:inputType
and android:digits
from the xml and also adding the below to the activity's onCreate()
seems to do the trick.
input.setKeyListener(DigitsKeyListener.getInstance("0123456789./"));
However it's not completely clear to me that this should or will always work - see note below from the docs on DigitsKeyListener
...
As for all implementations of KeyListener, this class is only concerned with hardware keyboards. Software input methods have no obligation to trigger the methods in this class.
Source: https://developer.android.com/reference/android/text/method/DigitsKeyListener
How to show soft-keyboard when edittext is focused
To force the soft keyboard to appear, you can use
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
yourEditText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
And for removing the focus on EditText
, sadly you need to have a dummy View
to grab focus.
To close it you can use
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);
This works for using it in a dialog
public void showKeyboard(){
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
public void closeKeyboard(){
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}
Number keyboard not showing on Android app
Problem is I had the EditText inside a ListView. Which is problematic for some reason.
I fixed it by setting the ListView like this
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:descendantFocusability="afterDescendants"
android:focusable="false" />
This helped me lead to the answer
EditText inside ListView will not stay focused
Is there a way to show the numbers first on the soft keyboard for Android?
Per my research and Bert B.'s comment, there is no way to do this cross-device compatible.
Related Topics
Converting Edittext to Int - Android
How to Clear Livedata Stored Value
How to Use Threetenabp in Android Project
Onactivityresult Is Not Being Called in Fragment
Http Failure Response for (Unknown Url): 0 Unknown Error on Android
Aligning Textviews on the Left and Right Edges in Android Layout
How to Get Last Record from Sqlite
How to Fix Mainactivity Does Not Have a Navcontroller
Android Volley - Basicnetwork.Performrequest: Unexpected Response Code 400
React-Native Run-Android Has Error, How to Fix It
String Resource New Line /N Not Possible
How to Emulate Gps Location in the Android Emulator
Move to Another Edittext When Soft Keyboard Next Is Clicked on Android
How to Retrieve Data from Sqlite Database in Android and Display It in Textview
How to Play the Audio Files Directly from Res/Raw Folder
Jsonexception: Value of Type Java.Lang.String Cannot Be Converted to Jsonobject
How to Set the Image from Drawable Dynamically in Android
How to Convert Utc Timestamp to Device Local Time in Android