Skip to content

Commit

Permalink
feat: finally after long time we have an ability to show hide the key…
Browse files Browse the repository at this point in the history
…board, it took em 12 years
  • Loading branch information
CraZyLegenD committed Nov 13, 2020
1 parent d9dcc12 commit 908ee92
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 100 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ext {
retrofit = "2.9.0"
rxBindings = "4.0.0"
glide = "4.11.0"
constraint_layout = '2.0.2'
constraint_layout = '2.0.4'
coroutines = '1.4.0'
nav_version = '2.3.1'
rx = '3.0.0'
Expand All @@ -12,7 +12,7 @@ ext {
fragment = "1.3.0-beta01"
activity = "1.2.0-beta01"
appCompat = "1.2.0"
coreKTX = "1.5.0-alpha04"
coreKTX = "1.5.0-alpha05"
securityVersion = "1.1.0-alpha02"


Expand Down Expand Up @@ -43,7 +43,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.android.tools.build:gradle:4.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,26 +435,6 @@ fun Activity.enableFullScreen() {
}


fun Activity?.hideKeyboardForced() {
this?.currentFocus?.let { currentFocus ->
try {
(currentFocus.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
.hideSoftInputFromWindow(currentFocus.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
} catch (e: Exception) {
}
}
}

fun Activity?.hideKeyboard() {
(this?.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager)
.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}

fun Activity?.showKeyboard() {
(this?.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager)
.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
}

fun Activity.keepScreenOn() {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,6 @@ import java.io.File



fun Context.showKeyboard(toFocus: View) {
toFocus.requestFocus()
val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
// imm.showSoftInput(toFocus, InputMethodManager.SHOW_IMPLICIT);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY)
}


fun Context.hideKeyboard() {
val activity = this as Activity?
if (activity != null) {
val inputManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?

if (inputManager != null) {
val v = this.currentFocus
if (v != null) {
inputManager.hideSoftInputFromWindow(
v.windowToken, InputMethodManager.HIDE_NOT_ALWAYS
)
}
}
}
}


inline fun Context.openEmail(title: String = "Select an email client", onCantHandleAction: () -> Unit = {}) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package com.crazylegend.kotlinextensions.dialog

import android.app.Dialog
import android.content.Context
import android.content.Context.INPUT_METHOD_SERVICE
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.TextView
import androidx.annotation.*
import androidx.appcompat.app.AlertDialog
Expand Down Expand Up @@ -182,16 +179,6 @@ inline fun AlertDialog.Builder.neutralButton(text: String, crossinline handleCli
}


/**Method to hide keyboard in dialog only*/
fun Dialog.hideKeyboard() {
val imm = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(currentFocus?.windowToken, 0)
}

/**Method to show keyboard in dialog only*/
fun Dialog.showKeyboard() =
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)


/**
* Make the background transparent of the dialog window
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import android.os.Handler
import android.os.Looper
import android.util.TypedValue
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.annotation.*
import androidx.appcompat.app.AlertDialog
Expand Down Expand Up @@ -554,19 +553,6 @@ fun FragmentManager.applyActions(actions: (fragmentTransaction: FragmentTransact
}
}

fun Fragment?.hideKeyboard() {
(this?.context?.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager)
.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}

fun Fragment.showKeyboard() {
(context?.getSystemService(Activity.INPUT_METHOD_SERVICE) as? InputMethodManager)
?.also { inputMethodManager ->
view?.rootView?.apply { post { inputMethodManager.showSoftInput(this, 0) } }
?: inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
}
}


/**
* An extension to `postponeEnterTransition` which will resume after a timeout.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ val View.imeHeight
.getInsets(WindowInsetsCompat.Type.ime()).bottom


@RequiresApi(Build.VERSION_CODES.R)
fun View.showKeyboard() = windowInsetsController?.show(WindowInsetsCompat.Type.ime())

@RequiresApi(Build.VERSION_CODES.R)
fun View.hideKeyboard() = windowInsetsController?.hide(WindowInsetsCompat.Type.ime())

fun View.hideKeyboard() {
ViewCompat.getWindowInsetsController(this)?.hide(WindowInsetsCompat.Type.ime())
}

fun View.showKeyboard() {
ViewCompat.getWindowInsetsController(this)?.show(WindowInsetsCompat.Type.ime())
}

fun View.showKeyboardAndFocus() {
ViewCompat.getWindowInsetsController(this)?.show(WindowInsetsCompat.Type.ime())
requestFocus()
}

fun View.imeVisibilityListener(onVisibilityChanged: (Boolean) -> Unit) {
ViewCompat.setOnApplyWindowInsetsListener(this) { _, insets ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.crazylegend.kotlinextensions.views
import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import android.os.Handler
import android.os.Looper
import android.text.*
import android.view.*
import android.view.inputmethod.EditorInfo
Expand All @@ -14,6 +15,8 @@ import com.crazylegend.kotlinextensions.activity.isKeyboardSubmit
import com.crazylegend.kotlinextensions.context.clipboardManager
import com.crazylegend.kotlinextensions.context.getCompatDrawable
import com.crazylegend.kotlinextensions.context.inputMethodManager
import com.crazylegend.kotlinextensions.insets.hideKeyboard
import com.crazylegend.kotlinextensions.insets.showKeyboard
import com.google.android.material.textfield.TextInputEditText
import java.net.MalformedURLException
import java.net.URL
Expand Down Expand Up @@ -381,42 +384,25 @@ inline fun EditText.onUnFocused(crossinline block: () -> Unit) {
}
}

/**
* Shows keyboard for this edit text
*/
fun EditText.showKeyboard() {
requestFocus()
context.inputMethodManager.showSoftInput(this, InputMethodManager.SHOW_IMPLICIT)
}

/**
* Shows keyboard for this edit text with delay
*
* @param delayMillis - delay in milliseconds before keyboard will be shown
*/
fun EditText.showKeyboardDelayed(delayMillis: Long) {
Handler().postDelayed({
Handler(Looper.getMainLooper()).postDelayed({
requestFocus()
showKeyboard()
}, delayMillis)
}

/**
* Hides keyboard for this edit text
*/
fun EditText.hideKeyboard() =
context.inputMethodManager.hideSoftInputFromWindow(
windowToken,
InputMethodManager.HIDE_NOT_ALWAYS
)

/**
* Hides keyboard for this edit text with delay
*
* @param delayMillis - delay in milliseconds before keyboard will be hided
*/
fun EditText.hideKeyboardDelayed(delayMillis: Long) =
Handler().postDelayed({ hideKeyboard() }, delayMillis)
Handler(Looper.getMainLooper()).postDelayed({ hideKeyboard() }, delayMillis)


@SuppressLint("ClickableViewAccessibility")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.crazylegend.kotlinextensions.views

import android.graphics.Rect
import android.view.View
import androidx.core.view.ViewCompat
import androidx.core.view.isGone
import androidx.core.view.isVisible
import com.crazylegend.kotlinextensions.context.dp2px
Expand Down Expand Up @@ -68,4 +69,13 @@ fun View.dp2px(dpValue: Int): Int? {

fun View.px2dp(pxValue: Int): Float? {
return context?.px2dp(pxValue)
}
}


val View.isAppearanceLightNavigationBars
get() =
ViewCompat.getWindowInsetsController(this)?.isAppearanceLightNavigationBars

val View.isAppearanceLightStatusBars
get() =
ViewCompat.getWindowInsetsController(this)?.isAppearanceLightStatusBars

0 comments on commit 908ee92

Please sign in to comment.