0% found this document useful (0 votes)
18 views3 pages

Practical No 14

The document provides XML and Java code for an Android application that demonstrates the use of ScrollView, ImageView, ListView, and GridView. The XML layout defines a scrollable interface with sections for each view type, while the Java code initializes the ListView and GridView with sample data. This setup allows users to see different view components in a single scrollable layout.

Uploaded by

Vaishnavi Humane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Practical No 14

The document provides XML and Java code for an Android application that demonstrates the use of ScrollView, ImageView, ListView, and GridView. The XML layout defines a scrollable interface with sections for each view type, while the Java code initializes the ListView and GridView with sample data. This setup allows users to see different view components in a single scrollable layout.

Uploaded by

Vaishnavi Humane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical No 14

XML Code –
<?xml version="1.0" encoding="utf-8"?>
<!-- The outer ScrollView allows the entire content to be scrollable -->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- LinearLayout container for all views -->


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<!-- Section: ImageView Demo -->


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ImageView"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginBottom="8dp" />

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:src="@drawable/sample_image"
android:scaleType="centerCrop"
android:layout_marginBottom="16dp" />

<!-- Section: ListView Demo -->


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ListView"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginBottom="8dp" />

<!-- Fixed height for ListView inside ScrollView -->


<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:divider="@android:color/darker_gray"
android:dividerHeight="1dp"
android:layout_marginBottom="16dp" />

<!-- Section: GridView Demo -->


<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GridView"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginBottom="8dp" />
<!-- Fixed height for GridView inside ScrollView, set with 3
columns -->
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:numColumns="3"
android:verticalSpacing="8dp"
android:horizontalSpacing="8dp"
android:gravity="center"
android:layout_marginBottom="16dp" />

</LinearLayout>
</ScrollView>

JAVA Code –
package com.example.checkbox;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

// Sample data for ListView and GridView


private String[] sampleItems = {
"Item 1", "Item 2", "Item 3", "Item 4", "Item 5",
"Item 6", "Item 7", "Item 8", "Item 9", "Item 10"
};

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

// Optionally, obtain and manipulate the ImageView if needed


ImageView imageView = findViewById(R.id.imageView);
// imageView.setImageResource(R.drawable.sample_image); // Already
set in XML

// Set up the ListView with an ArrayAdapter


ListView listView = findViewById(R.id.listView);
ArrayAdapter<String> listAdapter = new ArrayAdapter<>(
this,
android.R.layout.simple_list_item_1,
sampleItems
);
listView.setAdapter(listAdapter);

// Set up the GridView with an ArrayAdapter


GridView gridView = findViewById(R.id.gridView);
ArrayAdapter<String> gridAdapter = new ArrayAdapter<>(
this,
android.R.layout.simple_list_item_1,
sampleItems
);
gridView.setAdapter(gridAdapter);
}
}

Output –

You might also like