-
Notifications
You must be signed in to change notification settings - Fork 14
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
9 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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
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
82 changes: 82 additions & 0 deletions
82
app/src/main/java/soko/ekibun/bangumi/util/KeyboardUtil.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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package soko.ekibun.bangumi.util | ||
|
||
import android.app.Activity | ||
import android.graphics.Rect | ||
import android.os.Build | ||
import android.view.View | ||
import android.view.ViewTreeObserver | ||
import android.view.inputmethod.InputMethodManager | ||
|
||
/** | ||
* Created by mikepenz on 14.03.15. | ||
* This class implements a hack to change the layout padding on bottom if the keyboard is shown | ||
* to allow long lists with editTextViews | ||
* Basic idea for this solution found here: http://stackoverflow.com/a/9108219/325479 | ||
*/ | ||
class KeyboardUtil(act: Activity, private val contentView: View) { | ||
|
||
private val decorView: View = act.window.decorView | ||
|
||
|
||
//a small helper to allow showing the editText focus | ||
var onGlobalLayoutListener: ViewTreeObserver.OnGlobalLayoutListener = ViewTreeObserver.OnGlobalLayoutListener { | ||
val r = Rect() | ||
//r will be populated with the coordinates of your view that area still visible. | ||
decorView.getWindowVisibleDisplayFrame(r) | ||
|
||
//get screen height and calculate the difference with the useable area from the r | ||
val height = decorView.context.resources.displayMetrics.heightPixels | ||
val diff = height - r.bottom | ||
|
||
//if it could be a keyboard add the padding to the view | ||
if (diff != 0) { | ||
// if the use-able screen height differs from the total screen height we assume that it shows a keyboard now | ||
//check if the padding is 0 (if yes set the padding for the keyboard) | ||
if (contentView.paddingBottom != diff) { | ||
//set the padding of the contentView for the keyboard | ||
contentView.setPadding(0, 0, 0, diff) | ||
} | ||
} else { | ||
//check if the padding is != 0 (if yes reset the padding) | ||
if (contentView.paddingBottom != 0) { | ||
//reset the padding of the contentView | ||
contentView.setPadding(0, 0, 0, 0) | ||
} | ||
} | ||
} | ||
|
||
init { | ||
//only required on newer android versions. it was working on API level 19 | ||
if (Build.VERSION.SDK_INT >= 19) { | ||
decorView.viewTreeObserver.addOnGlobalLayoutListener(onGlobalLayoutListener) | ||
} | ||
} | ||
|
||
fun enable() { | ||
if (Build.VERSION.SDK_INT >= 19) { | ||
decorView.viewTreeObserver.addOnGlobalLayoutListener(onGlobalLayoutListener) | ||
} | ||
} | ||
|
||
fun disable() { | ||
if (Build.VERSION.SDK_INT >= 19) { | ||
decorView.viewTreeObserver.removeOnGlobalLayoutListener(onGlobalLayoutListener) | ||
} | ||
} | ||
|
||
companion object { | ||
|
||
|
||
/** | ||
* Helper to hide the keyboard | ||
* | ||
* @param act | ||
*/ | ||
fun hideKeyboard(act: Activity?) { | ||
if (act != null && act.currentFocus != null) { | ||
val inputMethodManager = act.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager | ||
inputMethodManager.hideSoftInputFromWindow(act.currentFocus!!.windowToken, 0) | ||
} | ||
} | ||
} | ||
} |