Android Display Numeric Keypad on Button Click - android

In my application, I am trying to display the numeric keypad when the user clicks on a button.
When the button is clicked, I shift the focus to the EditText in my layout using requestFocus() and next I need to display the numeric keypad so that the user can type in the values..
The values will always be numeric and hence I need to show only the numeric keypad.
I tired using this inside my button's onClick() method but it does not work.
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
Please provide me with any solution to this.
Also, my application is for an Android tablet supporting 4.0.3.

This one in your EditText property
android:inputType="phone" (This will displayed phone numeric keypad)
or
android:inputType="number" (This will displayed numeric keypad)
Now, you have to just set Focus on your EditText on Button's click..
something like,
edtNumber = (EditText) findViewById(R.id.number);
// Button's onClick....
#Override
public void onClick(DialogInterface dialog, int which)
{
edtNumber.requestFocus();
}

in EditText put below line.
android:inputType="number"

Related

How to show numeric keybord when EditText is set to floating numbers?

I want to my phone to show numeric keyboard when I focus on an EditText which is set to InputType.TYPE_NUMBER_FLAG_DECIMAL. But it shows alphanumeric (normal keyboard like whatsapp's) keyboard, How can I achieve this?
I use this to open keyboard:
InputMethodManager inputMethodManager = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
I use this to set input type of EditText:
mEditText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
It's Dynamic. So I can't write it as XML attribute. User can switch input type with these two lines:
mEditText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL); to get a floating number between 0 and 2.
mEditText.setInputType(InputType.TYPE_CLASS_NUMBER); to get any integer bigger or equal than 0.
Thanks in advance..
It is better to set inputType in XML code

Android show input keyboard only when user press a button

I have a PopupWindow in one Activity.
When user press on the list item in the activity, the window will popup for getting input from users.
There are some EditText in the window. And also I provided some buttons that preset some text on it, so when user press on it then it will enter to the edittext.
I can disable the softkeyboard when the window first popup. But when I change the focus on edittext (Move from one edittext to another edittext), the keyboard shown up.
I want the softkeyboard show only when user press on the "show keyboard" button in the popup window
How can I do it?
Updated:
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
selectedEditText = (EditText)view;
String text = selectedEditText.getText().toString();
selectedEditText.setSelection(text.length());
InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(selectedEditText.getWindowToken(), 0);
}
}
I tried the code above but it still showing.
set onFocusChangedListener() on every editText you have and disable Keyboard Pop inside it if it has focus..
Show it only when the user Presses the button..
There is very simple solution too; create fake focus above your EditText and you good to go.
Like this:
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true">
</LinearLayout>
<!--Your EditText-->
<EditText
...
</EditText>
also you can add these two line above your mentioned EditText in any VIEW, like TextView and so on.
android:focusable="true"
android:focusableInTouchMode="true"
Go to the xml and find the activity in which you want to hide keyboard
add given attribute to the Activity..
android:windowSoftInputMode="stateAlwaysHidden"

Android How to show numeric softkeyboard with EditText of InputType="text"

I need allow to user input numeric information in a EditText, but the output needs to be formatted like "##.###,##" (# in [0..9]). The formatting I was made in a TextWatcher... this is good, the TextWatcher does the job... bute, when user selects the EditText, as it marked as text, the AlphaKeyboard is shown, if I select the EditText as numeric the keyboard I need is shown and the TextWatcher stop working.
put the following in your xml at the required edittext
<EditText android:inputType="number" ... />
refer to
How do I show the number keyboard on an EditText in android?
You need to change edittext entry like this.
<EditText android:inputType="phone" android:numeric="decimal"></EditText>
Use this code to show soft keypad.
InputMethodManager imm = (InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.showSoftInputFromInputMethod(v.getApplicationWindowToken(), 1);
As you are doing formatting in TextWatcher (##,####....) i.e. you are adding "," in EditText and you have made the EditText as numeric. If its numeric then "," char is invalid. Thus its not allowing TextWatcher to update the formatted text that contains ',".
You have 3 options to deal with this problem :
Make the EditText as normal instead of numeric. In TextWatcher look out for any other invalid chars other than 0-9. You can show IME for Mumber keypad when the focus is on the EditText.
Use Mask for EditText.
Let it be numeric. Trap FocusGain & FocusLost for the EditText. In focusGain, remove the formatting & set the inputType to Numeric. & In focusLost, first make the input type to Normal and then update the entered value to formmated value. You can set the input type to normal at runtime using setInputType(InputType.TYPE_TEXT_VARIATION_NORMAL); & for numeric use : setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
I believe 1st option will be best to handle and manage out.

Android software keyboard switch between numeric and alphabetic programmatically

I have an EditText with inputMode = text. By default software keyboard is shown as alphabetical and user have to switch it to numeric by pressing specific key (like "123").
Having text inputMode is it possible to show numeric keyboard by default instead of alphabetic?
I need both alphabetic and numeric. But numeric is used more often then alphabetic so i search for way to switch mode programmatically.
I find the answer a day, finally I found this and its work.
android:inputType="textVisiblePassword"
source
Just set it with the normal setter:
EditText editText = (EditText) findViewById(R.id.edittext);
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
// or
editText.setInputType(InputType.TYPE_CLASS_TEXT);
you can achieve it by setting the below attribute to EditText on xml has follows android:inputType="number"

How to hide soft keyboard for one of the EditText when soft keyboard is already shown?

Here the situation: I am showing a screen with 4 input fields, 2 of them need to be edit by user (numeric field), other (text field)- shows some text info which can be changed by user via ListView, but it closed for manual edit.
I use in my XML file android:editable="false" for non-numeric EditText fields and it helps to disable popup of soft keyboard when the user click on field. But the problem is in when the user edit a numeric field the soft keyboard not hidding after user focused on text field.
So how can I hide the already shown soft keyboard when user focused on my text field?
The methods like:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(MEdit1.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
or something like this
MetricEdit = (EditText) findViewById(R.id.MetricEdit);
MetricEdit.setInputType(0);
take no effect.
Thanks in advance for your time.
try putting
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(MEdit1.getWindowToken(), 0);
in the onclicklistener for the edittext that is editable
you can also change the numeric edittext's keyboard to have a "done" at the bottom with the following:
android:imeOptions="actionDone"
in the xml for the edittext
This will help..
edittext.setInputType(InputType.TYPE_NULL);
if (android.os.Build.VERSION.SDK_INT >= 11)
{
edittext.setRawInputType(InputType.TYPE_CLASS_TEXT);
edittext.setTextIsSelectable(true);
}

Categories

Resources