This program is an Android application that demonstrates how to display a
list of items (in this case, country names) using a `ListView` in a simple user
interface. Here’s a step-by-step explanation:
Step 1: Create a New Project and Main Activity
- Project and Activity Setup**: The first step involves creating a new
Android project named `Listexample` and a main activity called
`MainActivity`.
- Creating `activity_main.xml`**: The main layout file (`activity_main.xml`
or `content_main.xml`) is defined to include a `LinearLayout` that contains a
`ListView`. This `ListView` will be used to display the list of items.
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/simpleListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="@color/material_blue_grey_800"
android:dividerHeight="1dp" />
</LinearLayout>
```
- `ListView`: The `ListView` with the ID `simpleListView` will display
the list of items. The `divider` and `dividerHeight` properties are used to
customize the appearance of the dividers between list items.
Step 2: Create a New Activity and Layout
-New Activity: A new activity named `Listview` is created.
- Creating `activity_listview.xml`: This layout defines how each item in the
list will appear. It uses a `LinearLayout` containing a `TextView`.
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="@dimen/activity_horizontal_margin"
android:textColor="@color/black"
android:textSize="25sp"/>
This `TextView` with the ID `textView` will display the name of each
country.
Step 3: Displaying the List Using `ArrayAdapter`
MainActivity.java`.-
package com.example.vahlistview;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
// Array of strings...
ListView simpleList;
String countryList[] = {"India", "China", "Australia", "Portugal",
"America",
"New Zealand"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpleList = (ListView)findViewById(R.id.simpleListView);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this, R.layout.activity_listview, R.id.textView, countryList);
simpleList.setAdapter(arrayAdapter);
}
}
```
`countryList[]: An array of country names that will be displayed in the
`ListView`.
ArrayAdapter<String>: This adapter binds the array of country names to the
`ListView`. It uses the layout defined in `activity_listview.xml` to render
each item.
simpleList.setAdapter(arrayAdapter): The adapter is set to the `ListView` to
populate it with data.
Additional Resources (`res/values/dimens.xml`)
- **Dimens Resource**: The `activity_horizontal_margin` dimension is
defined in the `dimens.xml` file.
```xml
<resources>
<dimen name="activity_horizontal_margin" />
</resources>
```
This file typically contains dimension values used throughout the app for
consistent padding, margins, text sizes, etc.
Summary
This Android application consists of a simple user interface with a `ListView`
that displays a list of countries using an `ArrayAdapter`. The `ListView` is
customized using XML layouts and Java code to populate it with data.