Multiple Views Inside a ScrollView in Android
Last Updated :
25 Aug, 2022
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. In this article, we are going to learn how to add multiple views inside a ScrollView.
Note: This Android article covered in both Java and Kotlin languages.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio.
Step 2: Working with the activity_main.xml file
Navigate to app > res > layout > activity_main.xml and add the below code to it. Comments are added in the code to get to know in detail.
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">
<!-- using ScrollView -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- using Linear Layout To add
multiple views inside it -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:textSize="30dp"
android:textColor="@color/purple_700"
android:textAlignment="center"
android:backgroundTint="@color/white"
android:text="GeeksforGeeks"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="150dp"
android:textSize="30dp"
android:textColor="@color/purple_700"
android:textAlignment="center"
android:text="GeeksforGeeks"
android:backgroundTint="@color/white"
android:layout_marginTop="10dp"/>
<Button
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="150dp"
android:textSize="30dp"
android:layout_marginTop="10dp"
android:textColor="@color/purple_700"
android:textAlignment="center"
android:backgroundTint="@color/white"
android:text="GeeksforGeeks" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="150dp"
android:textSize="60dp"
android:textColor="@color/purple_700"
android:textAlignment="center"
android:layout_marginTop="10dp"
android:padding="3dp"
android:background="#8C8A8A"
android:text="GeeksforGeeks"/>
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="150dp"
android:textSize="60dp"
android:textColor="@color/purple_700"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:padding="3dp"
android:background="#8C8A8A"
android:text="GeeksforGeeks"/>
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="150dp"
android:textSize="60dp"
android:textColor="@color/purple_700"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:padding="3dp"
android:background="#8C8A8A"
android:text="GeeksforGeeks"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Step 3: Working with the MainActivity file
Navigate to app > java > your app's package name > MainActivity file and add the below code to it. Comments are added in the code to get to know in detail.
Java
package com.example.gfgvalidator;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// for changing the background color of title bar
ActionBar aBar = getSupportActionBar();
ColorDrawable cd = new ColorDrawable(Color.parseColor("#FF00FF00"));
if (aBar != null) {
aBar.setBackgroundDrawable(cd);
}
}
}
Kotlin
package com.example.gfgvalidator;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// for changing the background color of title bar
val aBar = supportActionBar
val cd = ColorDrawable(Color.parseColor("#FF00FF00"))
aBar?.setBackgroundDrawable(cd)
}
}
Output:
Similar Reads
Android Architecture Android architecture contains a different number of components to support any Android device's needs. Android software contains an open-source Linux Kernel having a collection of a number of C/C++ libraries which are exposed through application framework services. Among all the components Linux Kern
5 min read
Android Tutorial In this Android Tutorial, we cover both basic and advanced concepts. So whether you are a fresher (graduate) or an experienced candidate with several years of Android Development experience, you can follow this Android tutorial to kick-start your journey in Android app development. Our Android Tutor
15+ min read
Activity Lifecycle in Android with Demo App In Android, an activity is referred to as one screen in an application. It is very similar to a single window of any desktop application. An Android app consists of one or more screens or activities. Each activity goes through various stages or a lifecycle and is managed by activity stacks. So when
9 min read
Introduction to Android Development Android operating system is the largest installed base among various mobile platforms across the globe. Hundreds of millions of mobile devices are powered by Android in more than 190 countries of the world. It conquered around 71% of the global market share by the end of 2021, and this trend is grow
5 min read
Top 50 Android Interview Questions and Answers - SDE I to SDE III A Linux-based open-source OS, Android was created by Andy Rubin and became one of the most popular smartphone operating systems. With 71 percent of the market share worldwide, Android is on top. Because it is on top in the smartphone OS, Android development is always in demand.If you are seeking a j
15+ min read
Android UI Layouts Layouts in Android define the user interface and hold UI controls or widgets that appear on the screen of an application. Every Android application consists of View and ViewGroup elements. Since an application contains multiple activitiesâeach representing a separate screenâevery activity has multip
5 min read
Android Studio Tutorial It is stated that "If you give me six hours to chop down a tree then I will spend the first four hours in sharpening the axe". So in the Android Development World if we consider Android Development as the tree then Android Studio should be the axe. Yes, if you are starting Android Development then y
9 min read
Components of an Android Application There are some necessary building blocks that an Android application consists of. These loosely coupled components are bound by the application manifest file which contains the description of each component and how they interact. The manifest file also contains the appâs metadata, its hardware confi
3 min read
MVVM (Model View ViewModel) Architecture Pattern in Android Developers always prefer clean and structured code for projects. Organizing the codes according to a design pattern helps in the maintenance of the software. By having knowledge of all crucial logic parts of the android application, it is easier to add and remove app features. Further, design patter
8 min read
Kotlin Android Tutorial Kotlin is a cross-platform programming language that may be used as an alternative to Java for Android App Development. Kotlin is an easy language so that you can create powerful applications immediately. Kotlin is much simpler for beginners to try as compared to Java, and this Kotlin Android Tutori
6 min read