0% found this document useful (0 votes)
9 views56 pages

Android Studio Practical

The document outlines a series of practical exercises focused on Android development, covering topics such as application fundamentals, UI design, activity lifecycle, and various layout types. Each practical includes specific aims, code snippets, and XML layouts for creating simple Android applications. The exercises are structured to progressively build skills in Android programming and application design.
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)
9 views56 pages

Android Studio Practical

The document outlines a series of practical exercises focused on Android development, covering topics such as application fundamentals, UI design, activity lifecycle, and various layout types. Each practical includes specific aims, code snippets, and XML layouts for creating simple Android applications. The exercises are structured to progressively build skills in Android programming and application design.
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

INDEX

Sr Date Practical Name Sign


No

1 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

2
Programming Resources : Android Resources: (Color, Theme,
String, Drawable, Dimension, Image).

3
Programming Activities and Fragments :Activity Life Cycle,
Activity methods, Multiple Activities, Life Cycle of fragments
and multiple fragments.

4
Programs related to different Layouts: Coordinate, Linear,
Relative, Table, Absolute, Frame, List View, Grid View.

5
Programming UI elements : Design App With UI

6
Programming menus, dialog, dialog fragments

7
Programs on Intents, Events Listeners and Adapters

8 Programs on Services, notification and broadcast receivers

9
Database Programming with SQLite

10
Programming Security and permissions

11 Programming Network Communications and Services (JSON)


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.

CODE:

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

<[Link]
xmlns:android="[Link]

xmlns:app="[Link]

xmlns:tools="[Link]

android:id="@+id/main"

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]>
[Link]
package [Link].practical1

import [Link]

import [Link]

import [Link]

import [Link]

import [Link]

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

[Link](savedInstanceState)

enableEdgeToEdge()

setContentView([Link].activity_main)

[Link](findViewById([Link])) {
v, insets ->

val systemBars =
[Link]([Link]())

[Link]([Link], [Link], [Link],


[Link])

insets

}
OUTPUT:
PRACTICAL 2

AIM: Programming Resources : Android Resources: (Color, Theme, String,


Drawable, Dimension, Image).

CODE:

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

<[Link]
xmlns:android="[Link]

xmlns:app="[Link]

xmlns:tools="[Link]

android:id="@+id/main"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

<ImageView

android:id="@+id/imageView"

android:layout_width="1000px"

android:layout_height="40000px"

android:src="@drawable/img"

app:layout_constraintBottom_toBottomOf="parent"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toTopOf="parent"

app:srcCompat="@mipmap/ic_launcher" />

</[Link]>
[Link]
<?xml version="1.0" encoding="utf-8"?>

<resources>

<!--Base application theme. -->

<style name="AppTheme" parent="[Link]">

<!--Customize your theme here. -->

<item name="colorPrimary">@color/colorPrimary</item>

<item name="colorPrimaryDark">@color/colorPrimaryDark</item>

<item name="colorAccent">@color/colorAccent</item>

</style>

</resources>

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

<resources>

<color name="colorPrimary">#008577</color>

<color name="colorPrimaryDark">#00574B</color>

<color name="colorAccent">#D81B60</color>

</resources>

[Link]
<resources>

<string name="app_name" translatable="false">My Application</string>

<string name="numbers" translatable="false">

<item>1</item>

<item>2</item>

<item>3</item>

</string>

</resources>
[Link]
package [Link]

import [Link]

import [Link]

import [Link]

import [Link]

import [Link]

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

[Link](savedInstanceState)

enableEdgeToEdge()

setContentView([Link].activity_main)

[Link](findViewById([Link])) {
v, insets ->

val systemBars =
[Link]([Link]())

[Link]([Link], [Link], [Link],


[Link])

insets

}
OUTPUT:
PRACTICAL 3

AIM: Programming Activities and fragments : Activity Life Cycle, Activity


methods, Multiple Activities, Life Cycle of fragments and multiple fragments.

CODE:

[Link]
package [Link]

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

class MainActivity : AppCompatActivity() {

private val TAG = "MainActivityLifecycle"

override fun onCreate(savedInstanceState: Bundle?) {


[Link](savedInstanceState)
setContentView([Link].activity_main)

Log.d(TAG, "onCreate")

val btnSecond = findViewById<Button>([Link])


val btnThird = findViewById<Button>([Link])
val btnLifecycle = findViewById<Button>([Link]) //
Added

[Link] {
startActivity(Intent(this, SecondActivity::[Link]))
}

[Link] {
startActivity(Intent(this, ThirdActivity::[Link]))
}

[Link] { // ✅ Added
startActivity(Intent(this, LifecycleActivity::[Link]))
}
}

override fun onStart() {


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

override fun onResume() {


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

override fun onPause() {


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

override fun onStop() {


[Link]()
Log.d(TAG, "onStop")
}

override fun onRestart() {


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

override fun onDestroy() {


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

[Link]
package [Link]

import [Link]
import [Link]

class SecondActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {


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

[Link]
package [Link]

import [Link]
import [Link]

class ThirdActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {


[Link](savedInstanceState)
setContentView([Link].activity_third)
}
}
[Link]
package [Link]

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

class FirstFragment : Fragment() {

private val TAG = "FirstFragment"

override fun onCreate(savedInstanceState: Bundle?) {


[Link](savedInstanceState)
Log.d(TAG, "onCreate")
}

override fun onCreateView(


inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
Log.d(TAG, "onCreateView")
return [Link]([Link].fragment_first, container, false)
}

override fun onStart() {


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

override fun onDestroy() {


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

[Link]
package [Link]

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

class LifecycleActivity : AppCompatActivity() {

private val TAG = "ActivityLifecycle"

override fun onCreate(savedInstanceState: Bundle?) {


[Link](savedInstanceState)
setContentView([Link].activity_lifecycle)
Log.d(TAG, "onCreate")
}

override fun onStart() {


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

override fun onResume() {


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

override fun onPause() {


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

override fun onStop() {


[Link]()
Log.d(TAG, "onStop")
}

override fun onRestart() {


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

override fun onDestroy() {


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

override fun onSaveInstanceState(outState: Bundle) {


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

override fun onRestoreInstanceState(savedInstanceState: Bundle) {


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

Activity_main.xml​

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


<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="24dp"
android:background="@android:color/white">

<Button
android:id="@+id/btnSecond"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Open Second Activity"/>

<Button
android:id="@+id/btnThird"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Open Third Activity"
android:layout_marginTop="20dp"/>

<!-- ✅ Added this button -->


<Button
android:id="@+id/btnLifecycle"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Open Lifecycle Activity"
android:layout_marginTop="20dp"/>

</LinearLayout>

Activity_second.xml

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


<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Activity is Working"
android:textSize="20sp"/>

</LinearLayout>
Activity_third.xml​

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


<LinearLayout
xmlns:android="[Link]
android:gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:text="Third Activity is Working"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

Fragment_activity.xml
<LinearLayout xmlns:android="[Link]
android:gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:text="First Fragment"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

Lifecycle_activity.xml

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


<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<TextView
android:text="Check Logcat for Lifecycle"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>
OUTPUT:
PRACTICAL 4

AIM : Programs related to different Layouts: Coordinate, Linear, Relative,


Table, Absolute, Frame, List View, Grid View

CODE:

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

<[Link]

xmlns:android="[Link]

android:id="@+id/main"

android:layout_width="match_parent"

android:layout_height="match_parent">

<ScrollView

android:layout_width="match_parent"

android:layout_height="match_parent">

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical"

android:padding="16dp">

<!-- ================= LINEAR LAYOUT ================= -->

<TextView

android:layout_width="match_parent"
android:layout_height="wrap_content"

android:text="LinearLayout"

android:textSize="20sp"/>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="One"/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Two"/>

</LinearLayout>

<!-- ================= RELATIVE LAYOUT ================= -->

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="RelativeLayout"

android:textSize="20sp"

android:paddingTop="16dp"/>
<RelativeLayout

android:layout_width="match_parent"

android:layout_height="120dp">

<Button

android:id="@+id/topBtn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Top"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Below"

android:layout_below="@id/topBtn"

android:layout_centerHorizontal="true"/>

</RelativeLayout>

<!-- =============== FRAME LAYOUT =============== -->

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="FrameLayout"

android:textSize="20sp"

android:paddingTop="16dp"/>
<FrameLayout

android:layout_width="match_parent"

android:layout_height="120dp"

android:background="#dddddd">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Bottom Text"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="On Top"

android:textSize="22sp"/>

</FrameLayout>

<!-- ===============TABLE LAYOUT ================ -->

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="TableLayout"

android:textSize="20sp"

android:paddingTop="16dp"/>

<TableLayout

android:layout_width="match_parent"
android:layout_height="wrap_content">

<TableRow>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Name"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Age"/>

</TableRow>

<TableRow>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Ravi"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="21"/>

</TableRow>
</TableLayout>

<!-- ================= ABSOLUTE LAYOUT ================= -->

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="AbsoluteLayout (Deprecated)"

android:textSize="20sp"

android:paddingTop="16dp"/>

<AbsoluteLayout

android:layout_width="match_parent"

android:layout_height="120dp">

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Fixed Position"

android:layout_x="100dp"

android:layout_y="40dp"/>

</AbsoluteLayout>

<!-- ================= LIST VIEW ================= -->

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"
android:text="ListView"

android:textSize="20sp"

android:paddingTop="16dp"/>

<ListView

android:id="@+id/listView"

android:layout_width="match_parent"

android:layout_height="200dp"/>

<!-- ================= GRID VIEW ================= -->

<TextView

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="GridView"

android:textSize="20sp"

android:paddingTop="16dp"/>

<GridView

android:id="@+id/gridView"

android:layout_width="match_parent"

android:layout_height="200dp"

android:numColumns="3"/>

</LinearLayout>

</ScrollView>
</[Link]>

[Link]

OUTPUT:
PRACTICAL 5

AIM: Programming UI elements : Design App With UI

CODE:
Activity_main.xml
[Link]

OUTPUT:
PRACTICAL 6

AIM: Programming menus, dialog, dialog fragments

CODE:

[Link]

package [Link].prac7
import [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 btnDialog = findViewById<Button>([Link])


val btnFragment = findViewById<Button>([Link])

[Link] {
showNormalDialog()
}

[Link] {
val dialog = MyDialogFragment()
[Link](supportFragmentManager, "MyDialog")
}
}

// ===== MENU =====


override fun onCreateOptionsMenu(menu: Menu?): Boolean {
[Link]([Link].main_menu, menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {

return when ([Link]) {

[Link].menu_dialog -> {
showNormalDialog()
true
}

[Link].menu_fragment -> {
val dialog = MyDialogFragment()
[Link](supportFragmentManager, "MyDialog")
true
}

else -> [Link](item)


}
}

// ===== MATERIAL DARK DIALOG =====


private fun showNormalDialog() {

val dialogView = [Link]([Link].dialog_brightness,


null)

val dialog = MaterialAlertDialogBuilder(this)


.setTitle("Brightness")
.setView(dialogView)
.setPositiveButton("OK") { dialogInterface, _ ->
[Link]()
}
.setNegativeButton("Cancel") { dialogInterface, _ ->
[Link]()
}
.create()

[Link]()
}
}

[Link]

package [Link].prac7

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

class MyDialogFragment : DialogFragment() {

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

val view = [Link](requireContext())


.inflate([Link].dialog_brightness, null)

return [Link](requireContext())
.setTitle("Brightness")
.setView(view)
.setPositiveButton("OK") { dialog, _ ->
[Link]()
}
.setNegativeButton("Cancel") { dialog, _ ->
[Link]()
}
.create()
}
}

Activity_main.xml

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


<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="20dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prac 7 - Programming Menu, Dialog &amp; DialogFragment"
android:textSize="19sp"
android:textStyle="bold"
android:layout_marginBottom="30dp"/>

<Button
android:id="@+id/btnDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Normal Dialog"
android:layout_marginBottom="20dp"/>

<Button
android:id="@+id/btnFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Dialog Fragment"/>

</LinearLayout>

Dialog_brightness.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">

<CheckBox
android:id="@+id/checkAuto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Automatic brightness" />

<SeekBar
android:id="@+id/seekBrightness"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:layout_marginTop="20dp"/>

</LinearLayout>

[Link]

<resources>
<string name="app_name">prac7</string>
<string name="open_normal_dialog">Open Normal Dialog</string>
<string name="open_dialog_fragment">Open Dialog Fragment</string>
<string name="normal_dialog_title">Normal Dialog</string>
<string name="normal_dialog_message">This is a normal
AlertDialog.</string>
<string name="dialog_fragment_title">Dialog Fragment</string>
<string name="dialog_fragment_message">This dialog is from
DialogFragment.</string>
</resources>

OUTPUT:
PRACTICAL 7

AIM: Programs on Intents, Events Listeners and Adapters

CODE:

[Link]​

package [Link]

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

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {


[Link](savedInstanceState)
enableEdgeToEdge()
setContentView([Link].activity_main)

[Link](findViewById([Link])) {
v, insets ->
val systemBars =
[Link]([Link]())
[Link]([Link], [Link], [Link],
[Link])
insets
}

// 🔹Event Listener Example


val editText = findViewById<EditText>([Link])
val button = findViewById<Button>([Link])

[Link] {
val name = [Link]()

// 🔹 Intent Example
val intent = Intent(this, SecondActivity::[Link])
[Link]("username", name)
startActivity(intent)
}

// 🔹 Adapter Example
val listView = findViewById<ListView>([Link])

val items = arrayOf("Apple", "Banana", "Mango", "Orange")

val adapter = ArrayAdapter(this,


[Link].simple_list_item_1,
items)

[Link] = adapter
}
}

Activity_main.xml

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


<TableLayout
xmlns:android="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
android:padding="20dp">

<TableRow>
<EditText
android:id="@+id/editTextName"
android:hint="Enter Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>

<TableRow>
<Button
android:id="@+id/buttonClick"
android:text="Send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>

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

</TableLayout>

[Link]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<TextView
android:id="@+id/textViewResult"
android:textSize="22sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

OUTPUT:


Practical 8

AIM: Programs on Services, notification and broadcast receivers

CODE:

[Link]​

package [Link].prac8
import [Link]
import [Link]

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {


[Link](savedInstanceState)
setContentView([Link].activity_main)

// Set title in top bar


supportActionBar?.title = "Android Broadcast"
}
}

activity_main.xml

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


<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Airplane Icon -->


<ImageView
android:id="@+id/imgPlane"
android:layout_width="120dp"
android:layout_height="120dp"
android:src="@android:drawable/ic_menu_send"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/txtFlight"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="200dp"/>

<!-- Flight Mode Text -->


<TextView
android:id="@+id/txtFlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flight Mode"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="@id/imgPlane"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"/>

</[Link]>

[Link]
<resources>
<style name="Theme.Prac8" parent="[Link]">

<item name="colorPrimary">#1976D2</item>
<item name="colorPrimaryDark">#1565C0</item>
<item name="colorAccent">#1976D2</item>

</style>

</resources>

OUTPUT:
PRACTICAL 9

AIM: Database Programming with SQLite

CODE:

[Link]
?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="[Link]

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="16dp">

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="SQLite Tutorial - User Management"

android:textSize="18sp"

android:textStyle="bold"

android:layout_marginBottom="20dp"/>

<EditText

android:id="@+id/etId"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="User ID"

android:inputType="number"/>

<EditText
android:id="@+id/etName"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="User Name"/>

<EditText

android:id="@+id/etAge"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="User Age"

android:inputType="number"

android:layout_marginBottom="10dp"/>

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

<Button

android:id="@+id/btnAdd"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:text="ADD"/>

<Button

android:id="@+id/btnDelete"

android:layout_width="0dp"

android:layout_weight="1"
android:layout_height="wrap_content"

android:text="DELETE"/>

<Button

android:id="@+id/btnShow"

android:layout_width="0dp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:text="SHOW ALL"/>

</LinearLayout>

<TextView

android:id="@+id/tvCount"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Fetched 0 users"

android:layout_marginTop="10dp"/>

<ListView

android:id="@+id/listView"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

</LinearLayout>
[Link]
package [Link].prac9

import [Link]

import [Link].*

import [Link]

class MainActivity : AppCompatActivity() {

lateinit var db: DatabaseHelper

override fun onCreate(savedInstanceState: Bundle?) {

[Link](savedInstanceState)

setContentView([Link].activity_main)

db = DatabaseHelper(this)

val etId = findViewById<EditText>([Link])

val etName = findViewById<EditText>([Link])

val etAge = findViewById<EditText>([Link])

val btnAdd = findViewById<Button>([Link])

val btnDelete = findViewById<Button>([Link])

val btnShow = findViewById<Button>([Link])

val listView = findViewById<ListView>([Link])

val tvCount = findViewById<TextView>([Link])

[Link] {

val id = [Link]().toInt()

val name = [Link]()


val age = [Link]().toInt()

if ([Link](id, name, age))

[Link](this, "User Added", Toast.LENGTH_SHORT).show()

else

[Link](this, "Insert Failed", Toast.LENGTH_SHORT).show()

[Link] {

val id = [Link]().toInt()

if ([Link](id))

[Link](this, "User Deleted", Toast.LENGTH_SHORT).show()

else

[Link](this, "Delete Failed", Toast.LENGTH_SHORT).show()

[Link] {

val users = [Link]()

[Link] = "Fetched ${[Link]} users"

val adapter = ArrayAdapter(this, [Link].simple_list_item_1,


users)

[Link] = adapter

}
[Link]
package [Link].prac9

import [Link]

import [Link]

import [Link]

import [Link]

class DatabaseHelper(context: Context) :

SQLiteOpenHelper(context, "UserDB", null, 1) {

override fun onCreate(db: SQLiteDatabase?) {

val createTable = """

CREATE TABLE users(

id INTEGER PRIMARY KEY,

name TEXT,

age INTEGER

"""

db?.execSQL(createTable)

override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {

db?.execSQL("DROP TABLE IF EXISTS users")

onCreate(db)

fun insertUser(id: Int, name: String, age: Int): Boolean {

val db = writableDatabase
val values = ContentValues()

[Link]("id", id)

[Link]("name", name)

[Link]("age", age)

val result = [Link]("users", null, values)

return result != -1L

fun deleteUser(id: Int): Boolean {

val db = writableDatabase

val result = [Link]("users", "id=?", arrayOf([Link]()))

return result > 0

fun getAllUsers(): ArrayList<String> {

val list = ArrayList<String>()

val db = readableDatabase

val cursor = [Link]("SELECT * FROM users", null)

if ([Link]()) {

do {

val id = [Link](0)

val name = [Link](1)

val age = [Link](2)

[Link]("Tutorialkart - $id\nTK - $age\n$name")

} while ([Link]())

[Link]()

return list } }
OUTPUT:
PRACTICAL 10

AIM: Programming Security and permissions

CODE:

[Link]
package [Link]

import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
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(


[Link],
[Link].READ_CONTACTS,
[Link].READ_EXTERNAL_STORAGE,
[Link].SEND_SMS,
[Link].READ_CALENDAR
)

managePermissions = ManagePermissions(this, list,


PermissionsRequestCode)

val button = findViewById<Button>([Link])

[Link] {
if ([Link].SDK_INT >= Build.VERSION_CODES.M) {
[Link]()
}
}
}

override fun onRequestPermissionsResult(


requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
if (requestCode == PermissionsRequestCode) {
val isPermissionsGranted =
[Link](
requestCode,
permissions,
grantResults
)

if (isPermissionsGranted) {
toast("Permissions granted.")
} else {
toast("Permissions denied.")
}
}
}
}

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

[Link]

package [Link]

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

class ManagePermissions(
private val activity: Activity,
private val list: List<String>,
private val code: Int
) {

fun checkPermissions() {
if (!arePermissionsGranted()) {
showAlert()
} else {
[Link]("Permissions already granted.")
}
}

private fun arePermissionsGranted(): Boolean {


for (permission in list) {
if ([Link](
activity,
permission
) != PackageManager.PERMISSION_GRANTED
) {
return false
}
}
return true
}

private fun showAlert() {


[Link](activity)
.setTitle("Need permission(s)")
.setMessage("Some permissions are required to perform this task.")
.setPositiveButton("OK") { _, _ ->
[Link](
activity,
[Link](),
code
)
}
.setNegativeButton("Cancel", null)
.show()
}

fun processPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
): Boolean {

if (requestCode != code) return false

for (result in grantResults) {


if (result != PackageManager.PERMISSION_GRANTED) {
return false
}
}
return true
}
}

[Link]​

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


<manifest xmlns:android="[Link]

<!-- Permissions -->


<uses-permission android:name="[Link]"/>
<uses-permission android:name="[Link]"/>
<uses-permission android:name="[Link].READ_CONTACTS"/>
<uses-permission android:name="[Link].READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="[Link].SEND_SMS"/>
<uses-permission android:name="[Link].READ_CALENDAR"/>

<application
android:allowBackup="true"
android:label="@string/app_name"
android:theme="@style/[Link]">

<activity
android:name=".MainActivity"
android:exported="true">

<intent-filter>
<action android:name="[Link]"/>
<category android:name="[Link]"/>
</intent-filter>

</activity>

</application>

</manifest>

[Link](:app)​
plugins {
alias([Link])
alias([Link])
}

android {
namespace = "[Link]"
compileSdk = 36

defaultConfig {
applicationId = "[Link]"
minSdk = 24
targetSdk = 36
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "[Link]"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("[Link]"),
"[Link]"
)
}
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "11"
}
}

dependencies {
implementation([Link])
implementation([Link])
implementation([Link])
implementation([Link])
implementation([Link])

testImplementation([Link])
androidTestImplementation([Link])
androidTestImplementation([Link])
}

OUTPUT:
PRACTICAL 11
AIM: Programming Network Communications and Services (JSON)

CODE:

[Link]
package [Link].prac11​

import [Link]​
import [Link]​

interface ApiService {​
@GET(".")​
fun getFeeds(): Call<String>​
}

[Link]
package [Link].prac11​

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

class RecyclerAdapter : [Link]<[Link]>() {​

private var list = ArrayList<String>()​

fun setItems(newList: ArrayList<String>) {​
list = newList​
notifyDataSetChanged()​
}​

override fun getItemCount() = [Link]​

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
ViewHolder {​
val view = [Link]([Link])​
.inflate([Link].simple_list_item_1, parent, false)​
return ViewHolder(view)​
}​

override fun onBindViewHolder(holder: ViewHolder, position: Int) {​
[Link] = list[position]​
}​

class ViewHolder(itemView: View) : [Link](itemView) {​
val textView: TextView = [Link]([Link].text1)​
}​
}
[Link]
package [Link].prac11​

import [Link]​
import [Link]​
import [Link]​
import [Link]​
import [Link]​
import [Link]​
import [Link]​
import [Link]​
import [Link]​
import [Link]​
import [Link]​
import [Link]​
import retrofit2.*​
import [Link]​
import [Link]​

import [Link]​

class MainActivity : AppCompatActivity() {​

private lateinit var recyclerView: RecyclerView​
private lateinit var imageView: ImageView​

private val list = ArrayList<String>()​
private val adapter = RecyclerAdapter()​

private val retrofit = [Link]()​
.baseUrl("[Link]
.addConverterFactory([Link]())​
.client(OkHttpClient())​
.build()​

private val broadcastReceiver = object : BroadcastReceiver() {​
override fun onReceive(context: Context, intent: Intent) {​
val noConnection = [Link](​
ConnectivityManager.EXTRA_NO_CONNECTIVITY, false​
)​

if (noConnection) disconnected()​
else connected()​
}​
}​

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

recyclerView = findViewById([Link])​
imageView = findViewById([Link])​

[Link] = LinearLayoutManager(this)​
[Link] = adapter​
}​

override fun onStart() {​
[Link]()​
registerReceiver(​
broadcastReceiver,​
IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)​
)​
}​

override fun onStop() {​
[Link]()​
unregisterReceiver(broadcastReceiver)​
}​

private fun disconnected() {​
[Link] = [Link]​
[Link] = [Link]​
}​

private fun connected() {​
[Link] = [Link]​
[Link] = [Link]​
fetchFeeds()​
}​

private fun fetchFeeds() {​
[Link](ApiService::[Link])​
.getFeeds()​
.enqueue(object : Callback<String> {​

override fun onFailure(call: Call<String>, t: Throwable) {​
Log.e("API_ERROR", [Link] ?: "Unknown error")​
}​

override fun onResponse(call: Call<String>, response:
Response<String>) {​
[Link]()?.let { parseTitles(it) }​
}​
})​
}​

private fun parseTitles(json: String) {​

val jsonObject = JSONObject(json).getJSONObject("data")​
val children = [Link]("children")​

[Link]()​

for (i in 0 until [Link]()) {​
val title = [Link](i)​
.getJSONObject("data")​
.getString("title")​

[Link](title)​
}​

[Link](list)​
}​
}
[Link]
<manifest xmlns:android="[Link]

<uses-permission android:name="[Link]"/>​
<uses-permission android:name="[Link].ACCESS_NETWORK_STATE"/>​

<application​
android:allowBackup="true"​
android:theme="@style/[Link]">​

<activity​
android:name=".MainActivity"​
android:exported="true">​

<intent-filter>​
<action android:name="[Link]"/>​
<category android:name="[Link]"/>​
</intent-filter>​

</activity>​

</application>​

</manifest>
<?xml version="1.0" encoding="utf-8"?>​
<[Link]​
xmlns:android="[Link]
xmlns:app="[Link]
android:layout_width="match_parent"​
android:layout_height="match_parent">​

<[Link]​
android:id="@+id/recyclerView"​
android:layout_width="match_parent"​
android:layout_height="match_parent"/>​

<ImageView​
android:id="@+id/imageView"​
android:layout_width="match_parent"​
android:layout_height="wrap_content"​
android:src="@drawable/no_internet_connection"​
android:visibility="gone"/>​

</[Link]>
OUTPUT:
When Internet is on:

When Internet is off:

You might also like