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:
  • 263 Vote(s) - 3.59 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hide keyboard when navigating from a fragment to another

#1
I have a Fragment that contains an Edit Text. When the Edit Text is pressed, the keyboard is being shown. When pressed the Save button in the upper corner, the application returns to the previous fragment, but the keyboard persists.

I would like the keyboard to be hidden when navigating to the previous fragment.

Please, note that I tried this solution:

[To see links please register here]

.

InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myView.getWindowToken(), 0);

I tried to use this in both fragments, in the onCreate method.


I also tried to hide the soft keyboard in the layout:

android:windowSoftInputMode="stateAlwaysHidden"

None of these worked, unfortunately.

I would have posted some pictures, but I do not have enough reputation yet.
I would appreciate any constructive help and opinion and do not forget that "A wise man can learn more from a foolish question than a fool can learn from a wise answer." :)

Regards,
Alexandra
Reply

#2
Put the code that hides the keyboard in your "save button" click listener, and use this method to hide the keyboard:

public static void hideKeyboard(Activity activity) {
InputMethodManager inputManager = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);

// check if no view has focus:
View currentFocusedView = activity.getCurrentFocus();
if (currentFocusedView != null) {
inputManager.hideSoftInputFromWindow(currentFocusedView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
Reply

#3
Easiest way to hide keyboard in fragment or Activity

Soluton : 1


//hide keyboard
public static void hideKeyboard(Context ctx) {
InputMethodManager inputManager = (InputMethodManager) ctx
.getSystemService(Context.INPUT_METHOD_SERVICE);

// check if no view has focus:
View v = ((Activity) ctx).getCurrentFocus();
if (v == null)
return;

inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}



Solution : 2


InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
Reply

#4
public void hideKeyboard(Activity activity) {
InputMethodManager inputManager = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);

// check if no view has focus:
View currentFocusedView = activity.getCurrentFocus();
if (currentFocusedView != null) {
inputManager.hideSoftInputFromWindow(currentFocusedView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
Reply

#5
@Override
public void onDestroyView() {
super.onDestroyView();
View view = getActivity().getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
Reply

#6
Kotlin
------
For Kotlin, you can use this as a top level function, just add the code to a separate class such as `Utils.kt`.

fun hideKeyboard(activity: Activity) {
val inputMethodManager =
activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

// Check if no view has focus
val currentFocusedView = activity.currentFocus
currentFocusedView?.let {
inputMethodManager.hideSoftInputFromWindow(
currentFocusedView.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
}

To access it from Fragment, call it like:

hideKeyboard(activity as YourActivity)

Thanks to Silvia H for Java code.
Reply

#7
You can hide the keyboard by putting this code in your onPause in the fragment you're leaving.

@Override
public void onPause() {
super.onPause();
InputMethodManager imm = (InputMethodManager)
requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myView.getWindowToken(), 0);
}
Reply

#8
Using above answers, i tried to implement it for ones who uses navigation component.

Try below code and it will work fine

private fun setupNavigatingListener() {
navController.addOnDestinationChangedListener { navController: NavController, navDestination: NavDestination, bundle: Bundle? ->
currentFocus?.let { Utils.hideKeyboard(it) }
}
}

Utils class

fun hideKeyboard(view: View) {
val inputManager =
view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_IMPLICIT_ONLY)
}
Reply

#9
With Kotlin its very easy:

simple create an extension function can call it from anywhere you get context

fun Fragment.hideKeyboard() {
view?.let { activity?.hideKeyboard(it) }
}

fun Activity.hideKeyboard() {
hideKeyboard(currentFocus ?: View(this))
}

fun Context.hideKeyboard(view: View) {
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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