0% found this document useful (0 votes)
2 views92 pages

AMP 97

The document outlines practical exercises for an Advanced Mobile Programming course, focusing on Android application development using Android Studio. It includes step-by-step instructions for creating projects, implementing Android components, and utilizing layouts such as Linear, Relative, and Table Layouts. Each practical section features source code examples and aims to familiarize students with Android programming fundamentals and UI design.

Uploaded by

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

AMP 97

The document outlines practical exercises for an Advanced Mobile Programming course, focusing on Android application development using Android Studio. It includes step-by-step instructions for creating projects, implementing Android components, and utilizing layouts such as Linear, Relative, and Table Layouts. Each practical section features source code examples and aims to familiarize students with Android programming fundamentals and UI design.

Uploaded by

Nitish Pandey
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[Link].I.

T Sem VI Advanced Mobile Programming

Practical 1

Aim:- Introduction to Android, Introduction to Android Studio IDE, Application


Fundamentals: Creating a Project, Android Components, Activities, Services,
Content Providers, Broadcast Receivers, Interface overview, Creating Android
Virtual device, USB debugging mode, Android Application Overview. Simple
“Hello World” program.

Steps:-
Step1:- Create a new project and select empty activity.

Step 2:- Give the name for your project, select where you want to save the
project, select the language to be Kotlin and don’t change the minimum SDK
let it be the default and then click finish.
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

Step 3:- You get two files one as your [Link] file for writing code logic
and activity_main.xml for designing the app.

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Step 4:- For the execution of the code you need something called as Android
Virtual Device(AVD) which can be created by going to File menu -> Tools ->
Select Device Manager or AVD Manager.

Step 5:- Click on + icon in Device Manager to create an AVD

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Step 6:- Select a device according to your choice and click on next.

Step 7:- Name your device if you want and select what type of orientation
should be available on startup.

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Source Code:-
[Link]
package [Link]

import [Link]
import [Link]

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</[Link]>

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Output:-

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Practical 2
Aim:- Android Resources: (Color, Theme, String, Drawable, Dimension,
Image).

Steps:-
Step 1:- The xml files for color, theme, & string can be found in res folder as
well as the drawable folder for images.

[Link]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
</resources>

[Link]
<resources>
<string name="app_name">Practical</string>
</resources>

[Link]
<resources xmlns:tools="[Link]
<style name="[Link]"
parent="[Link]">
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

</style>
<style name="[Link]" parent="[Link]" />
</resources>

Step 2:- For importing image you have to copy the image and paste it in the
drawable folder.

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Source Code:-
[Link]
package [Link]

import [Link]
import [Link]

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/a">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chris Quadros"
android:textColor="#ecd049"
android:textColorHint="#ff0909"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</[Link]>

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Output:-

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Practical 3
Aim:- Activity Life Cycle, Activity methods, Multiple Activities, Life Cycle of
fragments and multiple fragments.

Source Code:-
[Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

class MainActivity : AppCompatActivity() {


internal var context: Context?=null
val TAG = "Activity Status"
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
context=this@MainActivity
val btnNextActivity= findViewById([Link].btn_next_activity) as Button
[Link]{ view->
val intent = Intent(this, MainActivity2::[Link])
startActivity(intent);
finish();
Log.i(TAG,"onCreate")
}
}

override fun onStart() {


[Link]()
Log.i(TAG,"onStart")
}

override fun onResume() {


[Link]()
Log.i(TAG,"onResume")
}
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

override fun onPause() {


[Link]()
Log.i(TAG,"onPause")
}

override fun onStop() {


[Link]()
Log.i(TAG,"onResume")
}

override fun onRestart() {


[Link]()
Log.i(TAG,"onRestart")
}

override fun onDestroy() {


[Link]()
Log.i(TAG,"onDestroy")
}

override fun onSaveInstanceState(outState: Bundle) {


[Link](outState)
Log.i(TAG,"onSaveInstanceState")
}

override fun onRestoreInstanceState(savedInstanceState: Bundle) {


[Link](savedInstanceState)
Log.i(TAG,"onRestoreInstanceState")
}
}

[Link]
package [Link]

import [Link]
import [Link]

class MainActivity2 : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

