Ese Android U2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

NAME: -DHRUVNARAYAN RAWAL

SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
ASSIGNMENT-I
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
ASSIGNMENT-II

1>Write a Kotlin program in Android to display a welcome message to the user on a click of a Button
using Toast

XML FILE
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="156dp"
android:layout_marginTop="288dp"
android:text="Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

KT FILE
package com.example.prg1

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import
android.widget.Button import
android.widget.Toast

class MainActivity : AppCompatActivity() { override


fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn = findViewById<Button>(R.id.button) as
Button
btn.setOnClickListener
{
Toast.makeText(this,"hello world",Toast.LENGTH_LONG).show()
}

}
}

OUTPUT
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI

3> Write a Kotlin program in Android to accept a text from the user using Edit Text control and
provide 2 buttons to the user “Bold” and “Italic”. Display the user text in a Text View Control after
formatting it.

XML FILE:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">

<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="219dp"
android:layout_height="79dp"
android:layout_marginStart="108dp"
android:layout_marginTop="196dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Dhruvnarayan Rawal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="192dp"
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
android:layout_marginTop="376dp"
android:text="DHRUV"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="88dp"
android:layout_marginTop="500dp"
android:text="BOLT"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="500dp"
android:layout_marginEnd="88dp"
android:text="ITALIC"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

KT FILE:
package com.example.prg3
import
android.annotation.SuppressLint import
android.graphics.Typeface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import
android.widget.EditText import
android.widget.Button import
android.widget.TextView
class MainActivity : AppCompatActivity()
{ @SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val input = findViewById<EditText>(R.id.editTextTextPersonName)
val output = findViewById<TextView>(R.id.textView) val btnB =
findViewById<Button>(R.id.button) val btnI =
findViewById<Button>(R.id.button1) btnB.setOnClickListener {
var x = input.text.toString() output.text = x
output.setTypeface(null,Typeface.BOLD)
}
btnI.setOnClickListener {
var x = input.text.toString()
output.text = x
output.setTypeface(null,Typeface.ITALIC)
}
}
}
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI

OUTPUT

4> Write a Kotlin program in Android to demonstrate the use of Floating Label.

XML FILE:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">

<com.google.android.material.textfield.TextInputLayout
android:layout_width="409dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" android:hint="Enter
your name" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

KT FILE
package com.example.prg4

import androidx.appcompat.app.AppCompatActivity import


android.os.Bundle
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI

