Is it possible to open keyboard on Android without click or touch event? For example just after appending textarea to some element? element.focus() works for me on iOS but not on Android.
-
2@Vixed you should edit the question to make it fit better your own issue. As-is it is unclear whether it is for an Android native app in Java (as the current answer assumes), or for an hybrid app in JavaScript, e.g. using Cordova.ghybs– ghybs2017-10-27 08:04:53 +00:00Commented Oct 27, 2017 at 8:04
-
Please clarify does your app is in java or an hybrid app.Jaiprakash Soni– Jaiprakash Soni2017-10-27 11:09:04 +00:00Commented Oct 27, 2017 at 11:09
-
Webview javascript (jQuery) @ghybsVixed– Vixed2017-10-27 14:57:21 +00:00Commented Oct 27, 2017 at 14:57
-
check this stackoverflow.com/questions/9703271/…Jaiprakash Soni– Jaiprakash Soni2017-10-30 04:54:21 +00:00Commented Oct 30, 2017 at 4:54
4 Answers
Simply add "requestFocus" to your XML. Something like
<EditText
android:id="@+id/editText"
... />
<requestFocus />
and in onCreate()
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Or: It could be just
editText.requestFocus();
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Comments
This answer is based on the following assumption: You are using a WebView in your android app to insert a custom Javascript to achieve a certain task
Firstly create a method in your Android java class that will do do the job of popping up the keyboard:
@JavascriptInterface
public void takeUserInput() {
mWebView.setFocusable(true);
mWebView.setFocusableInTouchMode(true);
}
In your Javascript make a call to the takeUserInput() method whenever you want to pop-up the keyboard.
You can read up on how to call android functions from Javascript.
Hope this helps!
Comments
You have to make sure that the window's soft input mode is set to "always visible" before requesting focus on your element. You can set it using:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
After that you can bring up the keyboard by requesting focus on your element:
element.requestFocus();
Comments
According to my Knowledge you can't open the keyboard without using a input. Try to use virtual keyboard read following.