0% found this document useful (0 votes)
6 views4 pages

LAB 10 ListView

Uploaded by

mca231419ics
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)
6 views4 pages

LAB 10 ListView

Uploaded by

mca231419ics
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
You are on page 1/ 4

Name:-Aman Verma MET – Institute of Computer Science

Roll No:1266

Experiment:-9

Q) Write a program to implement listview in android studio

activityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/lst1"
android:divider="@color/black"
android:dividerHeight="2dp"
android:listSelector="#401FFFB1">
</ListView>

</RelativeLayout>

ListItems.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp"
Name:-Aman Verma MET – Institute of Computer Science
Roll No:1266

android:id="@+id/lvtxt"/>

</RelativeLayout>

MainActivity.java
package com.example.listview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

ListView lt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lt=findViewById(R.id.lst1);
String Alphabets[]
={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","
U","V","W","X","Y","Z"};
ArrayAdapter<String> adapter=new
ArrayAdapter<>(this,R.layout.list_items,R.id.lvtxt,Alphabets);
lt.setAdapter(adapter);

lt.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(), "You Have Selected :-
"+Alphabets[i], Toast.LENGTH_LONG).show();
}
Name:-Aman Verma MET – Institute of Computer Science
Roll No:1266

});

}
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ListView"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>

<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>

</manifest>
Name:-Aman Verma MET – Institute of Computer Science
Roll No:1266

Output:-

You might also like