0% found this document useful (0 votes)
58 views7 pages

Software Applications For Mobile Devices: Lab Journal 7

This document is a lab journal describing how to create a customized listbox or spinner in an Android mobile application. It provides code to parse an XML file containing song data and populate a listview widget with the song titles, artists, durations and thumbnail images. The code retrieves the XML from a URL, parses it using a DOM parser to extract the song details into hashmaps, then passes this data to an adapter to display it in the listview. Clicking a list item will trigger an onItemClick listener. The tasks demonstrate parsing XML and populating a customized listview on Android.

Uploaded by

Mahnoor Razzaq
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)
58 views7 pages

Software Applications For Mobile Devices: Lab Journal 7

This document is a lab journal describing how to create a customized listbox or spinner in an Android mobile application. It provides code to parse an XML file containing song data and populate a listview widget with the song titles, artists, durations and thumbnail images. The code retrieves the XML from a URL, parses it using a DOM parser to extract the song details into hashmaps, then passes this data to an adapter to display it in the listview. Clicking a list item will trigger an onItemClick listener. The tasks demonstrate parsing XML and populating a customized listview on Android.

Uploaded by

Mahnoor Razzaq
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/ 7

Software Applications for

Mobile Devices
Lab Journal 7

Name: Mahnoor Abdul Razzaq


Enrollment No: 01-131182-041
Class: BSE-6A
Instructor: Sir Waleed

DEPARTMENT OF SOFTWARE ENGINEERING

BAHRIA UNIVERSITY
ISLAMABAD CAMPUS
Creating customize Listbox or Spinners

Introduction:
Android is a mobile operating system based on a modified version of the Linux kernel and
other open-source software, designed primarily for touchscreen mobile devices such
as smartphones and tablets. Android was developed by the Open Handset Alliance, led by
Google, and other companies. The source code for Android is available under free and open-
source software licenses. Google publishes most of the code under the Apache License version
2.0 and the rest, Linux kernel changes, under the GNU General Public License version 2.

Tools Used:

Android Studio
Lab Task 1:

Code :
Android_manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />

</LinearLayout>

List_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >

<!-- ListRow Left sied Thumbnail image -->


<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="@drawable/image_bg"
android:layout_marginRight="5dip">

<ImageView
android:id="@+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/rihanna"/>

</LinearLayout>

<!-- Title Of Song-->


<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:text="Rihanna Love the way lie"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>

<!-- Artist Name -->


<TextView
android:id="@+id/artist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:textColor="#343434"
android:textSize="10dip"
android:layout_marginTop="1dip"
android:layout_toRightOf="@+id/thumbnail"
android:text="Just gona stand there and ..." />

<!-- Rightend Duration -->


<TextView
android:id="@+id/duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/title"
android:gravity="right"
android:text="5:45"
android:layout_marginRight="5dip"
android:textSize="10dip"
android:textColor="#10bcc9"
android:textStyle="bold"/>

<!-- Rightend Arrow -->


<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/arrow"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>

</RelativeLayout>

Image_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:shape="rectangle">
<stroke android:width="1dp" android:color="#dbdbdc" />
<solid android:color="#FFFFFF" />
</shape>
</item>
</layer-list>

CustomizedListView.java:

package com.example.androidhive;

import java.util.ArrayList;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class CustomizedListView extends Activity {


// All static variables
static final String URL = "https://api.androidhive.info/music/music.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";

ListView list;
LazyAdapter adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ArrayList&lt;HashMap&lt;String, String&gt;&gt; songsList = new ArrayList&lt;HashMap

XMLParser parser = new XMLParser();


String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element

NodeList nl = doc.getElementsByTagName(KEY_SONG);
// looping through all song nodes &lt;song&gt;
for (int I = 0; I &lt; nl.getLength(); i++) {
// creating new HashMap
HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key =&gt; value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

// adding HashList to ArrayList


songsList.add(map);
}

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

// Getting adapter by passing xml data ArrayList


adapter=new LazyAdapter(this, songsList);
list.setAdapter(adapter);

// Click event for single list row


list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView&lt;?&gt; parent, View view,
int position, long id) {

}
});
}
}

Output:
Conclusion:

In this lab we have performed all the assigned tasks.

You might also like