Skip to content

Instantly share code, notes, and snippets.

@shibbirweb
Last active February 28, 2021 16:35
Show Gist options
  • Select an option

  • Save shibbirweb/c2bbf95a0a3bc311e6bab1cd0c26fb35 to your computer and use it in GitHub Desktop.

Select an option

Save shibbirweb/c2bbf95a0a3bc311e6bab1cd0c26fb35 to your computer and use it in GitHub Desktop.
Auto hide keyboard click on outside of edit text
package me.shibbir.app
import android.content.Context
import android.view.MotionEvent
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity
open class BaseAppCompactActivity : AppCompatActivity() {
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
if (currentFocus != null) {
val imm: InputMethodManager =
getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(currentFocus!!.windowToken, 0)
}
return super.dispatchTouchEvent(ev)
}
}
// Usage
// Just extend BaseAppCompactActivity
class MainActivity : BaseAppCompactActivity() {
// ...your code starts here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment