Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.cssnr.remotewallpaper.ui.dialogs

import android.app.Dialog
import android.os.Build
import android.view.Window
import android.view.WindowInsets
import android.view.WindowManager

/**
* Shows the soft keyboard for this dialog window.
*
* Copied from androidx.preference PreferenceDialogFragmentCompat.requestInputMethod()
* which is how EditTextPreference dialogs shows the keyboard when a dialog is shown.
*
* https://github.com/androidx/androidx/blob/androidx-main/preference/preference/src/main/java/androidx/preference/PreferenceDialogFragmentCompat.java
*
* AI NOTE: Call AFTER create() and BEFORE show() (like the library calls requestInputMethod
* in onCreateDialog). The focused editor and window flags must be in place before the
* dialog window gains focus or the keyboard will not show reliably.
*/
fun Dialog.showKeyboard() {
val window: Window = window ?: return
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// Same as androidx.preference Api30Impl.showIme(window)
window.decorView.windowInsetsController?.show(WindowInsets.Type.ime())
} else {
// NOTE: or SOFT_INPUT_ADJUST_PAN to prevent dialog shrinking
window.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE or
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import org.cssnr.remotewallpaper.db.HistoryDatabase
import org.cssnr.remotewallpaper.db.HistoryItem
import org.cssnr.remotewallpaper.db.Remote
import org.cssnr.remotewallpaper.db.RemoteDatabase
import org.cssnr.remotewallpaper.ui.dialogs.showKeyboard
import java.io.File
import java.io.FileOutputStream
import java.time.ZonedDateTime
Expand Down Expand Up @@ -180,7 +181,6 @@ fun Context.showAddDialog() {
.create()

dialog.setOnShowListener {
input.requestFocus()
val sendButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
sendButton.setOnClickListener {
sendButton.isEnabled = false
Expand All @@ -206,6 +206,9 @@ fun Context.showAddDialog() {
}

dialog.setButton(AlertDialog.BUTTON_POSITIVE, "Set Image") { _, _ -> }

dialog.showKeyboard()
input.requestFocus()
dialog.show()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.cssnr.remotewallpaper.R
import org.cssnr.remotewallpaper.databinding.FragmentRemotesBinding
import org.cssnr.remotewallpaper.db.Remote
import org.cssnr.remotewallpaper.db.RemoteDatabase
import org.cssnr.remotewallpaper.ui.dialogs.showKeyboard

const val LOG_TAG = "Remotes"

Expand Down Expand Up @@ -172,7 +173,6 @@ class RemotesFragment : Fragment() {
.create()

dialog.setOnShowListener {
input.requestFocus()
val sendButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
sendButton.setOnClickListener {
sendButton.isEnabled = false
Expand Down Expand Up @@ -208,6 +208,9 @@ class RemotesFragment : Fragment() {
}

dialog.setButton(AlertDialog.BUTTON_POSITIVE, "Add") { _, _ -> }

dialog.showKeyboard()
input.requestFocus()
dialog.show()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.cssnr.remotewallpaper.R
import org.cssnr.remotewallpaper.api.FeedbackApi
import org.cssnr.remotewallpaper.ui.dialogs.showKeyboard
import org.cssnr.remotewallpaper.work.enqueueWorkRequest

class SettingsFragment : PreferenceFragmentCompat() {
Expand Down Expand Up @@ -212,8 +213,10 @@ class SettingsFragment : PreferenceFragmentCompat() {
input.error = "Feedback is Required"
}
}
input.requestFocus()
}

dialog.showKeyboard()
input.requestFocus()
dialog.show()
}

Expand Down
Loading