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

Program 4

The document contains the source code for an Android application that displays a ListView with color options. It includes XML layout files for the main activity and individual list items, as well as a Java class that populates the ListView and handles item click events. When an item is clicked, a Toast message displays the position and text of the clicked item.

Uploaded by

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

Program 4

The document contains the source code for an Android application that displays a ListView with color options. It includes XML layout files for the main activity and individual list items, as well as a Java class that populates the ListView and handles item click events. When an item is clicked, a Toast message displays the position and text of the clicked item.

Uploaded by

Jana Jana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Source Code:

activity_main.xml:
<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"
android:gravity="clip_vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>

da_item.xml

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


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

package com.guru.listview;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
//Array of options-->ArrayAdapter----->ListView
//List View: {views: da_items.xml}
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateListView();
registerClickCallback();
}
private void populateListView() {
//Create list of items
String[] myItems = {"Blue","Green","Purple","Red"};

//Build Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, //Context for the activity
R.layout.da_item, //Layout to use(Create)
myItems); //Items to be displayed
//Configure the list view
ListView list= (ListView) findViewById(R.id.listView1);
list.setAdapter(adapter);
}
private void registerClickCallback() {
// TODO Auto-generated method stub
ListView list= (ListView) findViewById(R.id.listView1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> paret, View viewClicked, int position,
long id) {
TextView textView = (TextView) viewClicked;
String message = "You Clicked #" + position + " which is string: "
+textView.getText().toString();
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();

}
});
}
}
OUTPUT

You might also like