Skip to content

Commit a8e588a

Browse files
authored
fix: open keyboard when tapping on style in note editor
- add UI utils function on Activity to open soft keyboard - use open soft keyboard utils function as needed Fixes 18615
1 parent f72e5fb commit a8e588a

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ import com.ichi2.libanki.Notetypes
159159
import com.ichi2.libanki.Notetypes.Companion.NOT_FOUND_NOTE_TYPE
160160
import com.ichi2.libanki.Utils
161161
import com.ichi2.themes.Themes
162+
import com.ichi2.utils.AndroidUiUtils.showSoftInput
162163
import com.ichi2.utils.ClipboardUtil
163164
import com.ichi2.utils.ClipboardUtil.MEDIA_MIME_TYPES
164165
import com.ichi2.utils.ClipboardUtil.hasMedia
@@ -2464,7 +2465,15 @@ class NoteEditor :
24642465
text = b.buttonText
24652466
}
24662467
val bmp = toolbar.createDrawableForString(text)
2467-
val v = toolbar.insertItem(0, bmp, b.toFormatter())
2468+
2469+
val v =
2470+
toolbar.insertItem(0, bmp) {
2471+
// Attempt to open keyboard for the currently focused view in the hosting Activity
2472+
val activity = context as? Activity
2473+
activity.showSoftInput()
2474+
2475+
toolbar.onFormat(b.toFormatter())
2476+
}
24682477
v.contentDescription = text
24692478

24702479
// Allow Ctrl + 1...Ctrl + 0 for item 10.

AnkiDroid/src/main/java/com/ichi2/anki/noteeditor/Toolbar.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import com.ichi2.anki.NoteEditor
5353
import com.ichi2.anki.R
5454
import com.ichi2.anki.preferences.sharedPrefs
5555
import com.ichi2.compat.CompatHelper
56+
import com.ichi2.utils.AndroidUiUtils.showSoftInput
5657
import com.ichi2.utils.ViewGroupUtils
5758
import com.ichi2.utils.ViewGroupUtils.getAllChildrenRecursive
5859
import com.ichi2.utils.dp
@@ -116,7 +117,13 @@ class Toolbar : FrameLayout {
116117
@IdRes id: Int,
117118
prefix: String,
118119
suffix: String,
119-
) = findViewById<View>(id).setOnClickListener { onFormat(TextWrapper(prefix, suffix)) }
120+
) = findViewById<View>(id).setOnClickListener {
121+
// Attempt to open keyboard for the currently focused view in the hosting Activity
122+
val activity = context as? Activity
123+
activity.showSoftInput()
124+
125+
onFormat(TextWrapper(prefix, suffix))
126+
}
120127

121128
setupButtonWrappingText(R.id.note_editor_toolbar_button_bold, "<b>", "</b>")
122129
setupButtonWrappingText(R.id.note_editor_toolbar_button_italic, "<i>", "</i>")

AnkiDroid/src/main/java/com/ichi2/utils/AndroidUiUtils.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,26 @@
1515
*/
1616
package com.ichi2.utils
1717

18+
import android.app.Activity
1819
import android.content.Context
1920
import android.view.View
2021
import android.view.Window
2122
import android.view.WindowManager
2223
import android.view.inputmethod.InputMethodManager
2324

2425
object AndroidUiUtils {
26+
/**
27+
* Shows the soft keyboard for the current focused view in the activity.
28+
*
29+
* It's a good practice to ensure that the `currentFocus` is an
30+
* appropriate input field (e.g., EditText) before calling this function
31+
*/
32+
fun Activity?.showSoftInput() {
33+
val currentFocus = this?.currentFocus ?: return
34+
val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
35+
imm.showSoftInput(currentFocus, InputMethodManager.SHOW_IMPLICIT)
36+
}
37+
2538
/**
2639
* This method is used for setting the focus on an EditText which is used in a dialog
2740
* and for opening the keyboard.

0 commit comments

Comments
 (0)