In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it. A ScrollView supports Vertical scrolling only, so in order to create a horizontally scrollable view, HorizontalScrollView is used.
Here is example :
Click on File, then New => New Project.
Choose “Empty Activity” for the project template.
Select language as Kotlin.
Select the minimum SDK as per your need.
Add some strings inside the strings.xml file to display those strings in the app.
<resources>
<string name="app_name">gfgapp_scrollview</string>
<string name="scrolltext">Kotlin is a statically typed,
general-purpose programming language developed
by JetBrains, that has built world-class IDEs
like IntelliJ IDEA, PhpStorm, Appcode, etc.
It was first introduced by JetBrains in 2011
and a new language for the JVM. Kotlin is
object-oriented language, and a “better language”
than Java, but still be fully interoperable
with Java code. Kotlin is sponsored by Google,
announced as one of the official languages for
Android Development in 2017.
Advantages of Kotlin language:
Easy to learn – Basic is almost similar to java.
If anybody worked in java then easily understand
in no time. Kotlin is multi-platform – Kotlin is
supported by all IDEs of java so you can write
your program and execute them on any machine
which supports JVM. It’s much safer than Java.
</string>
</resources>
Add the ScrollView and inside the ScrollView add a TextView to display the strings that are taken in the strings.xml file.
<?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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="-127dp">
<TextView
android:id="@+id/scrolltext"
style="@style/AppTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/scrolltext"
android:textColor="@color/green"/>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
As already mentioned above, Scrollview can only contain one direct child. In this case, the child is textview. On noticing this textview you will realize that the text added inside textview is mentioned as @string/scrolltext which refers to a string resource inside the strings.xml file.
import android.os.Bundle;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}