How to automatically pop-up keyboard? - android

I have Edit Text field where I have to input a password, but I have to push this field. How to automatically pop-up keyboard without touching the Edit Text?
There is an Edit text xml field:
<EditText
android:id="#+id/editPasswd"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:password="true"/>

Use this code in the point where you want to display the keyboard (may be in oCreate?)
EditText myEditText = (EditText) findViewById(R.id.editPasswd);
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(myEditText, InputMethodManager.SHOW_FORCED);

I'm assuming you mean to automatically pop it up when the activity starts. If so, adding this to the activity-tag in the manifest solved it for me (unlike Erfan's answer):
android:windowSoftInputMode="stateVisible"

One line in the Java class:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

Related

Android - How to disable the paste suggestion on top of the soft keyboard

I want to disable the paste suggestion on the soft keyboard in the EditText XML or programmatically.
The thing I'm referring to is the button with "563737" below
To disable the paste suggestion feature on the on-screen keyboard for an EditText element, you can specify the "textNoSuggestions" value for the "android:inputType" attribute in the EditText's XML definition.
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textNoSuggestions" />
Or you can set the inputType property programmatically in code:
EditText editText = findViewById(R.id.edit_text);
editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);

How do I show the Phone Input softkeyboard when my activity starts?

Between the Activity tags in my AndroidManifest.xml I have :
android:windowSoftInputMode = "stateVisible"
So when my activity starts the soft keyboard comes up:
Is there not something I can add to make the phone input keyboard come up instead? Or how would I do it? Thanks.
EDIT
For the sake of clarity to this question I'm adding this 'edit'.
First of all, thanks for all the answers below.
However, my objective is to have no Edittext Visible on the screen, at first. The Phone Input soft keyboard comes up as soon as the activity starts and when the user starts
to type then the Edittext will become visible, with the text being typed.
I tried making the Edittext invisible, and then make it visible when user starts typing, but the problem is an invisible edittext can't have the focus,
So, android:inputType="number" was having no effect. And neither was: edittext.requestFocus(); Because the edittext was invisible.
What I ended up doing eventually was something very simple, setting width and height to 0dp in my xml like :
android:id="#+id/etPhoneNumber"
android:layout_width="0dp"
android:layout_height="0dp"
android:inputType="number" />
That way, the edittext can get focus with
edittext.requestFocus();
as soon as the activity is created, and inputType = "number" is recognised.
The next thing I'll do is increase the size of the edittext as soon as a key is hit on the soft keyboard, so users can see it.
I'll put all this in as an answer so it might be helpful to other users. +1 for the help!
Assuming that you have an EditText in the Activity which you show once your app is started, you can add below XML Attribute to your EditText.
android:inputType="phone"
Check developer docs for other input types in case if you need.
You can add
android:inputType="phone"
or
android:inputType="number"
to your View in your XML layout.
You should have some View in your code where you can give input like EditText or AutoCompleteTextView for changing the type of the keypad.
For example -
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="phone"
/>
In manifest file set the property in activity,
android:windowSoftInputMode="stateAlwaysVisible"
and then in your xml where your EditText set the property,
android:inputType="number"
first of all take an EditText in your xml
<EditText
android:id="#+id/etPhoneNumber"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="number" />
use this piece of code in the onCreate method of your activity !
EditText etPhoneNumber = (EditText) findViewById(R.id.etPhoneNumber);
etPhoneNumber.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
This works for me very well ! Hope it works for you too !
Let me know if this works ! :)
The objective is to have no Edittext Visible on the screen, at first.
The Phone Input soft keyboard comes up as soon as the activity starts and when the user starts
to type then the Edittext will become visible, with the text being typed.
I tried making the Edittext invisible, and then make it visible when user starts typing, but the problem is an invisible edittext can't have the focus,
So, android:inputType="number" was having no effect. And neither was: edittext.requestFocus(); Because the edittext was invisible.
What I ended up doing eventually was something very simple, setting width and height to 0dp in my xml like :
android:id="#+id/etPhoneNumber"
android:layout_width="0dp"
android:layout_height="0dp"
android:inputType="number" />
That way, the edittext can get focus with
edittext.requestFocus();
as soon as the activity is created, and inputType = "number" is recognised.
The next thing to do now is increase the size of the edittext as soon as a key is hit on the soft keyboard, so users can see it.