class MainActivity : AppCompatActivity() { override


fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
OUTPUT

6> Write a Kotlin program in Android to demonstrate the use of background


property of Button Control as well as Image Button.

XML FILE

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


<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" tools:context=".MainActivity">

<EditText

android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="70dp" android:hint="...Enter
Your Name..." android:inputType="text" />

<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="70dp"
android:hint="...Email..."
android:inputType="textEmailAddress"/>

<EditText
android:id="@+id/enroll"
android:layout_width="match_parent"
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
android:layout_height="70dp"
android:hint="...Enrollment No... " />

<EditText
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="70dp"
android:hint="...Mobile Number..."
android:inputType="number"/>
<Button
android:id="@+id/register"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="Register" app:iconTint="#FFFFFF"
/>

<Button
android:id="@+id/reset"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="Reset" />

<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="185dp"
android:foregroundTint="#37E1D1"
android:hapticFeedbackEnabled="true"
android:textSize="24sp" />

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="172dp"
app:srcCompat="@android:drawable/sym_def_app_icon" />

</LinearLayout>

KT FILE
package com.example.prg6

import androidx.appcompat.app.AppCompatActivity import


android.os.Bundle

class MainActivity : AppCompatActivity() { override


fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
OUTPUT

7>Write a Kotlin program in Android to design a Student Registration Form.


Provide appropriate fields and 2 buttons “Submit” and “Reset”. Implement
the appropriate functionality of the Button.

XML FILE

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


<androidx.constraintlayout.widget.ConstraintLayout
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">

<TextView
android:id="@+id/textView3"
android:layout_width="61dp"
android:layout_height="35dp"
android:layout_marginStart="32dp"
android:layout_marginTop="244dp"
android:layout_marginEnd="22dp"
android:layout_marginBottom="452dp"
android:text="Mobile no"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/editTextTextPersonName3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
android:id="@+id/textView2"
android:layout_width="52dp"
android:layout_height="27dp"
android:layout_marginStart="32dp"
android:layout_marginTop="148dp"
android:layout_marginEnd="31dp"
android:layout_marginBottom="69dp"
android:text="Email"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toStartOf="@+id/editTextTextPersonName2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="216dp"
android:layout_height="54dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="80dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Enter your name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="52dp"
android:layout_height="27dp"
android:layout_marginTop="56dp"
android:text="Name"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toStartOf="@+id/editTextTextPersonName"
app:layout_constraintHorizontal_bias="0.545"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="@+id/editTextTextPersonName2"
android:layout_width="216dp"
android:layout_height="60dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="80dp"
android:ems="10"
android:inputType="textPersonName" android:text="Enter
your email id" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName" />

<EditText
android:id="@+id/editTextTextPersonName3"
android:layout_width="230dp"
android:layout_height="53dp"
android:layout_marginTop="48dp" android:ems="10"
android:inputType="textPersonName"
android:text="Enter your phone no"
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.25"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName2"
/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="88dp"
android:text="Submit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonName3"
/>

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="76dp"
android:text="Reset"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</androidx.constraintlayout.widget.ConstraintLayout>
KT FILE
package com.example.prg7
import
androidx.appcompat.app.AppCompatActivity import
android.os.Bundle
import android.text.InputFilter.LengthFilter
import android.widget.Button import
android.widget.EditText import
android.widget.Toast

class MainActivity : AppCompatActivity() { override


fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var name= findViewById<EditText>(R.id.editTextTextPersonName)
var email=findViewById<EditText>(R.id.editTextTextPersonName2)
var mo= findViewById<EditText>(R.id.editTextTextPersonName3)
var submit=findViewById<Button>(R.id.button) var
reset=findViewById<Button>(R.id.button2)
submit.setOnClickListener{
Toast.makeText(this@MainActivity,"record insert succesfully",
Toast.LENGTH_LONG).show()} reset.setOnClickListener {
name.setText("") email.setText("") mo.setText("")
}

} }
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
OUTPUT

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

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</LinearLayout>

KT FILE
package com.example.program8_u2

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Define the list of elements


val elements = arrayOf("Element 1", "Element 2", "Element 3",
"Element 4", "Element 5",
"Element 6", "Element 7", "Element 8", "Element 9", "Element
10", "Element 11",
"Element 12")

// Get the ListView widget from the XML layout file


val listView = findViewById<ListView>(R.id.listView)

// Create an ArrayAdapter to convert the array of elements into


views
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
val adapter = ArrayAdapter(this,
android.R.layout.simple_list_item_1, elements)

// Set the adapter for the ListView


listView.adapter = adapter
}
}

ANDROID MANIFEST.XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<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:supportsRtl="true"
android:theme="@style/Theme.Program8_U2"
tools:targetApi="31">
<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>
OUTPUT
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
Pr9>Write a Kotlin program in Android to implement the GridView Widget component. Display the
android versions images in a grid format.
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="120dp"
android:numColumns="auto_fit"
android:verticalSpacing="16dp"
android:horizontalSpacing="16dp"
android:padding="16dp"
android:gravity="center"
android:stretchMode="columnWidth"/>

KT FILE
package com.example.program9_u2

import ImageAdapter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.AdapterView
import android.widget.GridView
import android.widget.Toast

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Get the GridView widget from the XML layout file


val gridView = findViewById<GridView>(R.id.gridView)

// Define the images for each Android version


val images = arrayOf(R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground, R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground, R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground, R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground, R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground,
R.drawable.ic_launcher_foreground)

// Create an ImageAdapter to convert the array of images into views


val adapter = ImageAdapter(this, images)

// Set the adapter for the GridView


gridView.adapter = adapter
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
// Set an OnItemClickListener to handle clicks on the images
gridView.onItemClickListener = AdapterView.OnItemClickListener {
parent, view, position, id ->
// Get the selected image
val image = parent.getItemAtPosition(position) as Int

// Display a Toast message with the name of the Android version


val versionName = "Android " +
resources.getResourceEntryName(image).substring(7)
Toast.makeText(this, versionName, Toast.LENGTH_SHORT).show()
}
}
}

