AMP 97
AMP 97
Practical 1
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.
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 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.
Source Code:-
[Link]
package [Link]
import [Link]
import [Link]
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]>
Output:-
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.
Source Code:-
[Link]
package [Link]
import [Link]
import [Link]
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]>
Output:-
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]
[Link]
package [Link]
import [Link]
import [Link]
[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"/>
</[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]>
Output:-
Practical 4a
Aim:- Linear Layout
Source Code:-
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
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>
Output:-
Practical 4b
Aim:- Relative Layout
Source Code:-
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
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>
Output:-
Practical 4c
Aim:- Table Layout
Source Code:-
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link].activity_main.*
[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>
id ‘kotlin-android-extensions’
Output:-
Practical 4d
Aim:- Frame Layout
Source Code:-
[Link]
package [Link]
import [Link]
import [Link]
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>
Output:-
Practical 4e
Aim:- List View
Source Code:-
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link].activity_main.*
[Link]
package [Link]
import [Link]
import [Link]
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>
[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>
id ‘kotlin-android-extensions’
Output:-
Practical 5
Aim:- Design App With UI.
Source Code:-
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
[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>
Output:-
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.*
.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>
id ‘kotlin-android-extensions’
Output:-
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.*
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>
id ‘kotlin-android-extensions’
Output:-
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.*
[Link]
package [Link]
class Food {
var name: String? = null
var description: String? = null
var image: Int? = null
}
[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.*
[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 "gateway animal for urban
farmers". 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
@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
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>
id ‘kotlin-android-extensions’
Output:-
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]
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
</manifest>
Output:-
Practical 9
Aim:- Database Programming with SQLite
Source Code:-
[Link]
package [Link]
import [Link]
object DBContract {
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link].activity_main.*
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]
@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
}
if (cursor!!.moveToFirst()) {
while ([Link] == false) {
name =
[Link]([Link]([Link].COLUMN_NA
ME))
age =
[Link]([Link]([Link].COLUMN_AG
E))
age =
[Link]([Link]([Link].COLUMN_AG
E))
companion object {
// If you change the database schema, you must increment the database
version.
val DATABASE_VERSION = 1
val DATABASE_NAME = "[Link]"
}
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>
id ‘kotlin-android-extensions’
Output:-
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]
[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]
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"
/>
</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>
id ‘kotlin-android-extensions’
Output:-