[go: up one dir, main page]

0% found this document useful (0 votes)
52 views6 pages

5-Android Lab 5

This lab manual document provides instructions on creating an Android application using a List View. It contains 3 tasks: 1. Write code for a basic List View application and display the output. Clicking an item displays a toast message with the selected item. 2. Modify the code to allow multiple list items to be selected and enable filtering. 3. Add a Strings.xml file to store the list items and modify the activity code to retrieve the items from the Strings.xml file.

Uploaded by

aws ajwa
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)
52 views6 pages

5-Android Lab 5

This lab manual document provides instructions on creating an Android application using a List View. It contains 3 tasks: 1. Write code for a basic List View application and display the output. Clicking an item displays a toast message with the selected item. 2. Modify the code to allow multiple list items to be selected and enable filtering. 3. Add a Strings.xml file to store the list items and modify the activity code to retrieve the items from the Strings.xml file.

Uploaded by

aws ajwa
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/ 6

Lab Manual

Lab (5): List View

Visual Programming for Smart Devices lab

1 Instructor: Oraib M Alrashdan


 Objectives:
create an android application using List View.

Task 1: Try IT An application using List View .java


Write this code then show the output.
package com.example.oraibpc.listview;

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

public class MainActivity extends ListActivity {


String []
array_technology={"Android","Java","PHP","Python","C++","C#","Ruby","JavaFX","V
B","Ajax",".Net","Rails","Perl","HTML"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//called when the activity is first created.
setListAdapter(new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,array_technology)
);

}
public void onListItemClick(ListView parent ,View v,int postion,long id)
{
Toast.makeText(this,"You have
selected"+array_technology[postion],Toast.LENGTH_SHORT).show();
}
}

Note: Click an item. A message containing the item selected is displayed.

2 Instructor: Oraib M Alrashdan


3 Instructor: Oraib M Alrashdan
Task 2: Try It who to allow multiple items in the List view to be selected
and how to enable filtering support.

package com.example.oraibpc.listview;

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

public class MainActivity extends ListActivity {


String []
array_technology={"Android","Java","PHP","Python","C++","C#","Ruby","JavaFX","V
B","Ajax",".Net","Rails","Perl","HTML"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listView = getListView();

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setTextFilterEnabled(true);
setListAdapter(new
ArrayAdapter<String>(this,android.R.layout.simple_list_item
_checked,array_technology));
}
public void onListItemClick(ListView parent ,View v,int postion,long id)
{
Toast.makeText(this,"You have selected
"+array_technology[postion],Toast.LENGTH_SHORT).show();
}
}

4 Instructor: Oraib M Alrashdan


5 Instructor: Oraib M Alrashdan
Task 3: using your application created earlier, add the following
Strings.xml file located in the res/values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello"> Hello World ,List View Activity ! </string>
<string name="app_name">List View </string>
<string-array name="array_technology">
<item>Android</item>
<item>Java</item>
<item>PHP</item>
<item>Python</item>
<item>C++</item>
<item>C#</item>
<item>Ruby</item>
<item>JavaFX</item>
<item>VB</item>
<item>Ajax</item>
<item>.Net</item>
<item>Rails</item>
<item>Perl</item>
<item>HTML</item>
</string-array>
</resources>

Modify the main Avtivity.java file as shown below:


package com.example.oraibpc.listview;

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

public class MainActivity extends ListActivity {


String [] array_technology;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView.setTextFilterEnabled(true);
array_technology =
getResources().getStringArray(R.array.array_technology);
setListAdapter(new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_check
ed,array_technology));

}
public void onListItemClick(ListView parent ,View v,int postion,long id)
{
Toast.makeText(this,"You have selected
"+array_technology[postion],Toast.LENGTH_SHORT).show();
}
}

6 Instructor: Oraib M Alrashdan

You might also like