'Kotlin textview to scroll in both directions
I'm trying to setup a textView in Kotlin to scroll in both directions (ie horizontal and vertical).
That is, I want long lines to run off the right side, and user should be able to scroll/drag to the right to see. Additionally, when there's enough text to run off the bottom of the view, they should be able to scroll down as well. So basically a small "window viewer" if you will to a larger document.
(I'm doing this as the text displayed is a specific/strict format, and the "window" is limited in size).
I found a few questions talking about it, however, all suggestions I've found so far have not worked.
here's what I have so far:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginStart="105dp"
android:layout_marginTop="90dp"
android:scrollbars="horizontal|vertical"
android:scrollHorizontally="true"
android:text="This is line 1 and should scroll off the view to the right. \n \n This is line 2 and again should scroll off the view tot he right. \n \n This is line 3.\n and these \n lines should \n scroll off\n the bottom \n of the view\n when it\n gets long\n enough to \n pass the\n bottom of\n the view\n "
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.util.Log
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var txtVw = findViewById<TextView>(R.id.textView )
txtVw.movementMethod = ScrollingMovementMethod()
}
}
Is there an easy way to do this? or will I have to start playing with a Horizontal Scroll View and ScrollView ??
which ends up with thsi:
Scrolling up/down fine .. however, you see the first 2 lines wrap around. If I use "single line" property, of course the entire textView goes 1 line, so that doesn't help.
What am I missing? Read over the info on textView, but couldn't find anything useful on scrolling horizontally.
Solution 1:[1]
As Cheticamp mentioned, you should add a HorizontalScrollView
around your TextView
. If you want to do this programmatically you could do:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myHorizontalScrollView = HorizontalScrollView(this)
var txtVw = findViewById<TextView>(R.id.textView)
//since your textview already has a parent (the constraintlayout from the main_activity.xml file) you have to remove the widget from is parent first, before you add it in another layout.
(txtVw.parent as ConstraintLayout).removeView(txtVw)
myHorizontalScrollView.addView(txtVw)
// before doing this you must give an id to your ConstraintLayout
findViewById<ConstraintLayout>(R.id.myConstraint).addView(myHorizontalScrollView)
}
}
Or you could modify your activity_main.xml file to look like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginStart="105dp"
android:layout_marginTop="90dp"
android:scrollbars="horizontal|vertical"
android:scrollHorizontally="true"
android:text="This is line 1 and should scroll off the view to the
right. \n \n This is line 2 and again should scroll off the view
to the right. \n \n This is line 3.\n and these \n lines should \n
scroll off\n the bottom \n of the view\n when it\n gets long\n
enough to \n pass the\n bottom of\n the view\n "
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</HorizontalScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Panagiotis Giannoutsos |