[Link](savedInstanceState)
setContentView([Link].activity_main2)
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:text="Android Activity Life Cycle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="146dp"
android:layout_marginRight="142dp"
android:textSize="30sp"
android:textStyle="bold"
android:gravity="top|center_horizontal"
android:layout_gravity="fill"/>

<Button
android:id="@+id/btn_next_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Jump To Next Activities"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

</[Link]>

activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">

</[Link]>

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Output:-

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Practical 4a
Aim:- Linear Layout
Source Code:-
[Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
val btn =findViewById<View>([Link]) as Button
[Link] { view ->
[Link](
view,"Linear Layout Demo",
Snackbar.LENGTH_LONG
)
.setAction("LinearLayout",null).show()
}
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

android:text="Email"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="Login"/>

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="149dp"
android:layout_height="54dp"
android:text="Home"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:layout_width="112dp"
android:layout_height="51dp"
android:text="About"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

</LinearLayout>

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Output:-

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Practical 4b
Aim:- Relative Layout

Source Code:-
[Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
val btn =findViewById<View>([Link]) as Button
[Link] { view ->
[Link](
view,"Relative Layout Demo",
Snackbar.LENGTH_LONG
)
.setAction("RelativeLayout",null).show()
}
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:id="@+id/Label"
android:layout_width="wrap_content"
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

android:layout_height="wrap_content"
android:text="Email"
android:textSize="30dp" />

<EditText
android:id="@+id/InputEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/Label" />

<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_below="@id/InputEmail"
android:layout_alignParentLeft="true"
android:layout_marginRight="10px"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_toRightOf="@id/btnLogin"
android:layout_alignTop="@id/btnLogin"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register New Account"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>

</RelativeLayout>

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Output:-

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Practical 4c
Aim:- Table Layout

Source Code:-
[Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link].activity_main.*

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
[Link] {
Log.e("1","1");
}
[Link] {
Log.e("2","2");
}
[Link] {
Log.e("3","3");
}
[Link] {
Log.e("4","4");
}
[Link] {
Log.e("5","5");
}
[Link] {
Log.e("6","6");
}
[Link] {
Log.e("7","7");
}
[Link] {
Log.e("8","8");
}
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

[Link] {
Log.e("9","9");
}
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="150dp">

<TableRow>
<Button
android:text="1"
android:id="@+id/b1"
android:layout_gravity="center"/>
<Button
android:text="2"
android:id="@+id/b2"
android:layout_gravity="center"/>
<Button
android:text="3"
android:id="@+id/b3"
android:layout_gravity="center"/>
</TableRow>

<TableRow>
<Button
android:text="4"
android:id="@+id/b4"
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

android:layout_gravity="center"/>
<Button
android:text="5"
android:id="@+id/b5"
android:layout_gravity="center"/>
<Button
android:text="6"
android:id="@+id/b6"
android:layout_gravity="center"/>
</TableRow>

<TableRow>
<Button
android:text="7"
android:id="@+id/b7"
android:layout_gravity="center"/>
<Button
android:text="8"
android:id="@+id/b8"
android:layout_gravity="center"/>
<Button
android:text="9"
android:id="@+id/b9"
android:layout_gravity="center"/>
</TableRow>

</TableLayout>
</LinearLayout>

Note:- In gradle add the following line in plugins.

id ‘kotlin-android-extensions’

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Output:-

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Practical 4d
Aim:- Frame Layout

Source Code:-
[Link]
package [Link]

import [Link]
import [Link]

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/kira"
android:layout_gravity="center_vertical|center"
android:id="@+id/imageView"/>

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:layout_margin="10dp"
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

android:text="I'm KIRA and I'm going tobe be the GOD if this World"
android:textColor="#27BD2D"
android:textSize="36dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="Death Note"
android:layout_gravity="bottom|center"
android:layout_marginTop="80dp"/>
</FrameLayout>

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Output:-

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Practical 4e
Aim:- List View

Source Code:-
[Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link].activity_main.*

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
[Link] {
val intent=Intent(this,MainActivity2::[Link])
startActivity(intent)
}
}
}

[Link]
package [Link]

import [Link]
import [Link]

class MainActivity2 : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main2)
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">

<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Friendship List"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
android:layout_marginTop="200dp"
android:textSize="30dp"
android:id="@+id/b1"/>

