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

Android Practical

Mr. Sahil Ramjan Saiyad from SY. BSc Computer Science has successfully completed practical work in Android Application Development during the academic year 2023-24 at Satish Pradhan Dnyanasadhana College, Thane. The document includes a detailed index of practical assignments, such as implementing control structures in Kotlin, creating Android applications with various UI components, and developing applications for image galleries and media players. The certificate is signed by the subject in-charge and the head of the department.

Uploaded by

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

Android Practical

Mr. Sahil Ramjan Saiyad from SY. BSc Computer Science has successfully completed practical work in Android Application Development during the academic year 2023-24 at Satish Pradhan Dnyanasadhana College, Thane. The document includes a detailed index of practical assignments, such as implementing control structures in Kotlin, creating Android applications with various UI components, and developing applications for image galleries and media players. The certificate is signed by the subject in-charge and the head of the department.

Uploaded by

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

Enter Seat No: -

SATISH PRADHAN DNYANASADHANA COLLEGE, THANE


(Arts,Science and Commerce)
Re-Accredited “B+”Grade (CGPA 2.69) by NAAC, ISO 21001:2018
(Certified) Affiliated To University of Mumbai

CERTIFICATE

This is to certify that Mr: Sahil Ramjan Saiyad of SY. BSc Computer Science
(Semester-IV) Class has successfully completed all the practical work in subject
Android Application Development, under the guidance of Asst Prof. Uzma
Shaikh (subject in charge) during Year 2023-24 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. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A

INDEX
SR PRACTICAL PAGE DATE SIGN
NO NO
I. Write a program using Kotlin to implement control
structures and loops.
1.
ii. Write a program to implement object-oriented
1 4/1/24
concepts in Kotlin.
I. Create an Android application to design screens
using different layouts and UI including Button, Edit
text, Text view, Radio Button etc.ii. Write an
2. android application demonstrating response to
event/user interaction for a. Checkbox
4 16/1/24
b. Radio button
c. Button
d. Spinner
I. Create an application to create Image Flipper and
Image Gallery. On click on the image display the
3.
information about the image. 8 23/1/24
ii. Create an application to use Gridview for
shopping cart application.
I. Create an Android application to demonstrate
4. implicit and explicit intents
ii. Create an application to demonstrate shared
15 1/2/24
preferences
I. Create an Android application to demonstrate the
5. use of Broadcast listeners.
ii. Create an Android application to create and use
19 5/2/24
services.
I. Create an Android application to demonstrate
6. XML based animation
ii. Create an Android application to display canvas
22 12/3/24
and allow the user to draw on it.
Create a media player application in android that
7.
plays audio. Implement play, pause, and loop 29 14/3/24
features.
8. Create an Android application to use a camera and
capture image/video and display them on the screen.
34 14/3/24
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A

Practical no 1
Aim: - i. Write a program using Kotlin to implement control structures
and loops. ii. Write a program to implement object-oriented concepts in
Kotlin.
Program 1: - Write a program using Kotlin to implement control
structures and loops.
fun main() {
//Example of if-else statement
val number = 15

if (number % 2 == 0) {
println("$number is even.")
} else {
println("$number is odd.")
}

//Example of a for loop


println("Printing numbers from 1 to 5 using a for loop:")
for (i in 1..5) {
println(i)
}

//Example of a while loop


var countdown = 3
println("countdown using a while loop:")
while (countdown > 0) {
println(countdown)
countdown--
}

//Example of a when expression


val dayOfWeek = 4

val dayString = when (dayOfWeek) {


1 -> "Monday"
2 -> "Tuesday"
3 -> "Wednesday"
4 -> "Thursday"
5 -> "Friday"
6 -> "Saturday"
7 -> "Sunday"
else -> "Invalid day"
}
println("Day of the week is $dayString")
}

1
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A

Output: -

Program 2: - Write a program to implement object-oriented concepts in