How to disable letters in keyboard (leave only digits)?

I'm developing Android application, and I need to disable letters for user's keyboard in order to allow user to input only digits in EditText. Please, tell me, is it possible?
add android:inputType="number" to the EditText in the layout xml.
More information on inputType's can be found at http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType
You can also go further and do something like:
<EditText
android:inputType="number"
android:digits="0123456789"
/>
EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
OR
Using XML
android:inputType="number"

How to open numeric keyboard when click on EditText?

I read several other posts and using input.setInputType(TYPE_NUMBER_FLAG_DECIMAL); does open the keyboard but its not the numeric keyboard
add android:inputType="number" to your edittext in your xml, it will automatically open numeric keyboard when you will click inside edittext.
Add android:inputType="number" in xml will automatically open numeric keyboard when click on EditText, but this will allow you to enter characters only numbers, and if you wanna enter other characters, you may try this:
android:inputType="number"
android:digits="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
You can add the following to your EditText block in XML file:
android:inputType="phone"
android:digits="1234567890"
OR
android:inputType="number"
Add this line in your edittext xml, clean build and run it. It should works.
android:inputType="number"

imeOptions "actionNext" programmatically - how to jump to next field?

In layout XML it is possible to specify android:imeOptions="actionNext" which adds Next button in virtual keyboard and by clicking on it - focus jumps to the next field.
How to do this programmatically - e.g. based on some event trigger focus to go to the next field?
You can use the constants from EditorInfo class for the IME options.
like,
editText.setImeOptions(EditorInfo.IME_ACTION_NEXT);
Search for the next focusable field and than invoke requestFocus().
TextView nextField = (TextView)currentField.focusSearch(View.FOCUS_RIGHT);
nextField.requestFocus();
Just suggestion, if you are using
EditTextSample.setImeOptions(EditorInfo.IME_ACTION_DONE);
it doesn't work, make sure that your EditText is using a single line.
Eg:
editTextSample.setSingleLine();
There is always necessity to add extra keys apart from default keys available in virtual QWERTY keyboard.
Using XML
<EditText android:text="#+id/EditText01"
android:id="#+id/EditText01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:imeOptions="actionDone"/>
By Programmatic Way
An EditorInfo is most useful class when you have to deal with any type of user input in your Android application.
IME_ACTION_DONE: This action performs a “done” operation for nothing to input and the IME will be closed.
EditTextSample.setImeOptions(EditorInfo.IME_ACTION_DONE);
For more information you may visit http://developer.android.com/reference/android/view/inputmethod/EditorInfo.html
The kotlin pendant
editText.imeOptions = EditorInfo.IME_ACTION_DONE
editText.setLines(1);
editText.setSingleLine(true);
editText.setImeOptions(EditorInfo.IME_ACTION_GO);
I solve the problem make sure with single line and go next editText when click enter
In my case, set an imeOptions fix the problem.
edtAnswer.maxLines = 1
edtAnswer.inputType = InputType.TYPE_CLASS_TEXT
edtAnswer.imeOptions = EditorInfo.IME_ACTION_NEXT
I tried all of the answers but EditorAction worked for me!
EditText.onEditorAction(EditorInfo.IME_ACTION_NEXT)
My XML layout:
<EditText
android:id="#+id/input1"
style="#style/edittext"
android:nextFocusForward="#id/input2"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/input2"
style="#style/edittext"
android:nextFocusLeft="#id/input1"
android:layout_weight="1"
android:nextFocusRight="#id/input3"
android:layout_width="0dp"
android:layout_height="wrap_content"/>
and Kotlin code:
input1.onEditorAction(EditorInfo.IME_ACTION_NEXT)
Now focus moves to the input2 Edittext.
You can do this by
edittext.imeOptions = EditorInfo.IME_ACTION_DONE //for done button
or
edittext.imeOptions = EditorInfo.IME_ACTION_NEXT //for next button
But...
you need to understand that if you are using filters for edittext then you need to set
edittext.setSingleLine()
You can jump to the next field by using the following code:
BaseInputConnection inputConnection = new BaseInputConnection(editText, true);
inputConnection.performEditorAction(EditorInfo.IME_ACTION_NEXT);
//Use EditorInfo.IME_ACTION_UNSPECIFIED if you set android:imeOptions on the EditText

Categories

Resources