</LinearLayout>

activity_main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">

<ListView
xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:entries="@array/Tomadachi">
</ListView>

</LinearLayout>

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

[Link]
<resources>
<string name="app_name">prac4e</string>
<string-array name="Tomadachi">
<item>Sumedha</item>
<item>Chris</item>
<item>Ipsii</item>
<item>Pratik</item>
<item>Darshan</item>
<item>Prajwal</item>
<item>Jeni</item>
<item>Praneeta</item>
</string-array>
</resources>

Note:- In gradle add the following line in plugins.

id ‘kotlin-android-extensions’

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Output:-

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Practical 5
Aim:- Design App With UI.

Source Code:-
[Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)

var uname=findViewById([Link]) as EditText


var pass=findViewById([Link]) as EditText
var res=findViewById([Link].btn_res) as Button
var sub=findViewById([Link].btn_sub) as Button

[Link] {
[Link]("")
[Link]("")
}

[Link] {
val user=[Link];
val passwd=[Link]

[Link](this@MainActivity,user,Toast.LENGTH_LONG).show()
}
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="25dp"
android:background="#0D2EE1"
android:gravity="center">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:padding="30dp"
android:textSize="25dp"
android:textColor="#E6FD05"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/username"
android:hint="Enter the username"
android:textColor="#6bfff7"
android:textColorHint="#52afaa"
android:textAlignment="center"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/password"
android:hint="Enter the Password"
android:inputType="textPassword"
android:textColor="#6bfff7"
android:textColorHint="#52afaa"
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

android:textAlignment="center"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="30dp"
android:background="#0D2EE1">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_res"
android:text="Reset"
android:textAllCaps="false"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_sub"
android:text="Submit"
android:textAllCaps="false"/>

</LinearLayout>

</LinearLayout>

</LinearLayout>

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Output:-

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Practical 6a
Aim:- Alert dialog box, & dialog fragments.

Source Code:-
[Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link].activity_main.*

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
[Link] {
val dialogBuilder=[Link](this)
[Link]("Do you want to close this application")
.setCancelable(false)

.setPositiveButton("Proceed",[Link]{dialog,id-
>finish()})

.setNegativeButton("Cancel",[Link]{dialog,id-
>[Link]()})
val alert=[Link]()
[Link]("Alert Dialog Example")
[Link]()
}
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Show Alert"
android:gravity="center_vertical"/>

</LinearLayout>

Note:- In gradle add the following line in plugins.

id ‘kotlin-android-extensions’

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Output:-

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Practical 6b
Aim:- Programming menus(Cut, Copy, Paste, New).

Source Code:-
[Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link].activity_main.*

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
val actionBar=supportActionBar
actionBar!!.title="Kotlin"
[Link]="Menu Kotlin Examples"
[Link]=4.0F
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {


val inflater=menuInflater
[Link]([Link].toolbar_menu,menu)
return [Link](menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {


when([Link]){
[Link].action_cut->{
text_view.text="Cut"
return true
}
[Link].action_cut->{
text_view.text="Copy"
return true
}
[Link].action_cut->{
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

text_view.text="Paste"
return true
}
[Link].action_cut->{
text_view.text="New"
return true
}
}
return [Link](item)
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/root_layout"
android:background="#fee5">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tap an item from action bar menu"

android:textAppearance="@style/[Link]"
android:padding="25dp"
android:textColor="#168ace"
android:textStyle="bold"
android:layout_gravity="center"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

</LinearLayout>

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

toolbar_menu.xml(create inside menu folder)


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="[Link]
<item
android:id="@+id/action_cut"
android:title="Cut"
android:icon="@drawable/sam"/>
<item
android:id="@+id/action_copy"
android:title="Copy"/>
<item
android:id="@+id/action_paste"
android:title="Paste"/>
<item
android:id="@+id/action_new"
android:title="New"/>
</menu>

Note:- In gradle add the following line in plugins.

id ‘kotlin-android-extensions’

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Output:-

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Practical 7
Aim:- Programs on Intents, Events Listeners and Adapters.

Source Code:-
Activity_food_details.kt
package [Link]

import [Link]
import [Link]
import [Link]
import [Link].activity_food_details.*

class Activity_food_details : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_food_details)
val bundle = [Link]
[Link](bundle!!.getInt("image"))
[Link] = [Link]("name")
[Link] = [Link]("description")
}
}