Kotlin.
// Parent class
open class Animal(val name: String, val age: Int) {
// Function in the parent class
open fun makeSound() {
println("$name makes a generic animal sound.")
}
}
// Subclass inheriting from Animal
class Dog(name: String, age: Int, val breed: String) : Animal(name, age) {
// Overriding the makeSound() function
override fun makeSound() {
println("$name barks loudly!")
}
// Function specific to Dog class
fun fetch() {
println("$name is fetching the ball.")
}
}
// Subclass inheriting from Animal
class Cat(name: String, age: Int, val color: String) : Animal(name, age) {
override fun makeSound() {
println("$name meows softly.")
}
// Function specific to Cat class

2
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
fun scratch() {
println("$name is scratching the furniture.")
}
}
fun main() {
// Creating objects of the classes
val dog = Dog(name = "Buddy", age = 3, breed = "Golden Retriever")
val cat = Cat(name = "Whiskers", age = 2, color = "Tabby")
// Calling methods on the objects
println("${dog.name} is a ${dog.breed} aged ${dog.age} years")
dog.makeSound() // This will print "Buddy barks loudly!"

dog.fetch() // This will print "Buddy is fetching the ball."


println("${cat.name} is a ${cat.color} cat aged ${cat.age} years")
cat.makeSound() // This will print "Whiskers meows softly."
cat.scratch() // This will print "Whiskers is scratching the furniture."
}

Output: -

3
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A

Practical no 2
Aim: -I. Create an Android application to design screens using different
layouts and UI including Button, Edit text, Text view, Radio Button etc.
ii. Write an android application demonstrating response to event/user
interaction for
a. Checkbox
b. Radio button
c. Button
d. Spinner

Program: -
activity_main_xml file: -

<?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

4
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
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>
<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"

5
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>

Main Activity.kt file: -


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
import com.example.pract2.R

class MainActivity: AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle ? ) {
super.onCreate(savedInstanceState)
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) {

6
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
CodingLang.text = "You love Python"
} else if (CheckB2.isChecked) {
CodingLang.text = "You love Kotlin"
} else {
CodingLang.text = "You don't like these languages"
}}}}
Output:

7
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A

Practical no 3
Aim: -I. Create an application to create Image Flipper and Image Gallery.
On click on the image display the information about the image.
ii. Create an application to use Gridview for shopping cart application.
Program: -
Activitymain.kt file: -

<?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/cat1"
tools:ignore="ContentDescription" />

<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/cat2"
tools:ignore="ContentDescription" />

<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/cat3"
tools:ignore="ContentDescription" />

</ViewFlipper>

8
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="350dp"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:layout_margin="10dp"
app:srcCompat="@drawable/cat1"
tools:ignore="ContentDescription" />

<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="1dp"
android:id="@+id/horizontal"
tools:ignore="ContentDescription">

<LinearLayout
android:id="@+id/imgContainer"
android:layout_width="match_parent"
android:layout_height="90dp"
android:orientation="horizontal"></LinearLayout>

</HorizontalScrollView>

</LinearLayout>

Mainactivity.kt file: -

package com.example.pract3
import android.os.Bundle
import android.widget.ImageView
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {


private var images = intArrayOf(
R.drawable.cat1,
R.drawable.cat2,
R.drawable.cat3,
R.drawable.cat1,
R.drawable.cat2,
R.drawable.cat3,
R.drawable.cat1,
R.drawable.cat2,
R.drawable.cat3,
R.drawable.cat1,

9
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
R.drawable.cat2,
R.drawable.cat3,
)

override fun onCreate(savedInstanceState: Bundle?) {


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

val imgContainer = findViewById<LinearLayout>(R.id.imgContainer)

for (i in 0 until images.size) {


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])
}
imgContainer.addView(imageView)
}
}
}

Output:

10
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
Program :2 Create an application to use Gridview for shopping cart
application.
<?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">
<GridView
android:id="@+id/gridCont"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:listSelector="#00000000"
android:numColumns="3"
android:padding="6dp"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
tools:listitem="@layout/gridframe" />
</LinearLayout><?xml version="1.0" encoding="utf-8"?>
gridframe:
<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="match_parent"
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"

