0% found this document useful (0 votes)
49 views

Android (Ankit)

1. The document is a certificate issued by Satish Pradhan Dnyanasadhana College, Thane certifying that Mr. Ankit Singh of SYBSc Computer Science (Semester-III) has successfully completed all the practical work in the subject of Android Application Development under the guidance of Assistant Professor Snehal Rane. 2. It contains Ankit Singh's details like name, program, and roll number. 3. The certificate is signed by the subject in charge and head of the department.

Uploaded by

Piyush Gond
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Android (Ankit)

1. The document is a certificate issued by Satish Pradhan Dnyanasadhana College, Thane certifying that Mr. Ankit Singh of SYBSc Computer Science (Semester-III) has successfully completed all the practical work in the subject of Android Application Development under the guidance of Assistant Professor Snehal Rane. 2. It contains Ankit Singh's details like name, program, and roll number. 3. The certificate is signed by the subject in charge and head of the department.

Uploaded by

Piyush Gond
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Exam Seat No:

Satish Pradhan Dnyanasadhana


College Thane

Certificate
This is to certify that Mr./Miss.: Ankit Singh of SYBSc Computer Science
(Semester-III) Class has successfully completed all the practical work in subject
Android Application Development, under the guidance of Asst Prof. Snehal
Rane (subject in charge) during Year 2022-23 in partial fulfilment of Computer
Science Practical Examination conducted by University of Mumbai.

Subject in charge Head of the Department

Date
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

Sr.no Title Date Sign


1. Write a program using Kotlin to implement control
structures and loops.
1 2. Write a program to implement object-oriented
concepts in Kotlin.

1. Create an Android application to design screens using


different layouts and UI including Button, Edittext,
Textview, Radio Button, etc.
2 2. Write an android application demonstrating response
to event/user interaction for Checkbox, Radio button,
Button, Spinner.

1. Create an application to create an Image Flipper and


Image Gallery. Click on the image displays the
3 information about the image.
2. Create an app to use Gridview for shopping cart
applications.

1. Create an Android application to demonstrate implicit


and explicit intents
4 2. Create an application to demonstrate shared
preferences

1. Create an Android application to demonstrate the use


of Broadcast listeners.
5 2. Create an Android application to create and use
services.

1. Create an Android application to demonstrate XML


based animation
6 2. Create an Android application to display canvas and
allow the user to draw on it.

1. Create a media player application in android that


plays audio. Implement play, pause, and loop features.
7 2. Create an Android application to use a camera and
capture image/video and display them on the screen.

1. Create an android application to implement Asynctask


and threading concepts.
8 2. Create an Android application to demonstrate the
different types of menus. a. Pop-up Menu b. Context
Menu c. Option Menu

Create an Android application to record the current


9 location. Based on the current location allow the user to
use some useful services/applications

2
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

Practical 1
Aim: Write a program using Kotlin to implement control structures and loops.
Program 1: if-else statement
fun main() {
val numb = -10
println(" Entered number is: $numb")
if (numb > 0) {
println("Positive")
} else {
println("Negative")
}}
Output:

Program 2: if-else statement


fun main() {
val a = -9
val b = -11
val max =
if (a > b) {
println("$a is larger than $b.")
println("max variable holds value of a.")
} else {
println("$b is larger than $a.")
println("max variable holds value of b.")}
println("max = $max")}
Output:

Program 3: If-elseif ladder


fun main() {
val number = 0
val result =
if (number > 0){
"positive number"
}else if (number < 0){
"negative number"
}else{
"zero"}
println("number is $result")}
Output:

Program 4: Nested if statements:


fun main() {
val n1 = 3
val n2 = 5
val n3 = -2

3
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

val max = if (n1 > n2) { if (n1 > n3){ n1 } else { n3} } else { if (n2 > n3){ n2 } else { n3 }}
println("max = $max")}
Output:

Program 5: When statement:


fun main() {
val day = 4
val result = when(day) {
1 -> "Monday"
2 -> "Tuesday"
3 -> "Wednesday"
4 -> "Thursday"
5 -> "Friday"
6 -> "Saturday"
7 -> "Sunday"
else -> "Invalid day."}
println(result)}
Output:

Program 6: Using readline for talking input at runtime