[Link]
package [Link]

class Food {
var name: String? = null
var description: String? = null
var image: Int? = null

constructor(name: String, description: String, image: Int) {


[Link] = name
[Link] = description
[Link] = image
}

}
[Link]
package [Link]
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link].activity_main.*
import [Link].food_entry.view.*

class MainActivity : AppCompatActivity() {


var adapter:FoodAdapter? = null
var foodsList = ArrayList<Food>()
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
[Link](
Food(
"Coffee",
"Coffee preparation is the process of turning coffee beans into a
beverage. While the particular steps vary with the type of coffee and with the
raw materials, the process includes four basic steps; raw coffee beans must be
roasted, the roasted coffee beans must then be ground, the ground coffee must
then be mixed with hot water for a certain time (brewed), and finally the liquid
coffee must be separated from the used grounds.",
[Link]
)
)
[Link](
Food(
"Espersso",
"Espresso’s authentic formula is clear and basic, its proper execution a
matter of training, experience and natural talent. A jet of hot water at 88°-93°C
(190°-200°F) passes under a pressure of nine or more atmospheres through a
seven-gram (.25 oz) cake-like layer of ground and tamped coffee. Done right,
the result is a concentrate of not more than 30 ml (one oz) of pure sensorial
pleasure.",
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

[Link]
)
)
[Link](
Food(
"French Fires",
"Heat a few inches of vegetable oil to 300 degrees F in a heavy pot. In
3 or 4 batches, fry the potatoes about 4 to 5 minutes per batch, or until soft.
They should not be brown at all at this point-you just want to start the cooking
process. Remove each batch and drain them on new, dry paper towels.",
[Link]
)
)
[Link](
Food(
"Honey",
"While it is less likely that anyone would do this on their own if they
are not a beekeeper, this might be useful for those who aspire to become one.
Bees are really great and easy to keep, even in the urban environment! As
Novella Carpenter calls them, bees are &quot;gateway animal for urban
farmers&quot;. All you need is some space in the backyard/deck. The process of
honey harvesting and extraction most likely happens on a separate days.",
[Link]
)
)
[Link](
Food(
"Strawberry",
"Preparation. Coarsely mash strawberries with sugar, lemon juice, and
salt using a potato masher in a large bowl. Let stand, stirring and mashing
occasionally, 10 minutes. Transfer half of strawberry mixture to a blender and
purée with cream until smooth. Freeze mixture in ice cream maker.",
[Link]
)
)
[Link](
Food(
"Sugar cubes",
"Sugar cubes are extremely simple to make at home - all you need is
sugar and water. In addition to standard cubes, you can add color and flavor to
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

add fun flair to a tea party or another gathering. Learn how to make sugar cubes
using two different methods: using a pan in the oven or an ice cube tray you
leave out overnight.",
[Link]
)
)
adapter = FoodAdapter(this, foodsList)
[Link]=adapter

} class FoodAdapter : BaseAdapter {


var foodsList = ArrayList<Food>()
var context: Context? = null

constructor(context: Context, foodsList: ArrayList<Food>) : super() {


[Link] = context
[Link] = foodsList
}

override fun getCount(): Int {


return [Link]
}

override fun getItem(position: Int): Any {


return foodsList[position]
}

override fun getItemId(position: Int): Long {


return [Link]()
}

@SuppressLint("ViewHolder")
override fun getView(position: Int, convertView: View?, parent:
ViewGroup?): View {
val food = [Link][position]

var inflator =
context!!.getSystemService(LAYOUT_INFLATER_SERVICE) as
LayoutInflater
var foodView = [Link]([Link].food_entry, null)
[Link] {
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

val intent = Intent(context, Activity_food_details::[Link])


[Link]("name", [Link]!!)
[Link]("description", [Link]!!)
[Link]("image", [Link]!!)
context!!.startActivity(intent)
}
[Link]([Link]!!)
[Link] = [Link]!!

return foodView
}
}
}

activity_food_details.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity_food_details">

