I want to achieve writing and editing long text like articles in a text box, and the whole page is scrollable. As a result, I use TextInputEditText which has a beautiful style and can scroll up down in the text box, and use ScrollView as the root of layout.
It's OK when text is short, when I can normally click text, cursor acquires focus, soft keyboard jumps out, and I start editing. However, when text is long enough to exceed the screen height, after I click text at the top, the screen suddenly scrolls to the end of TextInputEditText and loses cursor focus. Although the keyboard jumps out as usual, I can't see the cursor and its context, so that I can't normally edit. At this time I type words, they don't appear on the screen, but when I scroll up, the words appear at the position of cursor. It was like this:
Another problem is that when the screen scrolls to the end of TextInputEditText, if I drag the cursor to move up, it doesn't take effect.
I have minimize the scale of codes. The activity_main.xml and MainActivity.kt are as follows:
activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/photo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="photo"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/content_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="content"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/photo_text" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:hint="content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/content_text"
app:layout_constraintTop_toTopOf="@+id/content_text">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/content_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/article"
/>
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val content_edit: TextInputEditText = findViewById(R.id.content_edit)
val constraint: ConstraintLayout = findViewById(R.id.constraintLayout)
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
content_edit.setOnClickListener {
it.requestFocus()
imm.showSoftInput(it, 0)
}
constraint.setOnClickListener {
content_edit.clearFocus()
imm.hideSoftInputFromWindow(it.windowToken, 0)
}
}
}
I searched online about same question, but it's hard to search the same incident. In fact, sometimes I also encounter similar incident when using apps. Maybe my English is not so good, as it's not my native language, so it would be nice if typos or grammatical errors are pointed out in time.
Thanks for your help!