0% found this document useful (0 votes)
14 views5 pages

Practical 14.1&14.3

The document contains code snippets for two Android applications that display lists of programming languages. The first application uses a ListView to show a predefined array of languages, while the second application employs a GridView to present a larger array. Both applications include click listeners that display a Toast message with the selected language when an item is clicked.

Uploaded by

nihalpatil0021
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)
14 views5 pages

Practical 14.1&14.3

The document contains code snippets for two Android applications that display lists of programming languages. The first application uses a ListView to show a predefined array of languages, while the second application employs a GridView to present a larger array. Both applications include click listeners that display a Toast message with the selected language when an item is clicked.

Uploaded by

nihalpatil0021
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/ 5

Practical 14.

Activity_main.xml

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

<LinearLayout 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"

android:padding="20dp"

android:orientation="vertical"

tools:context=".MainActivity">

<ListView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/list"

android:dividerHeight="5dp"

android:divider="@color/black"/>

</LinearLayout>

List2.xml

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

<LinearLayout 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="100dp"

android:textSize="20dp"

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

</LinearLayout>

1
MainActivity.java

package com.example.list;

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.TextView;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

String s[]={"Android", "Java", "php", "Hadoop", "Sap", "Python", "Ajex", "C++", "Ruby", "Rails", "VB.Net"};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TextView t1=(TextView) findViewById(R.id.text);

ListView l1=(ListView) findViewById(R.id.list);

ArrayAdapter adapter=new ArrayAdapter<>(getApplicationContext(),R.layout.list2,R.id.text,s);

l1.setAdapter(adapter);

l1.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

String ss=(String)adapter.getItem(position);

Toast.makeText(MainActivity.this, "Selected: "+ss, Toast.LENGTH_LONG).show();

});

2
Output:

Practical no. 14.3

Activity_Main.xml

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

<LinearLayout 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"

android:padding="20dp"

android:orientation="vertical"

tools:context=".MainActivity">

<GridView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/list"

android:numColumns="3" />

</LinearLayout>

3
List2.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:padding="20dp"

android:layout_height="match_parent">

<TextView

android:layout_width="wrap_content"

android:layout_height="50dp"

android:textSize="20dp"

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

</LinearLayout>

mainActivity.java

package com.example.list;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.ArrayAdapter;

import android.widget.GridView;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

String s[]={"Android", "Java", "php", "Hadoop", "Sap", "Python", "Ajex","C","C#","HTML","CSS","C++", "Ruby",


"Rails", "VB.Net"};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TextView t1=(TextView) findViewById(R.id.text);

4
GridView l1=(GridView) findViewById(R.id.list);

ArrayAdapter adapter=new ArrayAdapter<>(getApplicationContext(),R.layout.list2,R.id.text,s);

l1.setAdapter(adapter);

l1.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

String ss = (String) adapter.getItem(position);

});

Output:

You might also like