11
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
android:src="@drawable/k"/>
<TextView
android:id="@+id/txtData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="4dp"
android:text="Test"
android:textAlignment="center"/>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
GridAdapter:
package com.example.gridview
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
class GridAdapter(
var context: Context,
var arratList: ArrayList<Griditem>
):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, convertView: 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: Griditem = arratList.get(position)
icons.setImageResource(gridItem.icons!!)
names.text = gridItem.name
return view
}
}package com.example.gridview
class Griditem {
var icons: Int? = 0
var name:String? = null
constructor(icons:Int?,name: String){

12
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
this.icons = icons
this.name = name
}
}package com.example.gridview
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.GridView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity(),AdapterView.OnItemClickListener {
private var gridView:GridView? = null
private var arrayList:ArrayList<Griditem>? = null
private var gridAdapter:GridAdapter? = null
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<Griditem> {
val arrayList:ArrayList<Griditem> = ArrayList()
arrayList.add(Griditem(R.drawable.k,"K"))
arrayList.add(Griditem(R.drawable.k2,"K2"))
arrayList.add(Griditem(R.drawable.k3,"K3"))
arrayList.add(Griditem(R.drawable.k4,"K4"))
arrayList.add(Griditem(R.drawable.k5,"K5"))
arrayList.add(Griditem(R.drawable.k6,"K6"))
arrayList.add(Griditem(R.drawable.k7,"K7"))
arrayList.add(Griditem(R.drawable.k8,"K8"))
arrayList.add(Griditem(R.drawable.k9,"K9"))
arrayList.add(Griditem(R.drawable.k3,"K3"))
arrayList.add(Griditem(R.drawable.k6,"K6"))
arrayList.add(Griditem(R.drawable.k9,"K9"))
arrayList.add(Griditem(R.drawable.k3,"K3"))
arrayList.add(Griditem(R.drawable.k6,"K6"))
arrayList.add(Griditem(R.drawable.k,"K"))
return arrayList
}

13
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long)
{
val griditem:Griditem = arrayList!![position]
Toast.makeText(this,griditem.name,Toast.LENGTH_SHORT).show()
}
}
Output:

14
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A

Practical No. 4
Aim: I. Create an Android application to demonstrate implicit
and explicit intents. ii. Create an application to demonstrate
shared preferences
 MainActivity.kt
package com.example.practical4ab

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.view.View
import android.widget.*
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)

val impbtn=findViewById<Button>(R.id.implicitBtn)
val expbtn=findViewById<Button>(R.id.explicitBtn)
val txtName=findViewById<EditText>(R.id.name)

val sharedPref=getSharedPreferences("Sahil's App",Context.MODE_PRIVATE)


var sharedEdit=sharedPref.edit()

impbtn.setOnClickListener{
startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://www.google.com/")
)
)
}

expbtn.setOnClickListener(object:View.OnClickListener{
override fun onClick(view:View?){
if((txtName.text)?.isEmpty()==true)
{

15
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
Toast.makeText(this@MainActivity,"Please enter
name:",Toast.LENGTH_SHORT).show()
}else{
sharedEdit.putString("name",txtName.text.toString())
sharedEdit.commit()
}
startActivity(Intent(this@MainActivity,data::class.java))
}
})
}
}

 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/explicitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Explicit" />
<Button
android:id="@+id/implicitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Implicit" />
</LinearLayout>
 data.kt

16
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
package com.example.practical4ab

import android.content.Context
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class data :AppCompatActivity(){


override fun onCreate(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_data)
val pName=findViewById<TextView>(R.id.textView)
val sharedPref=getSharedPreferences("Sahil's App",Context.MODE_PRIVATE)
pName.text=sharedPref.getString("name","Text")
}
}

 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
tools:context=".data">

<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/textView"
android:text="Your name"
android:layout_margin="8dp"
android:textSize="18dp"/>
</LinearLayout>

17
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A

Output:

18
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A

Practical No. 5
Aim: Create an Android application to demonstrate the use of
Broadcast listeners. ii. Create an Android application to create
and use services.
MainActivityXMLFile: -

<?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:text="StartService"/>
<Button
android:id="@+id/stopServBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="StopService"/>
</LinearLayout>

MainactivityFile:-

package com.example.practical5
import android.content.Context
import android.media.MediaPlayer
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

19
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
class MainActivity : AppCompatActivity() {
private var mediaPlayer:MediaPlayer?=null
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)
val
sharedPref=getSharedPreferences("DaApp",Context.MODE_PRIVATE)
val sharedEdit=sharedPref.edit()
startbtn.setOnClickListener {
mediaPlayer=MediaPlayer.create(this@MainActivity, R.raw.music)
mediaPlayer?.start()
}
stopbtn.setOnClickListener {
mediaPlayer?.stop()
mediaPlayer?.release()
mediaPlayer=null
}
}
override fun onStop() {
super.onStop()
mediaPlayer?.release()
mediaPlayer=null
}
}

Audioservice.ktFile:-

package com.example.practical5
import android.app.Service
import android.content.Intent
import android.media.MediaPlayer
import android.os.IBinder

