├── .gitignore ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle.kts ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── github │ │ └── boybeak │ │ └── skbglobal │ │ └── ExampleInstrumentedTest.kt │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── github │ │ │ └── boybeak │ │ │ └── skbglobal │ │ │ ├── App.kt │ │ │ └── MainActivity.kt │ └── res │ │ ├── drawable │ │ ├── ic_launcher_background.xml │ │ └── ic_launcher_foreground.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.webp │ │ └── ic_launcher_round.webp │ │ ├── values-night │ │ └── themes.xml │ │ ├── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── themes.xml │ │ └── xml │ │ ├── backup_rules.xml │ │ └── data_extraction_rules.xml │ └── test │ └── java │ └── com │ └── github │ └── boybeak │ └── skbglobal │ └── ExampleUnitTest.kt ├── arts ├── landscape.png └── skb-global.gif ├── build.gradle.kts ├── gradle.properties ├── gradle ├── libs.versions.toml └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── jitpack.yml ├── settings.gradle.kts └── skb-global ├── .gitignore ├── build.gradle.kts ├── consumer-rules.pro ├── proguard-rules.pro └── src ├── androidTest └── java │ └── com │ └── github │ └── boybeak │ └── skbglobal │ └── ExampleInstrumentedTest.kt ├── main ├── AndroidManifest.xml └── java │ └── com │ └── github │ └── boybeak │ └── skbglobal │ ├── KeyboardObserver.kt │ ├── ObserverManager.kt │ └── SoftKeyboardGlobal.kt └── test └── java └── com └── github └── boybeak └── skbglobal └── ExampleUnitTest.kt /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/* 5 | 6 | **/*.DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | .cxx 11 | local.properties 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 boybeak 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SoftKeyboardGlobal ![version](https://jitpack.io/v/boybeak/skb-global.svg) 2 | 3 | May be the best solution for Android soft keyboard listening。 4 | 5 | ![gif](arts/skb-global.gif) 6 | 7 | ## Installation 8 | 9 | Add **jitpack** to your `settings.gradle.kts`. 10 | 11 | ```groovy 12 | dependencyResolutionManagement { 13 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) 14 | repositories { 15 | mavenCentral() 16 | maven { url 'https://jitpack.io' } 17 | } 18 | } 19 | ``` 20 | 21 | then, add dependency. 22 | 23 | ```groovy 24 | dependencies { 25 | implementation 'com.github.boybeak:skb-global:Tag' 26 | } 27 | ``` 28 | The newest version is: ![version](https://jitpack.io/v/boybeak/skb-global.svg) 29 | 30 | # Usage 31 | You can use it **globally** or **locally**. 32 | 33 | ### Globally 34 | Initialize `SoftKeyboardGlobal` before using it. 35 | 36 | ```kotlin 37 | class App : Application() { 38 | 39 | override fun onCreate() { 40 | super.onCreate() 41 | SoftKeyboardGlobal.install(this, true) 42 | } 43 | } 44 | ``` 45 | You can pass `true` to second parameter to display a debug UI indicator. 46 | 47 | ```kotlin 48 | SoftKeyboardGlobal.addSoftKeyboardCallback(object : SoftKeyboardGlobal.SoftKeyboardCallback { 49 | override fun onOpen(height: Int) { 50 | Log.d(TAG, "onOpen height=$height") 51 | } 52 | 53 | override fun onClose() { 54 | Log.d(TAG, "onClose") 55 | } 56 | 57 | override fun onHeightChanged(height: Int) { 58 | Log.d(TAG, "onHeightChanged height=$height") 59 | } 60 | }) 61 | ``` 62 | 63 | You can observe soft keyboard state change and height change at any where in your app. 64 | 65 | ### Locally 66 | ```kotlin 67 | class MainActivity : AppCompatActivity() { 68 | 69 | private val observer by lazy { KeyboardObserver.create(this, true) } 70 | private val switchBtn: SwitchCompat by lazy { findViewById(R.id.switchBtn) } 71 | 72 | override fun onCreate(savedInstanceState: Bundle?) { 73 | super.onCreate(savedInstanceState) 74 | setContentView(R.layout.activity_main) 75 | 76 | switchBtn.setOnCheckedChangeListener { buttonView, isChecked -> 77 | if (isChecked) { 78 | observer.watch() 79 | } else { 80 | observer.unwatch() 81 | } 82 | } 83 | } 84 | } 85 | ``` 86 | You can pass `true` to second parameter when creating a `KeyboardObserver` to display a debug UI indicator. 87 | 88 | ## Q&A 89 | ### 1. Landscape problem 90 | The `EditText`'s default behavior for landscape is **ExtractUi**. 91 | 92 | ![landscape](./arts/landscape.png) 93 | 94 | You need add `android:imeOptions="flagNoExtractUi"` to your `EditText` in xml or use `editText.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI` in program. -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /app/build.gradle.kts: -------------------------------------------------------------------------------- 1 | plugins { 2 | alias(libs.plugins.android.application) 3 | alias(libs.plugins.jetbrains.kotlin.android) 4 | } 5 | 6 | android { 7 | namespace = "com.github.boybeak.skbglobal" 8 | compileSdk = 34 9 | 10 | defaultConfig { 11 | applicationId = "com.github.boybeak.skbglobal" 12 | minSdk = 24 13 | targetSdk = 34 14 | versionCode = 1 15 | versionName = "1.0" 16 | 17 | testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" 18 | } 19 | 20 | buildTypes { 21 | release { 22 | isMinifyEnabled = false 23 | proguardFiles( 24 | getDefaultProguardFile("proguard-android-optimize.txt"), 25 | "proguard-rules.pro" 26 | ) 27 | } 28 | } 29 | compileOptions { 30 | sourceCompatibility = JavaVersion.VERSION_1_8 31 | targetCompatibility = JavaVersion.VERSION_1_8 32 | } 33 | kotlinOptions { 34 | jvmTarget = "1.8" 35 | } 36 | } 37 | 38 | dependencies { 39 | 40 | implementation(libs.androidx.core.ktx) 41 | implementation(libs.androidx.appcompat) 42 | implementation(libs.material) 43 | implementation(libs.androidx.activity) 44 | implementation(libs.androidx.constraintlayout) 45 | 46 | implementation(project(":skb-global")) 47 | // implementation(libs.skb.global) 48 | 49 | testImplementation(libs.junit) 50 | androidTestImplementation(libs.androidx.junit) 51 | androidTestImplementation(libs.androidx.espresso.core) 52 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile -------------------------------------------------------------------------------- /app/src/androidTest/java/com/github/boybeak/skbglobal/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.github.boybeak.skbglobal 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.github.boybeak.skbglobal", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 16 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/github/boybeak/skbglobal/App.kt: -------------------------------------------------------------------------------- 1 | package com.github.boybeak.skbglobal 2 | 3 | import android.app.Application 4 | import android.util.Log 5 | 6 | class App : Application() { 7 | 8 | companion object { 9 | private const val TAG = "App" 10 | } 11 | 12 | override fun onCreate() { 13 | super.onCreate() 14 | SoftKeyboardGlobal.install(this, true) 15 | SoftKeyboardGlobal.addSoftKeyboardCallback(object : SoftKeyboardGlobal.SoftKeyboardCallback { 16 | override fun onOpen(height: Int) { 17 | Log.d(TAG, "onOpen height=$height") 18 | } 19 | 20 | override fun onClose() { 21 | Log.d(TAG, "onClose") 22 | } 23 | 24 | override fun onHeightChanged(height: Int) { 25 | Log.d(TAG, "onHeightChanged height=$height") 26 | } 27 | }) 28 | } 29 | } -------------------------------------------------------------------------------- /app/src/main/java/com/github/boybeak/skbglobal/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.github.boybeak.skbglobal 2 | 3 | import android.os.Bundle 4 | import androidx.activity.enableEdgeToEdge 5 | import androidx.appcompat.app.AppCompatActivity 6 | import androidx.appcompat.widget.SwitchCompat 7 | import androidx.core.view.ViewCompat 8 | import androidx.core.view.WindowInsetsCompat 9 | 10 | class MainActivity : AppCompatActivity() { 11 | 12 | private val observer by lazy { 13 | KeyboardObserver.create(this, true) 14 | } 15 | 16 | private val switchBtn: SwitchCompat by lazy { 17 | findViewById(R.id.switchBtn) 18 | } 19 | 20 | override fun onCreate(savedInstanceState: Bundle?) { 21 | super.onCreate(savedInstanceState) 22 | enableEdgeToEdge() 23 | setContentView(R.layout.activity_main) 24 | ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets -> 25 | val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()) 26 | v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom) 27 | insets 28 | } 29 | 30 | switchBtn.setOnCheckedChangeListener { buttonView, isChecked -> 31 | if (isChecked) { 32 | observer.watch() 33 | } else { 34 | observer.unwatch() 35 | } 36 | } 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 18 | 24 | 25 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/boybeak/skb-global/6889007f776df2054521834c5d60910eb11db464/app/src/main/res/mipmap-hdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/boybeak/skb-global/6889007f776df2054521834c5d60910eb11db464/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/boybeak/skb-global/6889007f776df2054521834c5d60910eb11db464/app/src/main/res/mipmap-mdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/boybeak/skb-global/6889007f776df2054521834c5d60910eb11db464/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/boybeak/skb-global/6889007f776df2054521834c5d60910eb11db464/app/src/main/res/mipmap-xhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/boybeak/skb-global/6889007f776df2054521834c5d60910eb11db464/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/boybeak/skb-global/6889007f776df2054521834c5d60910eb11db464/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/boybeak/skb-global/6889007f776df2054521834c5d60910eb11db464/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/boybeak/skb-global/6889007f776df2054521834c5d60910eb11db464/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/boybeak/skb-global/6889007f776df2054521834c5d60910eb11db464/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /app/src/main/res/values-night/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FF000000 4 | #FFFFFFFF 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | SoftKeyboardGlobal 3 | -------------------------------------------------------------------------------- /app/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 |