fun main() {
val a = 12
val b = 5
println("Enter operator either +, -, * or /")
val operator = readLine()
val result = when(operator) {
"+" -> a + b "-" -> a - b "*" -> a * b "/" -> a / b
else -> "$operator operator is invalid operator."}
println("result = $result")}
Output:

Program 7: While loop


fun main(args: Array < String > ) {
var sum = 0
var i = 100
while (i != 0) {
sum += i // sum = sum + i;
--i}
println("sum = $sum")}
Output:

Program 8: Do-while loop


fun main() {
var sum: Int = 0
var input: String
do {
print("Enter an integer: ")
input = readLine()!!
4
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

sum += input.toInt()
} while (input != "0")
println("sum = $sum")}
Output:

Program 10: For loop using range function


fun main(args: Array < String > ) {
print("for (i in 1..5) print(i) = ")
for (i in 1. .5) print(i)
println()
print("for (i in 5..1) print(i) = ")
for (i in 5. .1) print(i)
println()
print("for (i in 5 downTo 1) print(i) = ")
for (i in 5 downTo 1) print(i)
println()
print("for (i in 1..5 step 2) print(i) = ")
for (i in 1. .5 step 2) print(i)
println()
print("for (i in 5 downTo 1 step 2) print(i) = ")
for (i in 5 downTo 1 step 2) print(i)}
Output:

Aim: Write a program to implement object-oriented concepts in Kotlin.


Program:
class Lamp {
private var isOn: Boolean = false
fun turnOn() { isOn = true}
fun turnOff() { isOn = false}
fun displayLightStatus() {
if (isOn == true){
println("lamp is on.")
}else{ println("lamp is off.")}}}
fun main(args: Array <String> ) {
val lamp = Lamp()
lamp.displayLightStatus()}
Output:

5
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

Practical 2
Aim: Create an Android application to design screens using different layouts and UI
including Button, Edittext, Textview, Radio Button, etc.
Aim: Write an android application demonstrating a response to event/user interaction
for Checkbox, Radio button, Button, and Spinner.
Program:
● 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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName" />
<TextView
android:id="@+id/gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender"
android:textSize="20sp" />
<TextView
android:id="@+id/textViewResponse"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textStyle="bold|italic" />
<RadioGroup
android:id="@+id/selectgender"
android:layout_width="129dp"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
6
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

<TextView
android:id="@+id/lang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I like"
android:textSize="20sp" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kotlin" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Python" />
<TextView
android:id="@+id/language"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:textStyle="bold" />
<Spinner
android:id="@+id/spinner"
android:layout_width="143dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/course"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Your Class"
android:textSize="18sp" />
<TextView
android:id="@+id/myclass"
android:layout_width="107dp"
android:layout_height="wrap_content"
android:textStyle="bold" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
● MainActivity.kt
package com.visdev.practtwo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.core.view.get
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle ? ) {
super.onCreate(savedInstanceState)

7
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

setContentView(R.layout.activity_main)
val Name = findViewById <EditText>(R.id.name)
val submitButton = findViewById <Button> (R.id.button)
val dispMessage = findViewById <TextView> (R.id.textViewResponse)
val radioGroup = findViewById <RadioGroup> (R.id.selectgender)
val radioB1 = findViewById <RadioButton> (R.id.radioButton1)
val radioB2 = findViewById <RadioButton> (R.id.radioButton2)
val CheckB1 = findViewById <CheckBox> (R.id.checkBox1)
val CheckB2 = findViewById <CheckBox> (R.id.checkBox2)
val CodingLang = findViewById <TextView> (R.id.language)
val Universityyear = findViewById <Spinner> (R.id.spinner)
val myclass = findViewById <TextView> (R.id.myclass)
val year = arrayOf("FYCS", "SYCS", "TYCS")
val arrayAdp = ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, year)
Universityyear.adapter = arrayAdp
Universityyear.onItemSelectedListener = object: AdapterView.OnItemSelectedListener {
override fun onItemSelected(p0: AdapterView < * > ? , p1 : View ? , position :
Int, p3: Long) { myclass.text = "Your Class: " + year[position]}
override fun onNothingSelected(p0: AdapterView<*> ? ) {
myclass.text = "Please select Your Class"}}
radioGroup.setOnCheckedChangeListener {group,checkedId ->
if (checkedId == R.id.radioButton1)
dispMessage.setText("Gender" + radioB1.text)
if (checkedId == R.id.radioButton2)
dispMessage.setText("Gender" + radioB2.text)}
submitButton.setOnClickListener {
dispMessage.setText("Welcome " + Name.text)
if (CheckB1.isChecked && CheckB2.isChecked) {
CodingLang.text = "You love both languages"
} else if (CheckB1.isChecked) {
CodingLang.text = "You love Python"
} else if (CheckB2.isChecked) {
CodingLang.text = "You love Kotlin"
} else {
CodingLang.text = "You don't like these languages"
}}}}
Output:

8
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

Practical 3
Aim: Create an application to create Image Flipper and Image Gallery. Click on the image
displays the information about the image.
Program:
● 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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<ViewFlipper
android:layout_width="match_parent"
android:layout_height="100dp"
android:flipInterval="2000"
android:autoStart="true"
android:inAnimation="@android:anim/slide_in_left"
android:outAnimation="@android:anim/slide_out_right">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/male" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/female" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/sus" />
</ViewFlipper>
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="350dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_margin="10dp"
android:src="@drawable/male"/>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="1dp"
android:id="@+id/horizontal">

9
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

<LinearLayout
android:id="@+id/imgContainer"
android:layout_width="match_parent"
android:layout_height="90dp"
android:orientation="horizontal"></LinearLayout>
</HorizontalScrollView>
</LinearLayout>
● MainActivity.kt
package com.visdev.practtwo
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
class MainActivity: AppCompatActivity() {
private var images =
intArrayOf(R.drawable.male,R.drawable.female,R.drawable.sus,R.drawable.male,R.drawable.female
,R.drawable.sus,R.drawable.male,R.drawable.female,R.drawable.sus,R.drawable.male,R.drawable.f
emale,R.drawable.sus)
override fun onCreate(savedInstanceState: Bundle ? ) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
for (i in 0. .11) {
val imageView = ImageView(this)
val layoutParams = ViewGroup.LayoutParams(200, ViewGroup.LayoutParams.MATCH_PARENT)
imageView.layoutParams = layoutParams
imageView.setImageResource(images[i])
imageView.setOnClickListener {
findViewById < ImageView > (R.id.imageView).setImageResource(images[i])}
findViewById < LinearLayout > (R.id.imgContainer).addView(imageView)}}}
Output:

10
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

Aim: Create an app to use Gridview for shopping cart applications.


Program:
● 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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<GridView
android:id="@+id/gridCont"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchMode="columnWidth"
android:numColumns="3"
android:listSelector="#00000000"
android:columnWidth="100dp"
android:horizontalSpacing="6dp"
android:verticalSpacing="6dp"
android:gravity="center"
android:padding="6dp" />
</LinearLayout>
● gridframe.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp"
app:cardPreventCornerOverlap="true"
app:cardCornerRadius="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:orientation="vertical">
<ImageView
android:id="@+id/imgData"
android:layout_width="80dp"
android:layout_height="80dp"
android:padding="5dp"
android:src="@drawable/female" />
<TextView
android:id="@+id/txtData"
android:layout_width="match_parent"
11
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

