mirror of
https://github.com/ev-map/EVMap.git
synced 2026-04-18 21:27:43 -04:00
Rework EditTextDialog
- use MaterialAlertDialog for modern styling - use proper way to move dialog above keyboard - fixes #212 (buttons were not clickable)
This commit is contained in:
@@ -86,7 +86,7 @@ class MapsActivity : AppCompatActivity(),
|
||||
ViewCompat.setOnApplyWindowInsetsListener(navView) { v, insets ->
|
||||
val header = navView.getHeaderView(0)
|
||||
header.setPadding(0, insets.getInsets(WindowInsetsCompat.Type.statusBars()).top, 0, 0)
|
||||
WindowInsetsCompat.CONSUMED
|
||||
insets
|
||||
}
|
||||
|
||||
prefs = PreferenceDataSource(this)
|
||||
|
||||
@@ -2,13 +2,13 @@ package net.vonforst.evmap.ui
|
||||
|
||||
import android.content.Context
|
||||
import android.content.DialogInterface
|
||||
import android.view.Gravity
|
||||
import android.view.View
|
||||
import android.view.WindowManager
|
||||
import android.view.inputmethod.EditorInfo
|
||||
import android.view.inputmethod.InputMethodManager
|
||||
import android.widget.EditText
|
||||
import android.widget.FrameLayout
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
|
||||
private fun dialogEditText(ctx: Context): Pair<View, EditText> {
|
||||
val container = FrameLayout(ctx)
|
||||
@@ -24,30 +24,19 @@ private fun dialogEditText(ctx: Context): Pair<View, EditText> {
|
||||
|
||||
fun showEditTextDialog(
|
||||
ctx: Context,
|
||||
customize: (AlertDialog.Builder, EditText) -> Unit
|
||||
customize: (MaterialAlertDialogBuilder, EditText) -> Unit
|
||||
): AlertDialog {
|
||||
val (container, input) = dialogEditText(ctx)
|
||||
val dialogBuilder = AlertDialog.Builder(ctx)
|
||||
val dialogBuilder = MaterialAlertDialogBuilder(ctx)
|
||||
.setView(container)
|
||||
|
||||
customize(dialogBuilder, input)
|
||||
|
||||
val dialog = dialogBuilder.show()
|
||||
|
||||
|
||||
// move dialog to top
|
||||
val attrs = dialog.window?.attributes?.apply {
|
||||
gravity = Gravity.TOP
|
||||
}
|
||||
dialog.window?.attributes = attrs
|
||||
dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
|
||||
|
||||
// focus and show keyboard
|
||||
input.requestFocus()
|
||||
input.postDelayed({
|
||||
val imm =
|
||||
ctx.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
||||
imm.showSoftInput(input, InputMethodManager.SHOW_IMPLICIT)
|
||||
}, 100)
|
||||
input.setOnEditorActionListener { _, actionId, _ ->
|
||||
if (actionId == EditorInfo.IME_ACTION_DONE) {
|
||||
val text = input.text
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
<item name="colorOnSecondaryContainer">@color/colorSecondaryDark</item>
|
||||
<item name="android:windowTranslucentStatus">true</item>
|
||||
<item name="preferenceTheme">@style/AppTheme.Preference</item>
|
||||
<item name="materialAlertDialogTheme">@style/AppTheme.AlertDialog</item>
|
||||
</style>
|
||||
|
||||
<style name="AppTheme.Preference" parent="@style/PreferenceThemeOverlay">
|
||||
@@ -67,4 +68,9 @@
|
||||
<item name="iconTint">?android:textColorSecondary</item>
|
||||
</style>
|
||||
|
||||
<style name="AppTheme.AlertDialog" parent="ThemeOverlay.Material3.MaterialAlertDialog">
|
||||
<!-- this is necessary to make sure the dialog gets "pushed up" when the keyboard appears -->
|
||||
<item name="android:windowTranslucentStatus">false</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user