Merge pull request #141 from TouchInstinct/added-keyboard-method

Added show/hide keyboard input ext.
This commit is contained in:
Aksenov Vladimir 2020-07-08 18:41:36 +05:00 committed by GitHub
commit 7e99274780
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 21 deletions

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

@ -23,7 +23,6 @@ import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.Intent
import android.content.res.Resources
import android.os.Build
import android.util.DisplayMetrics
import android.util.TypedValue
@ -33,7 +32,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewConfiguration
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import androidx.annotation.LayoutRes
import ru.touchin.roboswag.components.utils.spans.getSpannedTextWithUrls
@ -205,27 +203,30 @@ object UiUtils {
* @param view [View] to get ID from;
* @return Readable ID.
*/
fun getViewIdString(view: View): String = try {
view.resources.getResourceName(view.id)
} catch (exception: Resources.NotFoundException) {
view.id.toString()
}
@Deprecated(
message = "Use extension instead",
replaceWith = ReplaceWith("view.getViewIdString()")
)
fun getViewIdString(view: View): String = view.getViewIdString()
/**
* Hides device keyboard for target activity.
*/
fun hideSoftInput(activity: Activity) {
activity.currentFocus?.let(this::hideSoftInput)
}
@Deprecated(
message = "Use extension instead",
replaceWith = ReplaceWith("activity.hideSoftInput()")
)
fun hideSoftInput(activity: Activity) = activity.hideSoftInput()
/**
* Hides device keyboard for target view.
*/
fun hideSoftInput(view: View) {
view.clearFocus()
val inputManager = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(view.windowToken, 0)
}
@Deprecated(
message = "Use extension instead",
replaceWith = ReplaceWith("view.hideSoftInput()")
)
fun hideSoftInput(view: View) = view.hideSoftInput()
/**
* Shows device keyboard over [Activity] and focuses [View].
@ -234,11 +235,11 @@ object UiUtils {
*
* @param view View to get focus for input from keyboard.
*/
fun showSoftInput(view: View) {
view.requestFocus()
val inputManager = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
@Deprecated(
message = "Use extension instead",
replaceWith = ReplaceWith("view.showSoftInput()")
)
fun showSoftInput(view: View) = view.showSoftInput()
}

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)
}