android:layout_height="wrap_content"
android:padding="4dp"
android:text="Test"
android:textAlignment="center"
android:textColor="@color/purple_500" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
● GridItems.kt
package com.visdev.practtwo
class GridItems {
var icons: Int ? = 0
var name: String ? = null
constructor(icons: Int ? , name : String ? ) {
this.icons = icons
this.name = name}}
● GridAdapter.kt
package com.visdev.practtwo
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import java.text.FieldPosition
class GridAdapter(var context: Context,
var arratList: ArrayList < GridItems > ): BaseAdapter() {
override fun getCount(): Int { return arratList.size }
override fun getItem(position: Int): Any { return arratList.get(position) }
override fun getItemId(position: Int): Long { return position.toLong() }
override fun getView(position: Int, view: View ? , parent : ViewGroup ? ): View {
var view: View = View.inflate(context, R.layout.gridframe, null)
var icons: ImageView = view.findViewById(R.id.imgData)
var names: TextView = view.findViewById(R.id.txtData)
var gridItem: GridItems = arratList.get(position)
icons.setImageResource(gridItem.icons!!)
names.text = gridItem.name
return view}}
● MainActivity.kt
package com.visdev.practtwo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemClickListener
import android.widget.GridView
import android.widget.Toast
class MainActivity: AppCompatActivity(), AdapterView.OnItemClickListener {
private var gridView: GridView ? = null
private var arrayList: ArrayList < GridItems > ? = null
private var gridAdapter: GridAdapter ? = null
private var images =
intArrayOf(R.drawable.male,R.drawable.female,R.drawable.sus,R.drawable.male,R.drawable.female
12
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

,R.drawable.sus,R.drawable.male,R.drawable.female,R.drawable.sus,R.drawable.male,R.drawable.f
emale,R.drawable.sus,)
override fun onCreate(savedInstanceState: Bundle ? ) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
gridView = findViewById(R.id.gridCont)
arrayList = ArrayList()
arrayList = setDataList()
gridAdapter = GridAdapter(this, arrayList!!)
gridView?.adapter = gridAdapter
gridView?.onItemClickListener = this
}
private fun setDataList(): ArrayList < GridItems > {
var arrayList: ArrayList < GridItems > = ArrayList()
arrayList.add(GridItems(R.drawable.male, "Male"))
arrayList.add(GridItems(R.drawable.female, "Female"))
arrayList.add(GridItems(R.drawable.sus, "Sus"))
arrayList.add(GridItems(R.drawable.male, "Male"))
arrayList.add(GridItems(R.drawable.female, "Female"))
arrayList.add(GridItems(R.drawable.sus, "Sus"))
arrayList.add(GridItems(R.drawable.male, "Male"))
arrayList.add(GridItems(R.drawable.female, "Female"))
arrayList.add(GridItems(R.drawable.sus, "Sus"))
return arrayList
}
override fun onItemClick(parent: AdapterView < * > ? , view : View ? , position : Int, id: Long) {
var gridItems: GridItems = arrayList!![position]
Toast.makeText(this, gridItems.name, Toast.LENGTH_SHORT).show()
}
}
Output:

13
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

Practical 4
Aim: Create an Android application to demonstrate implicit and explicit intents
Aim: Create an application to demonstrate shared preferences
Program:
● 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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<EditText
android:id="@+id/name"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName" />
<Button
android:id="@+id/ImplicitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/purple_700"
android:text="Implicit" />
<Button
android:id="@+id/explicitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/purple_700"
android:text="explicit" />
</LinearLayout>
● activity_data.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:orientation="vertical"
android:gravity="center"
tools:context=".DataActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
14
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

android:text="Your Name"
android:textSize="18dp"/>
</LinearLayout>
● DataActivity.kt
package com.visdev.practtwo
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.*
import android.widget.AdapterView.OnItemClickListener
import androidx.appcompat.app.AppCompatActivity
class DataActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle ? ) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_data)
val pName = findViewById < TextView > (R.id.textView)
val sharedPref = getSharedPreferences("DaApp", Context.MODE_PRIVATE)
pName.text = sharedPref.getString("name", "VSatish")}}
● MainActivity.kt
package com.visdev.practtwo
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.*
import android.widget.AdapterView.OnItemClickListener
import androidx.appcompat.app.AppCompatActivity
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle ? ) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val impbtn = findViewById < Button > (R.id.ImplicitBtn)
val expbtn = findViewById < Button > (R.id.explicitBtn)
val txtName = findViewById < EditText > (R.id.name)
val sharedPref = getSharedPreferences("DaApp", Context.MODE_PRIVATE)
var sharedEdit = sharedPref.edit()
impbtn.setOnClickListener { startActivity(Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.casagbic.com/")) }
expbtn.setOnClickListener(object: View.OnClickListener {
override fun onClick(view: View ? ) {
if ((txtName.text)?.isEmpty() == true) {
Toast.makeText(this @MainActivity, "Please enter name", Toast.LENGTH_SHORT).show()
} else {
sharedEdit.putString("name", txtName.text.toString())
sharedEdit.commit()
}
startActivity(Intent(this, DataActivity::class.java))}})}}
Output:

15
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

16
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

