updated softKeyboard extensions

This commit is contained in:
Vladimir 2020-07-08 16:17:26 +03:00
parent c0545e88f7
commit 6dfbe07a5d
4 changed files with 65 additions and 13 deletions

View File

@ -24,15 +24,3 @@ fun View.setOnRippleClickListener(listener: () -> Unit) {
setOnClickListener { listener() }
}
}
fun View.showSoftInput() {
requestFocus()
val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}
fun View.hideSoftInput() {
clearFocus()
val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(windowToken, 0)
}

View File

@ -38,7 +38,7 @@ import ru.touchin.roboswag.core.observables.storable.NonNullStorable;
public final class PreferenceUtils {
/**
* Creates {@link Storable} that stores string into {@link SharedPreferences}.
* Creates {@link Storable} that stores string into {@link SharedPreferences}. Default value is null
*
* @param name Name of preference;
* @param preferences Preferences to store value;

View File

@ -205,6 +205,10 @@ object UiUtils {
* @param view [View] to get ID from;
* @return Readable ID.
*/
@Deprecated(
message = "Use extension instead",
replaceWith = ReplaceWith("view.getViewIdString()")
)
fun getViewIdString(view: View): String = try {
view.resources.getResourceName(view.id)
} catch (exception: Resources.NotFoundException) {
@ -214,6 +218,10 @@ object UiUtils {
/**
* Hides device keyboard for target activity.
*/
@Deprecated(
message = "Use extension instead",
replaceWith = ReplaceWith("activity.hideSoftInput()")
)
fun hideSoftInput(activity: Activity) {
activity.currentFocus?.let(this::hideSoftInput)
}
@ -221,6 +229,10 @@ object UiUtils {
/**
* Hides device keyboard for target view.
*/
@Deprecated(
message = "Use extension instead",
replaceWith = ReplaceWith("view.hideSoftInput()")
)
fun hideSoftInput(view: View) {
view.clearFocus()
val inputManager = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
@ -234,6 +246,10 @@ object UiUtils {
*
* @param view View to get focus for input from keyboard.
*/
@Deprecated(
message = "Use extension instead",
replaceWith = ReplaceWith("view.showSoftInput()")
)
fun showSoftInput(view: View) {
view.requestFocus()
val inputManager = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager

View File

@ -0,0 +1,48 @@
package ru.touchin.roboswag.components.utils
import android.app.Activity
import android.content.Context
import android.content.res.Resources
import android.view.View
import android.view.inputmethod.InputMethodManager
/**
* Returns string representation of [View]'s ID.
*
* @param view [View] to get ID from;
* @return Readable ID.
*/
fun View.getViewIdString(): String = try {
resources.getResourceName(id)
} catch (exception: Resources.NotFoundException) {
id.toString()
}
/**
* Hides device keyboard for target activity.
*/
fun Activity.hideSoftInput() {
currentFocus?.hideSoftInput()
}
/**
* Hides device keyboard for target view.
*/
fun View.hideSoftInput() {
clearFocus()
val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(windowToken, 0)
}
/**
* Shows device keyboard over [Activity] and focuses [View].
* Do NOT use it if keyboard is over [android.app.Dialog] - it won't work as they have different [Activity.getWindow].
* Do NOT use it if you are not sure that view is already added on screen.
*
* @param view View to get focus for input from keyboard.
*/
fun View.showSoftInput() {
requestFocus()
val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}