IMAGE ADAPTER.KT
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView

class ImageAdapter(private val context: Context, private val images:


Array<Int>) : BaseAdapter() {

override fun getCount(): Int {


return images.size
}

override fun getItem(position: Int): Any {


return images[position]
}

override fun getItemId(position: Int): Long {


return 0
}

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


ViewGroup?): View {
val imageView = ImageView(context)
imageView.setImageResource(images[position])
imageView.scaleType = ImageView.ScaleType.CENTER_CROP
imageView.layoutParams = ViewGroup.LayoutParams(350, 350)
return imageView
}
}
OUTPUT
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI

Pr11> Write a Kotlin program in Android to create a Login application. Provide 2 fields and 2 buttons
“Login” and “Clear”. Check whether the UserId and Password are Correct or not. If correct, redirect
the user to a Welcome page and also pass the username to be displayed on the Welcome Page. If
incorrect, inform the user through Notification.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">

<EditText
android:id="@+id/userIdEditText"
android:hint="User ID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"/>

<EditText
android:id="@+id/passwordEditText"
android:hint="Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"/>

<Button
android:id="@+id/loginButton"
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/>

<Button
android:id="@+id/clearButton"
android:text="Clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/>
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI

</LinearLayout>

KT FILE
package com.example.program11_u2

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast

class MainActivity : AppCompatActivity() {

private lateinit var userIdEditText: EditText


private lateinit var passwordEditText: EditText

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Get the EditText fields and buttons from the XML layout file
userIdEditText = findViewById(R.id.userIdEditText)
passwordEditText = findViewById(R.id.passwordEditText)
val loginButton = findViewById<Button>(R.id.loginButton)
val clearButton = findViewById<Button>(R.id.clearButton)

// Set a click listener for the Login button


loginButton.setOnClickListener {
// Check if the user ID and password are correct
if (userIdEditText.text.toString() == "admin" &&
passwordEditText.text.toString() == "password") {
// Redirect the user to the welcome page and pass the
username
val intent = Intent(this, WelcomeActivity::class.java)
intent.putExtra("username", userIdEditText.text.toString())
startActivity(intent)
} else {
// Inform the user through a notification
Toast.makeText(this, "Incorrect user ID or password",
Toast.LENGTH_SHORT).show()
}
}

// Set a click listener for the Clear button


clearButton.setOnClickListener {
// Clear the EditText fields
userIdEditText.text.clear()
passwordEditText.text.clear()
}
}
}

annotation class WelcomeActivity


NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
OUTPUT
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
Pr13>Write a Kotlin program in Android to implement JSON Parser.

Code:-

Data index. Html :-