Practical 5
Aim: Create an Android application to demonstrate the use of Broadcast listeners.
Aim: Create an Android application to create and use services.
Program:
● 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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="@+id/startServBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/purple_700"
android:text="Start Service" />
<Button
android:id="@+id/stopServBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/purple_700"
android:text="Stop Service" />
</LinearLayout>
● AudioService.kt
package com.visdev.practtwo
import android.app.Service
import android.content.Intent
import android.media.MediaPlayer
import android.os.IBinder
class AudioService: Service() {
private lateinit var player: MediaPlayer
override fun onStartCommand(intent: Intent ? , flags : Int, startId: Int): Int {
player = MediaPlayer.create(this, R.raw.music)
player.isLooping = true
player.start()
return START_STICKY}
override fun onDestroy() {
super.onDestroy()
player.stop()}
override fun onBind(intent: Intent): IBinder ? { return null }}
● MainActivity.kt
package com.visdev.practtwo
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
17
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

import android.widget.*
import android.widget.AdapterView.OnItemClickListener
import androidx.appcompat.app.AppCompatActivity
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle ? ) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val startbtn = findViewById < Button > (R.id.startServBtn)
val stopbtn = findViewById < Button > (R.id.stopServBtn)
startServBtn.setOnClickListener { startService(Intent(this, AudioService::class.java)}
stopServBtn.setOnClickListener { stopService(Intent(this, AudioService::class.java)) }}}
Output:

18
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

Practical 6
Aim: Create an Android application to demonstrate XML based animation.
Program:
● 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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imghu"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/sus"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginBottom="0dp"
android:text="Animate"/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginBottom="0dp"
android:text="Blink"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Zoom"/>
</LinearLayout>
● myanim.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true">
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toAlpha="1.0" />
</set>
● blink.xml
19
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

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


<set
xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="600"
android:repeatMode="reverse"
android:repeatCount="infinite"/>
</set>
● zoom.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true">
<scale
android:duration="1000"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="3"
android:toYScale="3"></scale>
</set>
● MainActivity.kt
package com.visdev.practtwo
import android.os.Bundle
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle ? ) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var imgView = findViewById < ImageView > (R.id.imghu)
findViewById < Button > (R.id.button).setOnClickListener {
val animat = AnimationUtils.loadAnimation(this, R.anim.myanim)
imgView!!.visibility = View.VISIBLE
imgView!!.startAnimation(animat)}
findViewById < Button > (R.id.button1).setOnClickListener {
val animat1 = AnimationUtils.loadAnimation(this, R.anim.blink)
imgView!!.visibility = View.VISIBLE
imgView!!.startAnimation(animat1)}
findViewById < Button > (R.id.button2).setOnClickListener {
val animat2 = AnimationUtils.loadAnimation(this, R.anim.zoom)
imgView!!.visibility = View.VISIBLE
imgView!!.startAnimation(animat2)}}
}
Output:

20
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

Aim: Create an Android application to display canvas and allow the user to draw on it.
Program:
● Implement
com.github.Miihir79:DrawingCanvas:+
● 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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<com.mihir.drawingcanvas.drawingView
android:id="@+id/drawing_view"
android:layout_width="match_parent"
android:layout_height="600dp"></com.mihir.drawingcanvas.drawingView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/btn_redo"
21
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

android:layout_width="100dp"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/btn_color"
app:srcCompat="@drawable/baseline_redo_24" />
<ImageButton
android:id="@+id/btn_undo"
android:layout_width="100dp"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/btn_brush"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="@drawable/baseline_undo_24" />
<ImageButton
android:id="@+id/btn_brush"
android:layout_width="100dp"
android:layout_height="match_parent"
app:srcCompat="@drawable/baseline_brush_24" />
<ImageButton
android:id="@+id/btn_clearscreen"
android:layout_width="100dp"
android:layout_height="match_parent"
app:srcCompat="@drawable/baseline_clear_24" />
</LinearLayout>
</LinearLayout>
● MainActivity.kt
package com.visdev.practtwo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageButton
import com.mihir.drawingcanvas.drawingView
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle ? ) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val drawing_view = findViewById < drawingView > (R.id.drawing_view)
findViewById < ImageButton > (R.id.btn_undo).setOnClickListener { drawing_view.undo() }
findViewById < ImageButton > (R.id.btn_redo).setOnClickListener { drawing_view.redo() }
findViewById < ImageButton > (R.id.btn_brush).setOnClickListener {
drawing_view.setSizeForBrush(25) //0-35
drawing_view.setBrushAlpha(100) //0-255
drawing_view.setBrushColor(R.color.teal_200) }
findViewById < ImageButton > (R.id.btn_clearscreen).setOnClickListener {
drawing_view.clearDrawingBoard() }
val alpha = drawing_view.getBrushAlpha()
drawing_view.erase()
val brushSize = drawing_view.getBrushSize()
val brushColor = drawing_view.getBrushColor()

