Skip to content

fix: stabilise description dialog shaking#20376

Open
HS0204 wants to merge 1 commit intoankidroid:mainfrom
HS0204:fix/description-dialog
Open

fix: stabilise description dialog shaking#20376
HS0204 wants to merge 1 commit intoankidroid:mainfrom
HS0204:fix/description-dialog

Conversation

@HS0204
Copy link
Contributor

@HS0204 HS0204 commented Mar 2, 2026

Purpose / Description

When multiple lines are entered in edit deck description dialog, the dialog shakes.
This was caused by a conflict between the calculation performed by TextInputLayout and the changes to the height of TextInputEditText.
This PR stabilises the description dialog by resolving the conflicting frame calculations.

Fixes

Approach

  • To find the cause out, I looked at the dialog_deck_description.xml, NestedScrollView - TextInputLayout - TextInputEditText.
  • When I removed TextInputLayout, it was stable without shaking and I identified that main cause was from it.
  • Problem: TextInputLayout triggered multiple requestLayout() calls within the same frame as TextInputEditText height changes.
  • Solution: Post requestLayout() to the end of the message queue via post {}, ensuring it runs after TextInputLayout's internal layout pass completes.
  • I left the comment because the reason for the code seemed a bit vague if someone didn't know this situation.

How Has This Been Tested?

Manually tested on a device. Compared the behaviour before and after the fix.

  • Issue reproduction
WhatsApp.Video.2026-03-02.at.19.20.03.mp4
  • After the fix
WhatsApp.Video.2026-03-02.at.19.20.02.mp4

Learning (optional, can help others)

N/A

Checklist

Please, go through these checks before submitting the PR.

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

Copy link
Member

@david-allison david-allison left a comment

Choose a reason for hiding this comment

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

Feel free to play with this and make it shorter. Code is untested.

How to apply a patch: https://github.com/ankidroid/Anki-Android/wiki/Development-Guide/#applying-a-patch

Index: AnkiDroid/src/main/java/com/ichi2/anki/dialogs/EditDeckDescriptionDialog.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/EditDeckDescriptionDialog.kt b/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/EditDeckDescriptionDialog.kt
--- a/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/EditDeckDescriptionDialog.kt	(revision 3d9df21b057d59e111275a485de6ad6f1b1fc7f0)
+++ b/AnkiDroid/src/main/java/com/ichi2/anki/dialogs/EditDeckDescriptionDialog.kt	(date 1772581113685)
@@ -26,6 +26,7 @@
 import androidx.core.os.bundleOf
 import androidx.core.view.ViewCompat
 import androidx.core.view.WindowInsetsCompat
+import androidx.core.widget.doAfterTextChanged
 import androidx.core.widget.doOnTextChanged
 import androidx.fragment.app.DialogFragment
 import androidx.fragment.app.viewModels
@@ -134,31 +135,13 @@
             }
         }
 
-        binding.deckDescriptionInput.addTextChangedListener(
-            object : TextWatcher {
-                override fun beforeTextChanged(
-                    s: CharSequence?,
-                    start: Int,
-                    count: Int,
-                    after: Int,
-                ) {}
-
-                override fun onTextChanged(
-                    s: CharSequence?,
-                    start: Int,
-                    before: Int,
-                    count: Int,
-                ) {}
-
-                override fun afterTextChanged(s: Editable?) {
-                    // stabilise TextInputLayout by posting a deferred requestLayout()
-                    // after the current layout pass completes
-                    binding.deckDescriptionInput.post {
-                        (binding.deckDescriptionInput.parent as? View)?.requestLayout()
-                    }
-                }
-            },
-        )
+        with (binding.deckDescriptionInput) {
+            doAfterTextChanged {
+                // stabilise TextInputLayout by posting a deferred requestLayout()
+                // after the current layout pass completes
+                (this.parent as? View)?.post { requestLayout() }
+            }
+        }
 
         setupFlows()
     }

) {}

override fun afterTextChanged(s: Editable?) {
// stabilise TextInputLayout by posting a deferred requestLayout()
Copy link
Member

Choose a reason for hiding this comment

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

This mostly explains the 'what', could you have another pass at making this explain the 'why'. It doesn't need to be very verbose.

I feel the PR description is excellent, but the code/commit message doesn't explain it as well, and the code & commit message will last much longer than the PR

@david-allison david-allison added the Needs Author Reply Waiting for a reply from the original author label Mar 3, 2026
- TextInputLayout's layout recalculation causes view shaking on multiline input
- Defer a final requestLayout() until after the current layout pass completes
@HS0204 HS0204 force-pushed the fix/description-dialog branch from 3d9df21 to f05c242 Compare March 4, 2026 01:49
@HS0204
Copy link
Contributor Author

HS0204 commented Mar 4, 2026

@david-allison I've changed the code and the comments. I've also tested it locally on a device and it worked.
Could you review it again?

Thank you for the helpful advice on writing maintainable code! Please let me know if I've tweaked the comments too simply.

Copy link
Member

@david-allison david-allison left a comment

Choose a reason for hiding this comment

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

BEAUTIFUL!

@david-allison david-allison added Needs Second Approval Has one approval, one more approval to merge and removed Needs Author Reply Waiting for a reply from the original author Needs Review labels Mar 4, 2026
@david-allison david-allison added this to the 2.24 release milestone Mar 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Second Approval Has one approval, one more approval to merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deck description dialog shakes sometimes

2 participants