Skip to content

Commit

Permalink
Replace show/hide keyboard extensions with new implementation based o…
Browse files Browse the repository at this point in the history
…n WindowInsetsControllerCompat.
  • Loading branch information
ychescale9 committed Feb 26, 2021
1 parent 4bfb5fa commit 11be15e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
7 changes: 5 additions & 2 deletions blueprint-ui/api/blueprint-ui.api
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
public final class reactivecircus/blueprint/ui/extension/ActivityKt {
public static final fun getScreenSize (Landroid/app/Activity;)Landroid/util/DisplayMetrics;
public static final fun hideKeyboard (Landroid/app/Activity;Landroid/view/View;)V
public static final fun hideStatusBar (Landroid/app/Activity;)V
public static final fun setStatusBarColor (Landroid/app/Activity;IZ)V
public static synthetic fun setStatusBarColor$default (Landroid/app/Activity;IZILjava/lang/Object;)V
public static final fun showKeyboard (Landroid/app/Activity;)V
public static final fun showStatusBar (Landroid/app/Activity;)V
}

Expand All @@ -18,3 +16,8 @@ public final class reactivecircus/blueprint/ui/extension/ContextKt {
public static final fun tintVectorDrawable (Landroid/content/Context;Landroid/content/res/Resources$Theme;II)Landroid/graphics/drawable/Drawable;
}

public final class reactivecircus/blueprint/ui/extension/WindowKt {
public static final fun hideSoftKeyboard (Landroid/view/Window;)V
public static final fun showSoftKeyboard (Landroid/view/Window;)V
}

Original file line number Diff line number Diff line change
@@ -1,32 +1,13 @@
package reactivecircus.blueprint.ui.extension

import android.app.Activity
import android.content.Context
import android.os.Build
import android.util.DisplayMetrics
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat

/**
* Programmatically close soft keyboard.
*/
public fun Activity.hideKeyboard(focusedView: View) {
val inputManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputManager.hideSoftInputFromWindow(focusedView.windowToken, 0)
}

/**
* Programmatically show soft keyboard.
*/
public fun Activity.showKeyboard() {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
}

/**
* Shows status bar on the activity.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package reactivecircus.blueprint.ui.extension

import android.view.Window
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat

/**
* Programmatically shows the soft keyboard.
*/
public fun Window.showSoftKeyboard() {
WindowInsetsControllerCompat(this, decorView.findViewById(android.R.id.content))
.show(WindowInsetsCompat.Type.ime())
}

/**
* Programmatically hides the soft keyboard.
*/
public fun Window.hideSoftKeyboard() {
WindowInsetsControllerCompat(this, decorView.findViewById(android.R.id.content))
.hide(WindowInsetsCompat.Type.ime())
}

0 comments on commit 11be15e

Please sign in to comment.