22
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

val drawing = drawing_view.getDrawing() }}


Output:

23
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

Practical 7
Aim: Create a media player application in android that plays audio. Implement play,
pause, and loop features.
Program:
● 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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:src="@drawable/male"/>
<RelativeLayout
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginTop="50dp">
<TextView
android:id="@+id/tv_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_due"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_pass"
android:saveEnabled="false" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/pauseBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"

24
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

android:enabled="false"
android:text="Pause" />
<Button
android:id="@+id/playBtn"
android:layout_width="88dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Play" />
<Button
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:enabled="false"
android:text="Stop"/>
</LinearLayout>
</LinearLayout>
● MainActivity.kt
package com.visdev.practtwo
import android.media.MediaPlayer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.widget.Button
import android.widget.SeekBar
import android.widget.TextView
import android.widget.Toast
class MainActivity: AppCompatActivity() {
private lateinit var mediaPlayer: MediaPlayer
private lateinit var runnable: Runnable
private var handler: Handler = Handler()
private var pause: Boolean = false
private var seek_bar: SeekBar ? = null
private var tv_pass: TextView ? = null
private var tv_due: TextView ? = null
override fun onCreate(savedInstanceState: Bundle ? ) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
seek_bar = findViewById(R.id.seek_bar)
tv_due = findViewById < TextView > (R.id.tv_due)
tv_pass = findViewById < TextView > (R.id.tv_pass)
val playBtn = findViewById < Button > (R.id.playBtn)
val pauseBtn = findViewById < Button > (R.id.pauseBtn)
val stopBtn = findViewById < Button > (R.id.stopBtn)
playBtn.setOnClickListener {
if (pause) {
mediaPlayer.seekTo(mediaPlayer.currentPosition)
mediaPlayer.start()
pause = false
Toast.makeText(this, "media playing", Toast.LENGTH_SHORT).show()
} else {
mediaPlayer = MediaPlayer.create(applicationContext, R.raw.rickbol)

25
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

mediaPlayer.start()
Toast.makeText(this, "media playing", Toast.LENGTH_SHORT).show()}
initializeSeekBar()
playBtn.isEnabled = false
pauseBtn.isEnabled = true
stopBtn.isEnabled = true
mediaPlayer.setOnCompletionListener {
playBtn.isEnabled = true
pauseBtn.isEnabled = false
stopBtn.isEnabled = false
Toast.makeText(this, "end", Toast.LENGTH_SHORT).show() }}
pauseBtn.setOnClickListener {
if (mediaPlayer.isPlaying) {
mediaPlayer.pause()
pause = true
playBtn.isEnabled = true
pauseBtn.isEnabled = false
stopBtn.isEnabled = true
Toast.makeText(this, "media pause", Toast.LENGTH_SHORT).show() }}
stopBtn.setOnClickListener {
if (mediaPlayer.isPlaying || pause.equals(true)) {
pause = false
seek_bar!!.progress = 0
mediaPlayer.stop()
mediaPlayer.reset()
mediaPlayer.release()
handler.removeCallbacks(runnable)
playBtn.isEnabled = true
pauseBtn.isEnabled = false
stopBtn.isEnabled = false
tv_pass!!.text = ""
tv_due!!.text = ""
Toast.makeText(this, "media stop", Toast.LENGTH_SHORT).show() }}
seek_bar!!.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, i: Int, b: Boolean) {
if (b) { mediaPlayer.seekTo(i * 1000) }}
override fun onStartTrackingTouch(seekBar: SeekBar) {}
override fun onStopTrackingTouch(seekBar: SeekBar) {} })}
private fun initializeSeekBar() {
seek_bar!!.max = mediaPlayer.seconds
runnable = Runnable {
seek_bar!!.progress = mediaPlayer.currentSeconds
tv_pass!!.text = "${mediaPlayer.currentSeconds} sec"
val diff = mediaPlayer.seconds - mediaPlayer.currentSeconds
tv_due!!.text = "$diff sec"
handler.postDelayed(runnable, 1000)}
handler.postDelayed(runnable, 1000)}
val MediaPlayer.seconds: Int
get() { return this.duration / 1000 }
val MediaPlayer.currentSeconds: Int
get() { return this.currentPosition / 1000 } }
Output:

26
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

Aim: Create an Android application to use a camera and capture image/video and display
them on the screen.
Program:
● 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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<ImageView
android:id="@+id/click_image"
android:layout_width="350dp"
android:layout_height="450dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="70dp" />
<Button
android:id="@+id/camera_button"
android:layout_width="100dp"
android:layout_height="50dp"
27
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

android:text="Camera" />
</LinearLayout>
● MainActivity.kt
package com.visdev.practtwo
import android.content.Intent
import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.view.View
import android.widget.Button
import android.widget.ImageView
class MainActivity: AppCompatActivity() {
private lateinit var cameraOpenId: Button
private lateinit var clickImageId: ImageView
override fun onCreate(savedInstanceState: Bundle ? ) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
cameraOpenId = findViewById(R.id.camera_button)
clickImageId = findViewById(R.id.click_image)
cameraOpenId.setOnClickListener {
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(cameraIntent, pic_id)}}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent ? ) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == pic_id) {
val photo = data!!.extras!!["data"] as Bitmap ?
clickImageId.setImageBitmap(photo)}}
companion object {
private
const val pic_id = 123}}
Output:

28
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

Practical 8
Aim: Create an android application to implement Asynctask and threading concepts.
Aim: Create an Android application to demonstrate the different types of menus. a. Pop-
up Menu b. Context Menu c. Option Menu
Program:
● Implement
com.google.firebase:firebase-crashlytics-buildtools:2.9.4
● 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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:src="@drawable/male"/>
<RelativeLayout
android:layout_width="368dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:layout_marginTop="50dp">
<TextView
android:id="@+id/tv_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_due"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true" />
<SeekBar
android:id="@+id/seek_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_pass"
android:saveEnabled="false" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="@+id/pauseBtn"
29
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:enabled="false"
android:text="Pause" />
<Button
android:id="@+id/playBtn"
android:layout_width="88dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Play" />
<Button
android:id="@+id/stopBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:enabled="false"
android:text="Stop"/>
</LinearLayout>
</LinearLayout>
● AsyncCallBack.kt
package com.visdev.practtwo
internal interface AsyncCallBack { fun setResult(result: String ? ) }
● GetAsyncTask.kt
package com.visdev.practtwo
import android.content.Context
import android.os.AsyncTask
import android.view.View
import android.widget.ProgressBar
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.client.HttpClient
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.client.methods.HttpGet
import com.google.firebase.crashlytics.buildtools.reloc.org.apache.http.impl.client.HttpClientBuilder
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
class GetAsyncTask: AsyncTask < Void ? , Void ? , String > () {
private var activity: MainActivity ? = null
private var asyncCallBack: AsyncCallBack ? = null
private var progessbar: ProgressBar ? = null
fun setInstance(context: Context ? , progressBar : ProgressBar ? ): GetAsyncTask {
activity = context as MainActivity ?
asyncCallBack = context
this.progessbar = progressBar
return this }
override fun onPreExecute() {
super.onPreExecute()
progessbar!!.visibility = View.VISIBLE }
override fun doInBackground(vararg p0: Void ? ): String {
val httpGet = HttpGet("https://api.quotable.io/random")
val client: HttpClient = HttpClientBuilder.create().build()
var result = ""
try {

30
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

val response = client.execute(httpGet)


val statusCodes = response.statusLine.statusCode
if (statusCodes == 200) {
val inputStream = response.entity.content
val reader = BufferedReader(InputStreamReader(inputStream))
result = reader.readText()
}
} catch (e: IOException) {
e.printStackTrace()
} finally {
activity!!.runOnUiThread {
progessbar!!.visibility = View.GONE
} } return result }
override fun onPostExecute(result: String ? ) {
super.onPostExecute(result)
asyncCallBack!!.setResult(result) }}
● MainActivity.kt
package com.visdev.practtwo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import org.json.JSONArray
import org.json.JSONObject
class MainActivity: AppCompatActivity() {
private lateinit var mediaPlayer: MediaPlayer
private lateinit var runnable: Runnable
private var handler: Handler = Handler()
private var pause: Boolean = false
private var seek_bar: SeekBar ? = null
private var tv_pass: TextView ? = null
private var tv_due: TextView ? = null
override fun onCreate(savedInstanceState: Bundle ? ) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById < Button > (R.id.getDataBtn).setOnClickListener {
GetAsyncTask().setInstance(this, findViewById(R.id.progressBar)).execute() }}
override fun setResult(result: String ? ) {
val json = JSONObject(result!!)
findViewById < TextView > (R.id.idTxt).text = json.getString("_id")
findViewById < TextView > (R.id.contextTxt).text = json.getString("content")
findViewById < TextView > (R.id.authorTxt).text = json.getString("author")
findViewById < TextView > (R.id.tagTxt).text = json.getJSONArray("tags").toString()
findViewById < TextView > (R.id.slugTxt).text = json.getString("authorSlug")
findViewById < TextView > (R.id.lengthTxt).text = json.getString("length")
findViewById < TextView > (R.id.addedTxt).text = json.getString("dateAdded")
findViewById < TextView > (R.id.modifiedTxt).text = json.getString("dateModified")
}
Output:

31
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

32
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

Practical 9
Aim: Create an Android application to record the current location. Based on the current
location allow the user to use some useful services/applications
Program:
● Implement
com.google.android.gms:play-services-location:21.0.1
● 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:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_latitude"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:gravity="center"
android:text="Latitude"
android:textColor="@color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_longitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:text="Longitude"
android:textColor="@color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_countryName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:gravity="center"
android:text="Country Name"
android:textColor="@color/black"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_Locality"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:gravity="center"
android:text="Locality"
android:textColor="@color/black"

33
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

android:textSize="16sp" />
<TextView
android:id="@+id/tv_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:gravity="center"
android:text="Address"
android:textColor="@color/black"
android:textSize="16sp" />
<Button
android:id="@+id/btn_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get My Current Location"
android:textAllCaps="false"
android:textSize="16sp" />
</LinearLayout>
● MainActivity.kt
package com.visdev.practtwo
import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Address
import android.location.Geocoder
import android.location.Location
import android.location.LocationManager
import android.os.Bundle
import android.provider.Settings
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import java.util.*
class MainActivity: AppCompatActivity() {
private lateinit
var mFusedLocationClient: FusedLocationProviderClient
private val permissionId = 2
override fun onCreate(savedInstanceState: Bundle ? ) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
findViewById < Button > (R.id.btn_location).setOnClickListener { getLocation() }}
@SuppressLint("MissingPermission", "SetTextI18n")
private fun getLocation() {
if (checkPermissions()) {
if (isLocationEnabled()) {

34
Satish Pradhan Dnyanasadhana College, Thane [A.Y. 2022-2023]
Name: Ankit Singh Roll No:76
Program: B.SC.CS (SEM IV) Subject: Android Application Development (PR)

mFusedLocationClient.lastLocation.addOnCompleteListener(this) { task ->


val location: Location ? = task.result
if (location != null) {
val geocoder = Geocoder(this, Locale.getDefault())
val list: List < Address > =
geocoder.getFromLocation(location.latitude, location.longitude, 1) as List < Address >
findViewById < TextView > (R.id.tv_latitude).text = "Latitude\n${list[0].latitude}"
findViewById < TextView > (R.id.tv_longitude).text = "Longitude\n${list[0].longitude}"
findViewById < TextView > (R.id.tv_countryName).text = "Country
Name\n${list[0].countryName}"
findViewById < TextView > (R.id.tv_Locality).text = "Locality\n${list[0].locality}"
findViewById < TextView > (R.id.tv_address).text =
"Address\n${list[0].getAddressLine(0)}" }}} else {
Toast.makeText(this, "Please turn on location", Toast.LENGTH_LONG).show()
val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivity(intent) }} else { requestPermissions() }}
private fun isLocationEnabled(): Boolean {
val locationManager: LocationManager =
getSystemService(Context.LOCATION_SERVICE) as LocationManager
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ||
locationManager.isProviderEnabled( LocationManager.NETWORK_PROVIDER)}
private fun checkPermissions(): Boolean {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION
) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
return true } return false }
private fun requestPermissions() {
ActivityCompat.requestPermissions( this, arrayOf(
Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION),
permissionId)}
@SuppressLint("MissingSuperCall")
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array < String > ,
grantResults: IntArray) {
if (requestCode == permissionId) {
if ((grantResults.isNotEmpty() && grantResults[0] ==
PackageManager.PERMISSION_GRANTED)) { getLocation()}}}}
Output:

35

You might also like