Now, I try to hide the softkeyboard when user touch outside the keyboard:
((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(editView.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
I want put the logic in my base activity class, so if it is possible to getWindowToken without View?
I faced exactly the same problem, while writing OnPageChangeListener within an Activity. You can use one of these solutions. Either:
getWindow().getDecorView().getRootView().getWindowToken()
or:
findViewById(android.R.id.content).getWindowToken()
Surely you can use:
getContentView().getWindowToken()
or you can refer to SO Quest
Simply use getWindow().getDecorView().getWindowToken()
public static final String M_TOKEN = "mToken";
#Nullable
protected IBinder getToken(Activity activity) {
try {
Field mTokenField = Activity.class.getDeclaredField(M_TOKEN);
mTokenField.setAccessible(true);
IBinder mToken = (IBinder) mTokenField.get(activity);
return mToken;
} catch (NoSuchFieldException e) {
// handle
} catch (IllegalAccessException e) {
// handle
}
return null;
}
You could just get the token from the WindowManager.LayoutParams of the window directly
getWindow().getAttributes().token
In kotlin:
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(window.attributes.token, 0)
Or, If you have a view:
imm.hideSoftInputFromWindow(view.windowToken, 0)
You can try this on your manifest file activity tag to hide keyboard.
android:windowSoftInputMode="stateHidden"
Related
I want to test keyboard visibility when an activity calls onCreate() and onResume().
How can i test whether or not the keyboard is shown using espresso?
I know, that the question is old enough, but it doesn't have any accepted answer though.
In our UI tests we use this method, which uses some shell commands:
/**
* This method works like a charm
*
* SAMPLE CMD OUTPUT:
* mShowRequested=true mShowExplicitlyRequested=true mShowForced=false mInputShown=true
*/
fun isKeyboardOpenedShellCheck(): Boolean {
val checkKeyboardCmd = "dumpsys input_method | grep mInputShown"
try {
return UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
.executeShellCommand(checkKeyboardCmd).contains("mInputShown=true")
} catch (e: IOException) {
throw RuntimeException("Keyboard check failed", e)
}
}
Hope, it'll be useful for someone
fun isKeyboardShown(): Boolean {
val inputMethodManager = InstrumentationRegistry.getInstrumentation().targetContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
return inputMethodManager.isAcceptingText
}
found at Google groups
another trick could be checking for the visibility of a view that you know is going to be covered when the keyboard is showing. don't forget to take animations into consideration...
instrumentation testing using espresso and hamcrest for the NOT matcher something like:
//make sure keyboard is visible by clicking on an edit text component
ViewInteraction v = onView(withId(R.id.editText));
ViewInteraction v2 = onView(withId(R.id.componentVisibleBeforeKeyboardIsShown));
v2.check(matches(isDisplayed()));
v.perform(click());
//add a small delay because of the showing keyboard animation
SystemClock.sleep(500);
v2.check(matches(not(isDisplayed())));
hideKeyboardMethod();
//add a small delay because of the hiding keyboard animation
SystemClock.sleep(500);
v2.check(matches(isDisplayed()));
This works for me.
private boolean isSoftKeyboardShown() {
final InputMethodManager imm = (InputMethodManager) getActivityInstance()
.getSystemService(Context.INPUT_METHOD_SERVICE);
return imm.isAcceptingText();
}
Java version of #igork's answer.
This method is working for me
val isKeyboardOpened: Boolean
get() {
for (window in InstrumentationRegistry.getInstrumentation().uiAutomation.windows) {
if (window.type == AccessibilityWindowInfo.TYPE_INPUT_METHOD) {
return true
}
}
return false
}
I had tried this out. And it works fine with Samsung tablet.
On Page 1_4.html i have to hide keypad and on 2.html I Have to show
keypad.
Both on textbox click inside webview
NOTE: Android Activity is Same.
I'm calling this code on webView.setOnTouchListener
if (value.equals("1") || value.equals("4")) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
} else if(value.equals("2")) {
getWindow().clearFlags( WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}
But its not working in any mobile phones. It gives me Warning
W/InputMethodManager(25060): startInputInner : InputBindResult == null
I have google it. But didn't find anything useful.
What should I do now? Any help will be Appreciated.
To open keypad try this
webview.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager)
MainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(webview, 0);
}
},100);
To close keypad try this
InputMethodManager inputMethodManager = (InputMethodManager)MainActivity.this.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(MainActivity.this.getCurrentFocus().getWindowToken(), 0);
try this code : This is working on all devices.
try
{
if(isopen)
{
// to hide keyboard
InputMethodManager inputMethodManager = (InputMethodManager)context.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}else{
// to open keyboard
InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(linearLayout.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
}
catch (NullPointerException e)
{
}
catch (Exception e)
{
}
I've seen this question:
Changing the ActionBar hide animation?
But it doesn't say whether it's possible to disable animation altogether.
You can now do this,
getSupportActionBar().setShowHideAnimationEnabled(false);
I fixed using the below method:
public static void disableShowHideAnimation(ActionBar actionBar) {
try
{
actionBar.getClass().getDeclaredMethod("setShowHideAnimationEnabled", boolean.class).invoke(actionBar, false);
}
catch (Exception exception)
{
try {
Field mActionBarField = actionBar.getClass().getSuperclass().getDeclaredField("mActionBar");
mActionBarField.setAccessible(true);
Object icsActionBar = mActionBarField.get(actionBar);
Field mShowHideAnimationEnabledField = icsActionBar.getClass().getDeclaredField("mShowHideAnimationEnabled");
mShowHideAnimationEnabledField.setAccessible(true);
mShowHideAnimationEnabledField.set(icsActionBar,false);
Field mCurrentShowAnimField = icsActionBar.getClass().getDeclaredField("mCurrentShowAnim");
mCurrentShowAnimField.setAccessible(true);
mCurrentShowAnimField.set(icsActionBar,null);
}catch (Exception e){
//....
}
}
}
If you use ActionBarSherlock then you can do it. See ActionBarImpl class, it has setShowHideAnimationEnabled(boolean enabled) method.
I have created an android custom keyboard. After pressing a button on it, I'd like it to change the keyboard back to the previous keyboard, presumable using InputMethodManager.setInputMethod(IBinder token, String id);
However, I can't work out where to get the token from - using getCurrentInputBinding().getConnectionToken() doesn't work.
Does anyone know where to find the token?
Thanks,
Ed
Turns out that the switchInputMethod(String id) method works a treat - no need for that token.
You get the token from the view by view.getWindowToken().
You can use this Method to get Token and activate last used Keyboard
private fun switchToLastKeyboard() {
try {
val imm: InputMethodManager =
this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
val token = this.window.window!!.attributes.token
//imm.setInputMethod(token, LATIN);
imm.switchToLastInputMethod(token)
} catch (t: Throwable) { // java.lang.NoSuchMethodError if API_level<11
Log.i("TAG", "onCreateInputView: Throwable " + t.message)
}
}
I have an application where I would like to warn the user if they are not using the default Android softkeyboard. (i.e. they are using Swype or some thing else).
How can I check which input method they currently have selected?
You can get a default IME, use:
Settings.Secure.getString(getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
InputMethodManager has getEnabledInputMethodList(). You get an InputMethodManager from getSystemService() in your Activity.
Here's a bit of code I used to determine if GoogleKeyboard, Samsung Keyboard, or Swype Keyboard is used. The value returned by reflection for mCurId indicates the IME ID.
Test with the different keyboards/input methods you are looking for to find the relevant one
public boolean usingSamsungKeyboard(Context context){
return usingKeyboard(context, "com.sec.android.inputmethod/.SamsungKeypad");
}
public boolean usingSwypeKeyboard(Context context){
return usingKeyboard(context, "com.nuance.swype.input/.IME");
}
public boolean usingGoogleKeyboard(Context context){
return usingKeyboard(context, "com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME");
}
public boolean usingKeyboard(Context context, String keyboardId)
{
final InputMethodManager richImm =
(InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
boolean isKeyboard = false;
final Field field;
try
{
field = richImm.getClass().getDeclaredField("mCurId");
field.setAccessible(true);
Object value = field.get(richImm);
isKeyboard = value.equals(keyboardId);
}
catch (IllegalAccessException e)
{
}
catch (NoSuchFieldException e)
{
}
return isKeyboard;
}