class AndroidService:Service() {
private lateinit var player:MediaPlayer
override fun onStartCommand(intent:Intent?,flags:Int,startId: Int): Int {
player=MediaPlayer.create(this, R.raw.music)

20
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
player.isLooping=true
player.start()
return START_STICKY
}
override fun onDestroy() {
super.onDestroy()
player.stop()
}
override fun onBind(intent:Intent): IBinder? {
return null
}
}

Output:

21
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A

Practical No. 6
Aim: I. Create an Android application to demonstrate XML
based animation
ii. Create an Android application to display canvas and allow the
user to draw on it.
Program

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


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

<ImageView
android:id="@+id/imghu"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/android" />

<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" />

<Button
android:id="@+id/btn3"

22
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="Draw" />
</LinearLayout>

(activity_draw.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:gravity="center"
android:orientation="vertical"
tools:context=".DrawActivity">

<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"
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"
android:importantForAccessibility="no" />

<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"

23
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
android:importantForAccessibility="no" />

<ImageButton
android:id="@+id/btn_brush"
android:layout_width="100dp"
android:layout_height="match_parent"
app:srcCompat="@drawable/baseline_brush_24"
android:importantForAccessibility="no" />

<ImageButton
android:id="@+id/btn_clearscreen"
android:layout_width="100dp"
android:layout_height="match_parent"
app:srcCompat="@drawable/baseline_clear_24"
android:importantForAccessibility="no" />
</LinearLayout>
</LinearLayout>

(MainActivity.kt) package com.example.pract

import android.content.Intent
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)
val 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)
}
findViewById<Button>(R.id.btn3).setOnClickListener {

24
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
startActivity(Intent(this, DrawActivity::class.java))
}
}
}

(DrawActivity.kt) package
com.example.pract

import android.os.Bundle
import android.widget.ImageButton
import androidx.appcompat.app.AppCompatActivity
import com.mihir.drawingcanvas.drawingView

class DrawActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_draw)

val drawingView = findViewById<drawingView>(R.id.drawing_view) ?: return


findViewById<ImageButton>(R.id.btn_undo)?.setOnClickListener {
drawingView.undo() }
findViewById<ImageButton>(R.id.btn_redo)?.setOnClickListener {
drawingView.redo() }
findViewById<ImageButton>(R.id.btn_brush)?.setOnClickListener {
drawingView.setSizeForBrush(25) //0-35
drawingView.setBrushAlpha(100) //0-255
drawingView.setBrushColor(R.color.black)
}
findViewById<ImageButton>(R.id.btn_clearscreen)?.setOnClickListener {
drawingView.clearDrawingBoard()
}
}
}

(settings.gradle.kts)
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal()
maven { url = uri("https://www.jitpack.io" ) }
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url = uri("https://www.jitpack.io" ) }

25
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
}
}

rootProject.name = "pract"
include(":app")

Build.gradle.kts(module:app)
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}

android {
namespace = "com.example.pract"
compileSdk = 34

defaultConfig {
applicationId = "com.example.pract"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
}

dependencies {

implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.11.0")

26
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")

implementation("com.github.Miihir79:DrawingCanvas:1.1.2")
}

(blink.xml)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="600"
android:fromAlpha="0.0"
android:interpolator="@android:anim/accelerate_interpolator"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toAlpha="1.0" />
</set>

(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>

(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>

27
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A

Output:

28
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A

Practical No. 7
Aim: Create a media player application in android that plays
audio. Implement play, pause, and loop features.
(activitymain.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/image"/>
<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"

29
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
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>

(mainactivity.kt)
package com.example.prac7

import android.media.MediaPlayer
import android.os.Bundle
import android.os.Handler
import android.widget.Button
import android.widget.SeekBar
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

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)

30
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
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.music)
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 {

31
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
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:

32
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A

33
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A

Practical No. 8
Aim: Create an Android application to use a camera and capture
image/video and display them on the screen.
<?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"
android:text="Camera" />
</LinearLayout>

package com.example.prac8

import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity

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)

34
SATISH PRADHAN DNYANASADHANA COLLEGE, THANE [A.Y. 2023-24]
Name : Sahil Ramjan Saiyad Class: SY. B.Sc. Computer Science
Subject: Android Application Development- Practical Roll no:52, Batch :B, Div :A
}
}

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:

35

You might also like