{"info":[

{"name":"Ajay","id":"111","email":"[email protected]"},

{"name":"John","id":"112","email":"[email protected]"},

{"name":"Rahul","id":"113","email":"[email protected]"},

{"name":"Maich","id":"114","email":"[email protected]"},

{"name":"Vikash","id":"115","email":"[email protected]"},

{"name":"Mayank","id":"116","email":"[email protected]"},

{"name":"Prem","id":"117","email":"[email protected]"},

{"name":"Chandan","id":"118","email":"[email protected]"},

{"name":"Soham","id":"119","email":"[email protected]"},

{"name":"Mukesh","id":"120","email":"[email protected]"},

{"name":"Ajad","id":"121","email":"[email protected]"}

Model.kt :-

package example.kttpoint.com.kotlinjsonparsing

public class Model{

lateinit var id:String

lateinit var name:String

lateinit var email:String

constructor(id: String,name:String,email:String) {

this.id = id

this.name = name

this.email = email
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
}

constructor()

adapter_layout.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="wrap_content"

android:id="@+id/linearLayout"

android:padding="5dp"

android:orientation="vertical">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/tvId"

android:layout_margin="5dp"

android:textSize="16dp"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/tvName"

android:textSize="16dp"

android:layout_margin="5dp"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
android:id="@+id/tvEmail"

android:layout_margin="5dp"

android:textSize="16dp"/>

</LinearLayout>

CustomAdapter.kt

Create a custom adapter class CustomAdapter.kt and extend BaseAdapter to handle the

custom ListView.

package example.kttpoint.com.kotlinjsonparsing

import android.content.Context

import android.view.LayoutInflater

import android.view.View

import android.view.ViewGroup

import android.widget.BaseAdapter

import android.widget.LinearLayout

import android.widget.TextView

class CustomAdapter(context: Context,arrayListDetails:ArrayList<Model>) : BaseAdap

ter(){

private val layoutInflater: LayoutInflater

private val arrayListDetails:ArrayList<Model>

init {

this.layoutInflater = LayoutInflater.from(context)

this.arrayListDetails=arrayListDetails

}
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
override fun getCount(): Int {

return arrayListDetails.size

override fun getItem(position: Int): Any {

return arrayListDetails.get(position)

override fun getItemId(position: Int): Long {

return position.toLong()

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

val view: View?

val listRowHolder: ListRowHolder

if (convertView == null) {

view = this.layoutInflater.inflate(R.layout.adapter_layout, parent, false)

listRowHolder = ListRowHolder(view)

view.tag = listRowHolder

} else {

view = convertView

listRowHolder = view.tag as ListRowHolder

listRowHolder.tvName.text = arrayListDetails.get(position).name

listRowHolder.tvEmail.text = arrayListDetails.get(position).email

listRowHolder.tvId.text = arrayListDetails.get(position).id

return view
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
}

private class ListRowHolder(row: View?) {

public val tvName: TextView

public val tvEmail: TextView

public val tvId: TextView

public val linearLayout: LinearLayout

init {

this.tvId = row?.findViewById<TextView>(R.id.tvId) as TextView

this.tvName = row?.findViewById<TextView>(R.id.tvName) as TextView

this.tvEmail = row?.findViewById<TextView>(R.id.tvEmail) as TextView

this.linearLayout = row?.findViewById<LinearLayout>(R.id.linearLayout) as LinearLayout

MainActivity.kt

package example.kttpoint.com.kotlinjsonparsing

import android.support.v7.app.AppCompatActivity

import android.os.Bundle

import android.view.View

import android.widget.ListView

import android.widget.ProgressBar

import okhttp3.*

import org.json.JSONArray

import org.json.JSONObject

import java.io.IOException

import kotlin.collections.ArrayList
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI

class MainActivity : AppCompatActivity() {

lateinit var progress:ProgressBar

lateinit var listView_details: ListView

var arrayList_details:ArrayList<Model> = ArrayList();

//OkHttpClient creates connection pool between client and server

val client = OkHttpClient()

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

progress = findViewById(R.id.progressBar)

progress.visibility = View.VISIBLE

listView_details = findViewById<ListView>(R.id.listView) as ListView

run("http://10.0.0.7:8080/jsondata/index.html")

fun run(url: String) {

progress.visibility = View.VISIBLE

val request = Request.Builder()

.url(url)

.build()

client.newCall(request).enqueue(object : Callback {

override fun onFailure(call: Call, e: IOException) {

progress.visibility = View.GONE

override fun onResponse(call: Call, response: Response) {

var str_response = response.body()!!.string()


NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
//creating json object

val json_contact:JSONObject = JSONObject(str_response)

//creating json array

var jsonarray_info:JSONArray= json_contact.getJSONArray("info")

var i:Int = 0

var size:Int = jsonarray_info.length()

arrayList_details= ArrayList();

for (i in 0.. size-1) {

var json_objectdetail:JSONObject=jsonarray_info.getJSONObject(i)

var model:Model= Model();

model.id=json_objectdetail.getString("id")

model.name=json_objectdetail.getString("name")

model.email=json_objectdetail.getString("email")

arrayList_details.add(model)

runOnUiThread {

//stuff that updates ui

val obj_adapter : CustomAdapter

obj_adapter = CustomAdapter(applicationContext,arrayList_details)

listView_details.adapter=obj_adapter

progress.visibility = View.GONE

OUTPUT
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
Pr14> Write a Kotlin program in Android to implement XML Parser.

Code:-

activity_main.xml

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

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/a

pk/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="example.kttpoint.com.kotlinxmlparsingusingxmlpullparser.MainAct

ivity">

<ListView

android:id="@+id/listView"

android:layout_width="match_parent"

android:layout_height="wrap_content" >

</ListView>

</android.support.constraint.ConstraintLayout>

employees.xml

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

<employees>

<employee>

<id>1</id>

<name>Sachin Kumar</name>

<salary>50000</salary>

</employee>
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
<employee>

<id>2</id>

<name>Rahul Kumar</name>

<salary>60000</salary>

</employee>

<employee>

<id>3</id>

<name>John Mike</name>

<salary>70000</salary>

</employee>

<employee>

<id>4</id>

<name>Ajay Kumar</name>

<salary>45000</salary>

</employee>

<employee>

<id>5</id>

<name>Toni Nayer</name>

<salary>55000</salary>

</employee>

<employee>

<id>6</id>

<name>Mr Bony</name>

<salary>42000</salary>

</employee>

<employee>

<id>7</id>

<name>Raj Kumar</name>

<salary>30000</salary>
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
</employee>

<employee>

<id>8</id>

<name>Rahul Kumar</name>

<salary>60000</salary>

</employee>

<employee>

<id>9</id>

<name>John Mike</name>

<salary>70000</salary>

</employee>

<employee>

<id>10</id>

<name>Sachin Kumar</name>

<salary>50000</salary>

</employee>

<employee>

<id>11</id>

<name>Rahul Kumar</name>

<salary>60000</salary>

</employee>

<employee>

<id>12</id>

<name>John Mike</name>

<salary>70000</salary>

</employee>

</employees>

Employee.kt

package example.kttpoint.com.kotlinxmlparsingusingxmlpullparser
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI

class Employee {

var id: Int = 0

var name: String? = null

var salary: Float = 0.toFloat()

override fun toString(): String {

return " Id = $id\n Name = $name\n Salary = $salary"

XmlPullParserHandler.kt

package example.kttpoint.com.kotlinxmlparsingusingxmlpullparser

import org.xmlpull.v1.XmlPullParserException

import org.xmlpull.v1.XmlPullParser

import org.xmlpull.v1.XmlPullParserFactory

import java.io.IOException

import java.io.InputStream

class XmlPullParserHandler {

private val employees = ArrayList<Employee>()

private var employee: Employee? = null

private var text: String? = null

fun parse(inputStream: InputStream): List<Employee> {

try {

val factory = XmlPullParserFactory.newInstance()

factory.isNamespaceAware = true

val parser = factory.newPullParser()


NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
parser.setInput(inputStream, null)

var eventType = parser.eventType

while (eventType != XmlPullParser.END_DOCUMENT) {

val tagname = parser.name

when (eventType) {

XmlPullParser.START_TAG -

> if (tagname.equals("employee", ignoreCase = true)) {

// create a new instance of employee

employee = Employee()

XmlPullParser.TEXT -> text = parser.text

XmlPullParser.END_TAG -

> if (tagname.equals("employee", ignoreCase = true)) {

// add employee object to list

employee?.let { employees.add(it) }

} else if (tagname.equals("id", ignoreCase = true)) {

employee!!.id = Integer.parseInt(text)

} else if (tagname.equals("name", ignoreCase = true)) {

employee!!.name = text

} else if (tagname.equals("salary", ignoreCase = true)) {

employee!!.salary = java.lang.Float.parseFloat(text)

else -> {

eventType = parser.next()

}
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI
} catch (e: XmlPullParserException) {

e.printStackTrace()

} catch (e: IOException) {

e.printStackTrace()

return employees

MainActivity.kt

package example.kttpoint.com.kotlinxmlparsingusingxmlpullparser

import android.support.v7.app.AppCompatActivity

import android.os.Bundle

import android.widget.ArrayAdapter

import android.widget.ListView

import java.io.IOException

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

val listView = findViewById<ListView>(R.id.listView)

var employees: List<Employee>? = null

try {

val parser = XmlPullParserHandler()

val istream = assets.open("employees.xml")

employees = parser.parse(istream)
NAME: -DHRUVNARAYAN RAWAL
SUBJECT: ANDROID
ENROLMENT NO: IU2082820063
BRANCH: IMCA
SEMESTER: VI

val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, employees)

listView.adapter = adapter

} catch (e: IOException) {

e.printStackTrace()

OUTPUT

You might also like