-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
134 additions
and
9 deletions.
There are no files selected for viewing
12 changes: 10 additions & 2 deletions
12
example/src/main/java/co/kyash/rxkeyboarddetector/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package co.kyash.rkd | ||
|
||
enum class KeyboardStatus { | ||
OPENED, | ||
CLOSED | ||
} |