<ImageView
android:id="@+id/imgFoodDetails"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="30dp"
app:srcCompat="@drawable/strawberryicecream"
android:layout_gravity="center"/>

<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="TextView"
android:textColor="@color/purple_200"
android:textSize="24sp" android:layout_gravity="top|center"
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

tools:ignore="HardcodedText" />

<TextView
android:id="@+id/tvDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="TextView"
android:layout_gravity="fill_horizontal|center"
tools:ignore="HardcodedText" />

</LinearLayout>

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<!--suppress ALL -->
<LinearLayout
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gvFoods"
android:columnWidth="150dp"
android:horizontalSpacing="15dp"
android:numColumns="auto_fit"
android:verticalSpacing="15dp"/>

</LinearLayout>

food_entries.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

android:layout_height="match_parent"
android:background="#ddd"
android:gravity="center"
android:padding="15dp">

<ImageView
android:layout_width="150dp"
android:layout_height="120dp"
android:id="@+id/imgFood"
android:src="@drawable/frenchfries"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tvName"
android:gravity="center"
android:text="FrenchFries"
android:textSize="20sp"
tools:ignore="HardcodedText" />

</LinearLayout>

Note:- In gradle add the following line in plugins.

id ‘kotlin-android-extensions’

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Output:-

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Practical 8
Aim:- Programs on Services, notification and broadcast receivers.

