Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added collapse keyboard function #215

Merged
merged 5 commits into from
Oct 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions app/src/main/java/ro/code4/monitorizarevot/helper/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.graphics.Rect
import android.graphics.Typeface
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
Expand All @@ -15,8 +16,10 @@ import android.provider.MediaStore
import android.text.*
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.MotionEvent
import android.view.View
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.annotation.IdRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
Expand Down Expand Up @@ -449,3 +452,34 @@ internal inline fun FirebaseRemoteConfig?.getStringOrDefault(key: String, defaul
this?.getString(key).takeUnless {
it == FirebaseRemoteConfig.DEFAULT_VALUE_FOR_STRING
} ?: defaultValue

/*
* Hide software keyboard if user taps outside the EditText
* use inside override fun dispatchTouchEvent()
*/
fun collapseKeyboardIfFocusOutsideEditText(
motionEvent: MotionEvent,
oldFocusedView: View,
newFocusedView: View
) {
if (motionEvent.action == MotionEvent.ACTION_UP) {
if (newFocusedView == oldFocusedView) {

val srcCoordinates = IntArray(2)
oldFocusedView.getLocationOnScreen(srcCoordinates)

val rect = Rect(srcCoordinates[0], srcCoordinates[1], srcCoordinates[0] +
oldFocusedView.width, srcCoordinates[1] + oldFocusedView.height)

if (rect.contains(motionEvent.x.toInt(), motionEvent.y.toInt()))
return
} else if (newFocusedView is EditText) {
// If new focus is other EditText then will not collapse
return
}

// Collapse the keyboard from activity
ContextCompat.getSystemService(newFocusedView.context, InputMethodManager::class.java)
?.hideSoftInputFromWindow(newFocusedView.windowToken, 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ import android.os.Bundle
import android.text.SpannableString
import android.text.method.LinkMovementMethod
import android.text.util.Linkify
import android.view.MotionEvent
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import com.google.android.material.snackbar.Snackbar
import com.google.firebase.messaging.RemoteMessage
import com.google.gson.Gson
import retrofit2.HttpException
import ro.code4.monitorizarevot.R
import ro.code4.monitorizarevot.helper.APIError400
import ro.code4.monitorizarevot.helper.LocaleManager
import ro.code4.monitorizarevot.helper.collapseKeyboardIfFocusOutsideEditText
import ro.code4.monitorizarevot.helper.fromJson
import ro.code4.monitorizarevot.helper.lifecycle.ActivityCallbacks
import ro.code4.monitorizarevot.interfaces.Layout
Expand Down Expand Up @@ -131,4 +132,14 @@ abstract class BaseActivity<out T : BaseViewModel> : AppCompatActivity(), Layout
.show()
}

// Collapse the keyboard when the user taps outside the EditText
override fun dispatchTouchEvent(motionEvent: MotionEvent): Boolean {

currentFocus?.let { oldFocus ->
super.dispatchTouchEvent(motionEvent)
val newFocus = currentFocus ?: oldFocus
collapseKeyboardIfFocusOutsideEditText(motionEvent, oldFocus, newFocus)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to return result from line 139 here instead of calling the super method again

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch! we'll have to fix this in a new PR

}
return super.dispatchTouchEvent(motionEvent)
}
}