ScrollView and ListView in Android
ScrollView and ListView in Android
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
<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"/>
<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>
package com.example.scrollviewexample;
import android.os.Bundle;
import androidx.appcompat.app.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.
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>
package com.example.listviewexample;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
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);
Explanation
1. ScrollView:
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:
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