Source Code:-
[Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

class Appstates {
internal lateinit var connectivityManager: ConnectivityManager
internal var wifiInfo:NetworkInfo?=null
internal var mobileInfo:NetworkInfo?=null
internal var connected=false

val isOnline:Boolean
get(){
try {

connectivityManager=[Link](Context.CONNECTIVITY_S
ERVICE) as ConnectivityManager
val networkInfo=[Link]

connected=networkInfo!=null&&[Link]&&[Link]
nnected
return connected
}catch (e:Exception){
println("CheckConnectivity Exception"+[Link])
Log.v("connectivty",[Link]())
}
return connected
}
companion object{
internal lateinit var context:Context
private val instant=Appstates()
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

fun getInstance(ctx:Context):Appstates{
context=[Link]
return instant
}
}
}

[Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

class MainActivity : AppCompatActivity() {


private var button:Button?=null
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)

button=findViewById<View>([Link])as Button
button!!.setOnClickListener([Link] {
if ([Link](applicationContext).isOnline){
[Link](applicationContext,"Connected to Wifi or mobile
network",Toast.LENGTH_SHORT).show()
}
else{
[Link](applicationContext,"No internet
connection",Toast.LENGTH_SHORT).show()
}
})
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/checkInternet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@color/cardView_dark_background"
android:text="Check Connection"
android:textColor="#fff"/>

</RelativeLayout>

[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
xmlns:tools="[Link]
package="[Link]">
<uses-permission android:name="[Link]"/>
<uses-permission
android:name="[Link].ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/[Link]"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

<category android:name="[Link]" />


</intent-filter>
</activity>
</application>

</manifest>

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Output:-

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Practical 9
Aim:- Database Programming with SQLite

Source Code:-
[Link]
package [Link]

import [Link]

object DBContract {

/* Inner class that defines the table contents */


class UserEntry : BaseColumns {
companion object {
val TABLE_NAME = "users"
val COLUMN_USER_ID = "userid"
val COLUMN_NAME = "name"
val COLUMN_AGE = "age"
}
}
}

[Link]
package [Link]

import [Link]
import [Link]

import [Link]
import [Link]
import [Link].activity_main.*

class MainActivity : AppCompatActivity() {


lateinit var usersDBHelper : UsersDBHelper
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
usersDBHelper = UsersDBHelper(context = this)
}

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

fun addUser(v:View){
var userid = this.edittext_userid.[Link]()
var name = this.edittext_name.[Link]()
var age = this.edittext_age.[Link]()
var result = [Link](UserModel(userid = userid,name =
name,age = age))
//clear all edittext s
this.edittext_age.setText("")
this.edittext_name.setText("")
this.edittext_userid.setText("")
this.textview_result.text = "Added user : "+result
this.ll_entries.removeAllViews()
}

fun deleteUser(v:View){
var userid = this.edittext_userid.[Link]()
val result = [Link](userid)
this.textview_result.text = "Deleted user : "+result
this.ll_entries.removeAllViews()
}

fun showAllUsers(v:View){
var users = [Link]()
this.ll_entries.removeAllViews()
[Link] {
var tv_user = TextView(this)
tv_user.textSize = 30F
tv_user.text = [Link]() + " - " + [Link]()
this.ll_entries.addView(tv_user)
}
this.textview_result.text = "Fetched " + [Link] + " users"
}
}

[Link]
package [Link]

class UserModel(val userid: String, val name: String, val age: String)

[Link]
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

import [Link]

class UsersDBHelper(context: Context) : SQLiteOpenHelper(context,


DATABASE_NAME, null, DATABASE_VERSION) {
override fun onCreate(db: SQLiteDatabase) {
[Link](SQL_CREATE_ENTRIES)
}

override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion:


Int) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
[Link](SQL_DELETE_ENTRIES)
onCreate(db)
}

override fun onDowngrade(db: SQLiteDatabase, oldVersion: Int, newVersion:


Int) {
onUpgrade(db, oldVersion, newVersion)
}

@Throws(SQLiteConstraintException::class)
fun insertUser(user: UserModel): Boolean {
// Gets the data repository in write mode
val db = writableDatabase

// Create a new map of values, where column names are the keys
val values = ContentValues()
[Link]([Link].COLUMN_USER_ID, [Link])
[Link]([Link].COLUMN_NAME, [Link])
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

[Link]([Link].COLUMN_AGE, [Link])

// Insert the new row, returning the primary key value of the new row
val newRowId = [Link]([Link].TABLE_NAME, null,
values)

return true
}

@Throws(SQLiteConstraintException::class)
fun deleteUser(userid: String): Boolean {
// Gets the data repository in write mode
val db = writableDatabase
// Define 'where' part of query.
val selection = [Link].COLUMN_USER_ID + " LIKE ?"
// Specify arguments in placeholder order.
val selectionArgs = arrayOf(userid)
// Issue SQL statement.
[Link]([Link].TABLE_NAME, selection,
selectionArgs)

return true
}

fun readUser(userid: String): ArrayList<UserModel> {


val users = ArrayList<UserModel>()
val db = writableDatabase
var cursor: Cursor? = null
try {
cursor = [Link]("select * from " +
[Link].TABLE_NAME + " WHERE " +
[Link].COLUMN_USER_ID + "='" + userid + "'", null)
} catch (e: SQLiteException) {
// if table not yet present, create it
[Link](SQL_CREATE_ENTRIES)
return ArrayList()
}

var name: String


var age: String
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

if (cursor!!.moveToFirst()) {
while ([Link] == false) {
name =
[Link]([Link]([Link].COLUMN_NA
ME))
age =
[Link]([Link]([Link].COLUMN_AG
E))

[Link](UserModel(userid, name, age))


[Link]()
}
}
return users
}

fun readAllUsers(): ArrayList<UserModel> {


val users = ArrayList<UserModel>()
val db = writableDatabase
var cursor: Cursor? = null
try {
cursor = [Link]("select * from " +
[Link].TABLE_NAME, null)
} catch (e: SQLiteException) {
[Link](SQL_CREATE_ENTRIES)
return ArrayList()
}

var userid: String


var name: String
var age: String
if (cursor!!.moveToFirst()) {
while ([Link] == false) {
userid =
[Link]([Link]([Link].COLUMN_US
ER_ID))
name =
[Link]([Link]([Link].COLUMN_NA
ME))

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

age =
[Link]([Link]([Link].COLUMN_AG
E))

[Link](UserModel(userid, name, age))


[Link]()
}
}
return users
}

companion object {
// If you change the database schema, you must increment the database
version.
val DATABASE_VERSION = 1
val DATABASE_NAME = "[Link]"

private val SQL_CREATE_ENTRIES =


"CREATE TABLE " + [Link].TABLE_NAME + " (" +
[Link].COLUMN_USER_ID + " TEXT
PRIMARY KEY," +
[Link].COLUMN_NAME + " TEXT," +
[Link].COLUMN_AGE + " TEXT)"

private val SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " +


[Link].TABLE_NAME
}

}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
xmlns:tools="[Link]
xmlns:app="[Link]
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SQLite Tutorial - User Management"
android:textSize="20dp"
android:padding="10dp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/edittext_userid"
android:hint="User ID"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edittext_name"
android:hint="User Name"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edittext_age"
android:hint="User Age"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_add_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

android:layout_weight="1"
android:onClick="addUser"
android:text="Add" />

<Button
android:id="@+id/button_delete_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="deleteUser"
android:text="Delete" />

<Button
android:id="@+id/button_show_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="showAllUsers"
android:text="Show All" />
</LinearLayout>

<TextView
android:id="@+id/textview_result"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/ll_entries"
android:padding="15dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</LinearLayout>
</LinearLayout>

Note:- In gradle add the following line in plugins.

id ‘kotlin-android-extensions’

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Output:-

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Practical 10
Aim:- Programming Security and permissions.

Source Code:-
[Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link].activity_main.*
import [Link]

class MainActivity : AppCompatActivity() {


private val PermissionsRequestCode=123
private lateinit var managePermissions: ManagePermissions
override fun onCreate(savedInstanceState: Bundle?) {
[Link](savedInstanceState)
setContentView([Link].activity_main)
val list= listOf<String>(
[Link],
[Link].READ_CALENDAR,
[Link].READ_CONTACTS,
[Link].SEND_SMS
)
managePermissions=
ManagePermissions(this,list,PermissionsRequestCode)
[Link]{
if ([Link].SDK_INT >=Build.VERSION_CODES.M)
[Link]()
}
}

override fun onRequestPermissionsResult(


requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
){
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

[Link](requestCode, permissions,
grantResults)

when(requestCode){
PermissionsRequestCode ->{
val
isPermissionsGranted=[Link](requestC
ode,permissions,grantResults)
if(isPermissionsGranted){
toast("Permissions granted.")}
else{
toast("Permissions denied.")
}
return
}
}
}
}

fun [Link](message:String){
[Link](this,message,Toast.LENGTH_SHORT).show()
}

[Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]

class ManagePermissions (val activity:Activity, val list: List<String>, val


code:Int){
fun checkPermissions(){
if (isPermissionsGranted()
!=PackageManager.PERMISSION_GRANTED){
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

showAlert()
} else {
[Link](activity,"Permissions already granted",
Toast.LENGTH_LONG).show()
}
}
private fun isPermissionsGranted(): Int{
var counter=0;
for(permission in list){
counter += [Link](activity,permission)
}
return counter
}
private fun deniedPermission():String{
for (permission in list){
if([Link](activity,permission)==
PackageManager.PERMISSION_DENIED)
return permission
}
return ""
}
private fun showAlert(){
val builder=[Link](activity)
[Link]("Need Permission(s)")
[Link]("Some permissions are required to do the task.")
[Link]("OK",{dialog,which -> requestPermissions()})
[Link]("Cancel",null)
val dialog=[Link]()
[Link]()
}
private fun requestPermissions(){
val permission=deniedPermission()
if
([Link](activity,permission))
{
[Link](activity,"Should show an explanation.",
Toast.LENGTH_LONG).show()
}else{
[Link](activity,[Link](),code)
}
Created By: Ashish Pandey Roll No.: 97
[Link].I.T Sem VI Advanced Mobile Programming

}
fun processPermissionsResult(requestCode: Int, permissions:
Array<String>,grantResults: IntArray):Boolean{
var result=0
if ([Link]()){
for (item in grantResults){
result += item
}
}
if (result==PackageManager.PERMISSION_GRANTED)
return true
return false
}
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Permission Example Demo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:text="Request for Permission"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
/>

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

</LinearLayout>

[Link]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="[Link]
xmlns:tools="[Link]
package="[Link].prac10">
<uses-permission android:name="[Link]"/>
<uses-permission android:name="[Link]"/>
<uses-permission
android:name="[Link].READ_CONTACTS"/>
<uses-permission android:name="[Link].SEND_SMS"/>
<uses-permission
android:name="[Link].READ_CALENDAR"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Prac10"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>

Note:- In gradle add the following line in plugins.

id ‘kotlin-android-extensions’

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Output:-

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97


[Link].I.T Sem VI Advanced Mobile Programming

Created By: Ashish Pandey Roll No.: 97

You might also like