From d5690081ccff508aa9b50abe2681b46af0fbc172 Mon Sep 17 00:00:00 2001 From: cdhiraj40 Date: Wed, 6 Oct 2021 22:22:43 +0530 Subject: [PATCH] closing Keyboard when clicked in the background activity --- .../activities/base/AbsBaseActivity.kt | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsBaseActivity.kt b/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsBaseActivity.kt index acec7d62..3b929728 100644 --- a/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsBaseActivity.kt +++ b/app/src/main/java/code/name/monkey/retromusic/activities/base/AbsBaseActivity.kt @@ -17,14 +17,19 @@ package code.name.monkey.retromusic.activities.base import android.Manifest import android.content.Intent import android.content.pm.PackageManager +import android.graphics.Rect import android.media.AudioManager import android.net.Uri import android.os.Build import android.os.Bundle import android.provider.Settings import android.view.KeyEvent +import android.view.MotionEvent import android.view.View +import android.view.inputmethod.InputMethodManager +import android.widget.EditText import androidx.core.app.ActivityCompat +import androidx.core.content.getSystemService import code.name.monkey.appthemehelper.ThemeStore import code.name.monkey.retromusic.R import com.google.android.material.snackbar.Snackbar @@ -158,4 +163,19 @@ abstract class AbsBaseActivity : AbsThemeActivity() { companion object { const val PERMISSION_REQUEST = 100 } + // this lets keyboard close when clicked in backgroud + override fun dispatchTouchEvent(event: MotionEvent): Boolean { + if (event.action == MotionEvent.ACTION_DOWN) { + val v = currentFocus + if (v is EditText) { + val outRect = Rect() + v.getGlobalVisibleRect(outRect) + if (!outRect.contains(event.rawX.toInt(), event.rawY.toInt())) { + v.clearFocus() + val imm = getSystemService()?.hideSoftInputFromWindow(v.windowToken, 0) + } + } + } + return super.dispatchTouchEvent(event) + } }