0% found this document useful (0 votes)
87 views

ScrollView and ListView in Android

The document explains ScrollView and ListView, which are used for displaying scrollable content in Android. ScrollView allows scrolling of a single child view. ListView displays a list of items from an adapter. An example program demonstrates adding 10 string items to a ListView.

Uploaded by

pseuhoet65
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

ScrollView and ListView in Android

The document explains ScrollView and ListView, which are used for displaying scrollable content in Android. ScrollView allows scrolling of a single child view. ListView displays a list of items from an adapter. An example program demonstrates adding 10 string items to a ListView.

Uploaded by

pseuhoet65
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Describe Scroll View & List

View? Explain with suitable


Program. Write a java class to
add any 10 items within
Listview.
Frequency 3

In Android, ScrollView and ListView are two important components used for
displaying scrollable content. Here is a detailed explanation of each, along with a
simple example program demonstrating their usage.

ScrollView
ScrollView is a layout container that allows its child views to be scrolled vertically.
It is useful when you have a content layout that exceeds the size of the screen and
you want to enable vertical scrolling to view all the content.
Example: Using ScrollView

1. XML Layout ( activity_scroll_view.xml ):

<?xml version="1.0" encoding="utf-8"?>


<ScrollView xmlns:android="<http://schemas.android.com/apk/re
s/android>"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"

Describe Scroll View & List View? Explain with suitable Program. Write a java class to add any 10 items within Listview. 1
android:padding="16dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Item 1"
android:padding="8dp"
android:background="#FFC107"/>

<!-- Add more items to demonstrate scrolling -->


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Item 2"
android:padding="8dp"
android:background="#FF9800"/>

<!-- Repeat the TextView to create a long list -->


<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Item 3"
android:padding="8dp"
android:background="#FF5722"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Item 4"
android:padding="8dp"
android:background="#F44336"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Item 5"

Describe Scroll View & List View? Explain with suitable Program. Write a java class to add any 10 items within Listview. 2
android:padding="8dp"
android:background="#E91E63"/>

</LinearLayout>
</ScrollView>

1. Java Code ( ScrollViewActivity.java ):

package com.example.scrollviewexample;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class ScrollViewActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll_view);
}
}

ListView
ListViewis a view group that displays a list of scrollable items. Each item in the list
is automatically placed in an adapter-backed view. It is suitable for displaying a
large set of data, where each item in the list follows the same layout.

Example: Using ListView

1. XML Layout ( activity_list_view.xml ):

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="<http://schemas.android.com/apk/
res/android>"
android:layout_width="match_parent"
android:layout_height="match_parent"

Describe Scroll View & List View? Explain with suitable Program. Write a java class to add any 10 items within Listview. 3
android:orientation="vertical"
android:padding="16dp">

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

1. Java Code ( ListViewActivity.java ):

package com.example.listviewexample;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;

public class ListViewActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);

ListView listView = findViewById(R.id.listView);

// Sample data to display in the ListView


String[] items = {"Item 1", "Item 2", "Item 3", "Item
4", "Item 5"};

// Create an ArrayAdapter to bind the data to the Lis


tView
ArrayAdapter<String> adapter = new ArrayAdapter<>(thi
s,

Describe Scroll View & List View? Explain with suitable Program. Write a java class to add any 10 items within Listview. 4
android.R.layout.simple_list_item_1, items);

// Set the adapter to the ListView


listView.setAdapter(adapter);
}
}

Explanation
1. ScrollView:

The ScrollView contains a LinearLayout with several TextView elements.

When the content within the ScrollView exceeds the height of the screen,
the user can scroll vertically to view the rest of the content.

2. ListView:

The ListView is used to display a list of items.

An ArrayAdapter is used to bind an array of strings to the ListView .

The ListView automatically handles scrolling when the number of items


exceeds the height of the screen.

Both components are essential for handling large amounts of content in Android
applications, with ScrollView being more suitable for custom layouts with variable
content and ListView being ideal for uniform, repeated content.

Describe Scroll View & List View? Explain with suitable Program. Write a java class to add any 10 items within Listview. 5

You might also like