0% found this document useful (0 votes)
68 views8 pages

Practical:12: 2ceit509 Mobile Application Development

The document describes a mobile application project that aims to create a JSON URL for contact details including ID, name, phone number and address. It should have a minimum of 5 contact details displayed in a listview. The application includes layout files for the main activity and list items, Java classes for contacts, adapters and async tasks to retrieve contact data from an API URL and display in the listview. It also includes the Android manifest file.

Uploaded by

DIVYESH PATEL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views8 pages

Practical:12: 2ceit509 Mobile Application Development

The document describes a mobile application project that aims to create a JSON URL for contact details including ID, name, phone number and address. It should have a minimum of 5 contact details displayed in a listview. The application includes layout files for the main activity and list items, Java classes for contacts, adapters and async tasks to retrieve contact data from an API URL and display in the listview. It also includes the Android manifest file.

Uploaded by

DIVYESH PATEL
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

[ 2CEIT509 MOBILE APPLICATION DEVELOPMENT]

Practical:12

AIM- Create an application to create JSON URL for Contact which


have field(id, Name(First Name, Last Name), Phone No, Address)
and should be minimum five contact details & display received contact
data in listview..

Submitted By : Prince Kalariya


Enrollment number : 19012021017

Department of Computer
Engineering/Information Technology

19012021017 Prince Kalariya


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"

tools:context=".MainActivity"
android:orientation="vertical">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/titletxt"
android:layout_width="match_parent"
android:layout_height="69dp"
android:background="#000000"
android:gravity="center"
android:text="DETAILS"
android:textColor="#FFFFFF"
android:textSize="35sp"
android:textStyle="bold" />

</RelativeLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<ListView
android:id="@+id/listrc"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#D62727"
android:backgroundTint="#3D90B1"
tools:listitem="@layout/listitem">

</ListView>

</LinearLayout>

</LinearLayout>

19012021017 Prince Kalariya


MainActivity.kt
package com.example.practical_12_19012021017

import android.content.Intent
import android.os.AsyncTask
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ListView
import org.json.JSONArray
import java.net.URL

class MainActivity : AppCompatActivity() {


val listitems = Contact.contactArray as ArrayList<Contact>
val adapter = ContactAdapter(this, listitems)
lateinit var list: ListView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
supportActionBar?.hide()

list = findViewById(R.id.listrc)
val arr = listitems
list.setOnItemClickListener { parent, view, position, id ->
val id: String = arr[position]._id.toString()
val name: String = arr[position].name.toString()
val num: String = arr[position].phone.toString()
val addr: String = arr[position].address

}
contacts().execute()

inner class contacts() : AsyncTask<String, Void, String>() {


override fun onPreExecute() {
super.onPreExecute()

override fun doInBackground(vararg params: String?): String? {


var response: String?
try {
response =
URL(
"\n" +
"\n" +

"https://api.npoint.io/f900af304ae53adffa75"
).readText(
Charsets.UTF_8
)

} catch (e: Exception) {

19012021017 Prince Kalariya


response = null
}
return response
}

override fun onPostExecute(result: String?) {


super.onPostExecute(result)
try {

val jsonarr = JSONArray(result)

for (i in 0..7) {

val jsonObj_0 = jsonarr.getJSONObject(i)


val id_json = "Person ID : " +
jsonObj_0.getString("id")
val name_json = "Name : " + jsonObj_0.getString("Name")
val phone_json = "Phone No : " +
jsonObj_0.getString("Phone")
val address_json = "Address : " +
jsonObj_0.getString("Address")
listitems.add(Contact(id_json, name_json, phone_json,
address_json))
list.adapter = adapter
}

} catch (e: Exception) {

}
}
}

Listitem.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"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listindiv"
android:layout_marginTop="10dp"
>

<com.google.android.material.card.MaterialCardView
android:id="@+id/list_cardview"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"

19012021017 Prince Kalariya


android:layout_gravity="center"
android:layout_height="wrap_content"
android:backgroundTint="#ADD8E6"
app:cardCornerRadius="15dp"

>
<TextView
android:id="@+id/list_Id"
android:text="ID"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="15sp" >

</TextView>

<TextView
android:id="@+id/list_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="30dp"
android:text="ABCDEF"
android:textColor="@color/black"
android:textSize="15sp" />

<TextView
android:id="@+id/list_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginLeft="10dp"
android:text="Abraham Lincoln"
android:textColor="@color/black"
android:textSize="15sp" />

<TextView
android:id="@+id/list_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="15dp"
android:text="Abraham Lincoln"
android:textColor="@color/black"
android:textSize="15sp" />

</com.google.android.material.card.MaterialCardView>

</LinearLayout>
Contact.kt

19012021017 Prince Kalariya


package com.example.practical_12_19012021017

class Contact (var _id:String, var name:String, var phone:String,var


address:String) {

companion object{

var contactArray: List<Contact> = ArrayList()


}

ContctAdapter.kt
package com.example.practical_12_19012021017

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView

class ContactAdapter(private var context: Context, var


items:ArrayList<Contact>): BaseAdapter() {
override fun getCount(): Int {
return items.size
}

override fun getItem(position: Int): Any {


return items[position]
}

override fun getItemId(position: Int): Long {


return position.toLong()
}

override fun getView(position: Int, convertView: View?, parent:


ViewGroup): View {

val view = LayoutInflater.from(context).inflate(R.layout.listitem,


parent, false)

val id:TextView = view.findViewById(R.id.list_Id)


val name: TextView =view.findViewById(R.id.list_name)
val num: TextView =view.findViewById(R.id.list_phone)
val address: TextView = view.findViewById(R.id.list_address)

id.text=items[position]._id
name.text=items[position].name
num.text=items[position].phone
address.text=items[position].address

19012021017 Prince Kalariya


return view
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.practical_12_19012021017">
<uses-permission android:name="android.permission.INTERNET"></uses-
permission>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.practical_12_19012021017">
<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>
</activity>
</application>

</manifest>

String.xml
<resources>
<string name="app_name">practical_12_19012021017</string>
</resources>

Output :

19012021017 Prince Kalariya


19012021017 Prince Kalariya

You might also like