Skip to content

Commit

Permalink
Created detector and sample
Browse files Browse the repository at this point in the history
  • Loading branch information
konifar committed Apr 6, 2018
1 parent ffd2fc3 commit 8768bca
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 9 deletions.
12 changes: 10 additions & 2 deletions example/src/main/java/co/kyash/rxkeyboarddetector/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package co.kyash.rxkeyboarddetector;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.TextView
import co.kyash.rkd.KeyboardDetector

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val textView = findViewById<TextView>(R.id.status)

KeyboardDetector(this).observe().subscribe({
textView.text = it.name
})
}
}
62 changes: 55 additions & 7 deletions example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello World!" />
android:background="@color/white"
android:elevation="4dp"
app:elevation="4dp"
app:title="@string/app_name" />

</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/status"
style="@style/Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="@dimen/space_16dp" />

</LinearLayout>

</ScrollView>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/grey500" />

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/white"
android:paddingBottom="@dimen/space_16dp"
android:paddingEnd="@dimen/space_8dp"
android:paddingLeft="@dimen/space_8dp"
android:paddingRight="@dimen/space_8dp"
android:paddingStart="@dimen/space_8dp"
android:paddingTop="@dimen/space_16dp">

<EditText style="@style/BaseEditText" />

</FrameLayout>

</LinearLayout>
63 changes: 63 additions & 0 deletions library/src/main/java/co/kyash/rkd/KeyboardDetector.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package co.kyash.rkd

import android.app.Activity
import android.graphics.Rect
import android.util.DisplayMetrics
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.view.WindowManager
import io.reactivex.Observable


class KeyboardDetector constructor(
private val activity: Activity?
) {

companion object {
const val TAG = "KeyboardDetector"
const val MIN_KEYBOARD_HEIGHT_RATIO = 0.15
}

fun observe(): Observable<KeyboardStatus> {
if (activity == null) {
Log.w(TAG, "Activity is null")
return Observable.just(KeyboardStatus.CLOSED)
}

val softInputMethod = activity.window.attributes.softInputMode
if (WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE != softInputMethod
&& WindowManager.LayoutParams.SOFT_INPUT_ADJUST_UNSPECIFIED != softInputMethod) {
throw IllegalArgumentException("Activity window SoftInputMethod is not ADJUST_RESIZE")
}

val rootView = (activity.findViewById<View>(android.R.id.content) as ViewGroup)

val windowHeight = DisplayMetrics().let {
activity.windowManager.defaultDisplay.getMetrics(it)
it.heightPixels
}

return Observable.create<KeyboardStatus> { emitter ->
val listener = ViewTreeObserver.OnGlobalLayoutListener {
val rect = Rect().apply { rootView.getWindowVisibleDisplayFrame(this) }
val keyboardHeight = windowHeight - rect.height()

if (keyboardHeight > windowHeight * MIN_KEYBOARD_HEIGHT_RATIO) {
emitter.onNext(KeyboardStatus.OPENED)
} else {
emitter.onNext(KeyboardStatus.CLOSED)
}
}

rootView.viewTreeObserver.addOnGlobalLayoutListener(listener)

emitter.setCancellable {
rootView.viewTreeObserver.removeOnGlobalLayoutListener(listener)
}

}.distinctUntilChanged()
}

}
6 changes: 6 additions & 0 deletions library/src/main/java/co/kyash/rkd/KeyboardStatus.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package co.kyash.rkd

enum class KeyboardStatus {
OPENED,
CLOSED
}

0 comments on commit 8768bca

Please sign in to comment.