From bb23f998e08ca474aac7e04bd3c1b344922e5463 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Wed, 27 Jul 2022 11:39:20 +0300 Subject: [PATCH] Fix crash on description selection --- .../base/ui/widgets/SelectableTextView.kt | 29 +++++++++++++++++++ .../res/layout-w600dp/fragment_details.xml | 2 +- app/src/main/res/layout/fragment_details.xml | 2 +- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/org/koitharu/kotatsu/base/ui/widgets/SelectableTextView.kt diff --git a/app/src/main/java/org/koitharu/kotatsu/base/ui/widgets/SelectableTextView.kt b/app/src/main/java/org/koitharu/kotatsu/base/ui/widgets/SelectableTextView.kt new file mode 100644 index 000000000..e931853f0 --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/base/ui/widgets/SelectableTextView.kt @@ -0,0 +1,29 @@ +package org.koitharu.kotatsu.base.ui.widgets + +import android.content.Context +import android.text.Selection +import android.text.Spannable +import android.util.AttributeSet +import android.view.MotionEvent +import androidx.annotation.AttrRes +import com.google.android.material.textview.MaterialTextView + +class SelectableTextView @JvmOverloads constructor( + context: Context, + attrs: AttributeSet? = null, + @AttrRes defStyleAttr: Int = android.R.attr.textViewStyle, +) : MaterialTextView(context, attrs, defStyleAttr) { + + override fun dispatchTouchEvent(event: MotionEvent?): Boolean { + fixSelectionRange() + return super.dispatchTouchEvent(event) + } + + // https://stackoverflow.com/questions/22810147/error-when-selecting-text-from-textview-java-lang-indexoutofboundsexception-se + private fun fixSelectionRange() { + if (selectionStart < 0 || selectionEnd < 0) { + val spannableText = text as? Spannable ?: return + Selection.setSelection(spannableText, text.length) + } + } +} \ No newline at end of file diff --git a/app/src/main/res/layout-w600dp/fragment_details.xml b/app/src/main/res/layout-w600dp/fragment_details.xml index e45f03909..5775a9790 100644 --- a/app/src/main/res/layout-w600dp/fragment_details.xml +++ b/app/src/main/res/layout-w600dp/fragment_details.xml @@ -217,7 +217,7 @@ app:layout_constraintTop_toBottomOf="@id/recyclerView_bookmarks" tools:visibility="visible" /> - -