ScrollView And Horizontal ScrollView Tutorial With Example In Android
In android ScrollView can hold only one direct child. This means that, if you have complex layout with more views(Buttons, TextViews or any other view) then you must enclose them inside another standard layout like Table Layout, Relative Layout or Linear Layout. You can specify layout_width and layout_height to adjust width and height of screen. You can specify height and width in dp(density pixel) or px(pixel). Then after enclosing them in a standard layout, enclose the whole layout in ScrollView to make all the element or views scrollable.
ScrollView in Android Studio Design: It is present inside Containers >> ScrollView or HorizontalScrollView

Important Note 2: In android default ScrollView is used to scroll the items in vertical direction and if you want to scroll the items horizontally then you need to implement horizontal ScrollView.
ScrollView Syntax:
<ScrollView android:id="@+id/scrollView" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- add child view’s here --> </ScrollView>
Here is how Scroll View looks in Android:

Table Of Contents [hide]
Horizontal ScrollView:
In android, You can scroll the elements or views in both vertical and horizontal directions. To scroll in Vertical we simply use ScrollView as we shown in the previous code of this article and to scroll in horizontal direction we need to use HorizontalScrollview.

<HorizontalScrollView android:id="@+id/horizontalscrollView" android:layout_width="fill_parent" android:layout_height="fill_parent"> <-- add child view’s here --> </HorizontalScrollView >
Attributes Of Scroll View:
ScrollView and HorizontalScrollView has same attributes, the only difference is scrollView scroll the child items in vertical direction while horizontal scroll view scroll the child items in horizontal direction.
Now let’s we discuss about the attributes that helps us to configure a ScrollView and its child controls. Some of the most important attributes you will use with ScrollView include:
1. id: In android, id attribute is used to uniquely identify a ScrollView.
Below is id attribute’s example code with explanation included.
<ScrollView android:id="@+id/scrollView" android:layout_width="fill_parent" android:layout_height="fill_parent" />
2. scrollbars: In android, scrollbars attribute is used to show the scrollbars in horizontal or vertical direction. The possible Value of scrollbars is vertical, horizontal or none. By default scrollbars is shown in vertical direction in scrollView and in horizontal direction in HorizontalScrollView.
Below is scrollbars attribute’s example code in which we set the scrollbars in vertical direction.
< HorizontalScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical"/><!--scrollbars in vertical direction-->
Example of ScrollView In Android Studio:
Example 1: In this example we will use 10 button and scroll them using ScrollView in vertical direction. Below is the code and final Output we will create:

Select File -> New -> New Project -> Android Application Project (or) Android Project. Fill the forms and click “Finish” button.
Step 2: Open res -> layout -> activity_main.xml (or) main.xml and add below code. Here we are creating a Relative Layout having 10 buttons which are nested in Linear Layout and then in ScrollView.
Important Note: Remember ScrollView can hold only one direct child. So we have to jointly put 10 buttons inside Linear Layout to make it one child. And then we put it inside ScrollView.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="vertical"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="20dp" android:orientation="vertical"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:background="#f00" android:text="Button 1" android:textColor="#fff" android:textSize="20sp" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:background="#0f0" android:text="Button 2" android:textColor="#fff" android:textSize="20sp" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:background="#00f" android:text="Button 3" android:textColor="#fff" android:textSize="20sp" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:background="#ff0" android:text="Button 4" android:textColor="#fff" android:textSize="20sp" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:background="#f0f" android:text="Button 5" android:textColor="#fff" android:textSize="20sp" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:background="#f90" android:text="Button 6" android:textColor="#fff" android:textSize="20sp" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:background="#f00" android:text="Button 7" android:textColor="#ff9" android:textSize="20sp" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:background="#444" android:text="Button 8" android:textColor="#fff" android:textSize="20sp" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:background="#ff002211" android:text="Button 9" android:textColor="#fff" android:textSize="20sp" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" android:background="#0f0" android:text="Button 10" android:textColor="#fff" android:textSize="20sp" /> </LinearLayout> </ScrollView> </RelativeLayout>
Step 3: Now Open src -> package -> MainActivity.java and paste the below code
package com.example.gourav.scrollviewExample; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Step 4: Now open manifest.xml and paste the below code
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.gourav.scrollviewExample" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Step 5: Lastly open res ->values -> strings.xml and paste the below code
<resources> <string name="app_name">ScrollViewExample</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> </resources>
Output:
Now run the App in Emulator / AVD or in real device. You will see the 10 buttons which can be scrollable in vertical direction.

Example 2: In this example we will scroll the buttons in horizontal direction. Below is the complete code and final output:

Select File -> New -> New Project and Fill the forms and click “Finish” button.
Step 2: Now open res -> layout -> activity_mail.xml (or) main.xml and add below code. Here we are creating same buttons in HorizontalScrollView.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <HorizontalScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" android:scrollbars="horizontal"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="20dp" android:background="#f00" android:padding="10dp" android:text="Button 1" android:textColor="#fff" android:textSize="20sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="20dp" android:background="#0f0" android:padding="10dp" android:text="Button 2" android:textColor="#fff" android:textSize="20sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="20dp" android:background="#00f" android:padding="10dp" android:text="Button 3" android:textColor="#fff" android:textSize="20sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="20dp" android:background="#ff0" android:padding="10dp" android:text="Button 4" android:textColor="#fff" android:textSize="20sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="20dp" android:background="#f0f" android:padding="10dp" android:text="Button 5" android:textColor="#fff" android:textSize="20sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="20dp" android:background="#f90" android:padding="10dp" android:text="Button 6" android:textColor="#fff" android:textSize="20sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="20dp" android:background="#f00" android:padding="10dp" android:text="Button 7" android:textColor="#ff9" android:textSize="20sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="20dp" android:background="#444" android:padding="10dp" android:text="Button 8" android:textColor="#fff" android:textSize="20sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="20dp" android:background="#ff002211" android:padding="10dp" android:text="Button 9" android:textColor="#fff" android:textSize="20sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="20dp" android:background="#0f0" android:padding="10dp" android:text="Button 10" android:textColor="#fff" android:textSize="20sp" /> </LinearLayout> </HorizontalScrollView> </RelativeLayout>
package com.example.gourav.horizontalscrollviewExample; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
Step 5: Now open AndroidManifest.xml inside manifests and paste the below code:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.gourav.horizontalscrollviewExample" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.gourav.horizontalscrollviewExample.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Step 6: Open res ->values -> strings.xml and paste the below code:
<resources> <string name="app_name">ScrollViewExample</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> </resources>
Output:
Now run the App in Emulator /AVD or real device and you will see the 10 buttons can now be scrollable in Horizontal direction.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.