Name:-Aman Verma MET – Institute of Computer Science
Roll No:1266
Experiment:-9
Q) Write a program to implement listview in android studio
[Link]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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>
[Link]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
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>
[Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
ListView lt;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
lt=findViewById([Link].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,[Link].list_items,[Link],Alphabets);
[Link](adapter);
[Link](new [Link]() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
[Link](getApplicationContext(), "You Have Selected :-
"+Alphabets[i], Toast.LENGTH_LONG).show();
}
Name:-Aman Verma MET – Institute of Computer Science
Roll No:1266
});
}
}
[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
xmlns:tools="[Link]
<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/[Link]"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
<meta-data
android:name="[Link].lib_name"
android:value="" />
</activity>
</application>
</manifest>
Name:-Aman Verma MET – Institute of Computer Science
Roll No:1266
Output:-