Created
April 19, 2018 00:46
-
-
Save agustinsivoplas/6f80233699aebebd512542503ea32682 to your computer and use it in GitHub Desktop.
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
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) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment