Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 458 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Android - Hide keyboard on Android 8

#1
I'm having troubles with hiding the keyboard on Android 8.
I used this before and it worked for the older androids:

val view = activity.currentFocus
if (view != null) {
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
Android 8 just ignores it and shows the keyboard anyway.
Probably making the input field unfocusable would help, but I really need it to be focusable, so this is not an option.
Reply

#2
Instead of `hideSoftInputFromWindow` you can use `toggleSoftInput`.

val imm: InputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
if (imm.isActive)
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)

works for Android 8 on the emulator at least
Reply

#3
I had a similar problem and I solved it like this:

class MainActivity : Activity() {

override fun onCreate(savedInstanceState: Bundle?) {
...
window.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)

}
}

> This is not the most elegant solution. But in my situation it was acceptable
Reply

#4
Use this method I have created

public static void showHideInput(Context context,boolean visibility, View view){

if (view != null) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (visibility) imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
else imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
Reply

#5
I had the same issue recently, and worked around it by providing the root view from (in my case) the fragment for the window token instead of the activity's current focus.
<BR><BR>This way, the keyboard is dismissed, and focus in the `EditText` is maintained.

Tested on Pixel 2 XL running `Android 8.1`:

/**
* Hides the Soft Keyboard on demand
*
* @param activity the activity from which to get the IMM
* @param view the view from which to provide a windowToken
*/
fun hideSoftKeyboard(activity: Activity, view: View?) {
val inputMethodManager = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view?.windowToken, 0)
}
Reply

#6
Hide keyboard inside runnable, with calling post method of your view:

view.post(() -> {
hideKeyboard(view);
}
Reply

#7
The @EarlOfEgo 's solution caused some troubles on older Android versions. This is the ideal solution, which works perfectly on all (at least almost) Android versions:

protected fun hideKeyboard() {
val view = activity.currentFocus
if(android.os.Build.VERSION.SDK_INT >= 26) {
val imm: InputMethodManager = context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
view?.post({
imm.hideSoftInputFromWindow(activity.currentFocus.windowToken, 0)
imm.hideSoftInputFromInputMethod(activity.currentFocus.windowToken, 0)
})
} else {
if (view != null) {
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, 0)
imm.hideSoftInputFromInputMethod(view.windowToken, 0)
}
}
}
Reply

#8
just try this method to intercept the focus event and hide the softkeyboard:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);

if (hasFocus) {
View lFocused = getCurrentFocus();
if (lFocused != null)
lFocused.postDelayed(new Runnable() {
@Override
public void run() {
InputMethodManager lInputManager = (InputMethodManager) pContext.getSystemService(Context.INPUT_METHOD_SERVICE);
lInputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}, 100);//Modified to 100ms to intercept SoftKeyBoard on Android 8 (Oreo) and hide it.
}
}
Reply

#9
Here you have two static functions to hide keyboard, depend on your case which one you want to use. I tested on Android Oreo and it works.

object UIHelper {

fun hideSoftKeyboard(activity: Activity?) {
if (activity != null) {
val inputManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
if (activity.currentFocus != null && inputManager != null) {
inputManager.hideSoftInputFromWindow(activity.currentFocus!!.windowToken, 0)
inputManager.hideSoftInputFromInputMethod(activity.currentFocus!!.windowToken, 0)
}
}
}

fun hideSoftKeyboard(view: View?) {
if (view != null) {
val inputManager = view!!.getContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager?.hideSoftInputFromWindow(view!!.getWindowToken(), 0)
}
}

fun showKeyboard(activityContext: Context, editText: EditText) {
editText.requestFocus()
Handler().postDelayed(Runnable {
val inputMethodManager = activityContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
}, 250)
}
}

Example of use:

1. `UIHelper.hideSoftKeyboard(this)`
2. `UIHelper.hideSoftKeyboard(passwordField)`

To show:

UIHelper.showKeyboard(this, passwordField)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through