Untitled Document
Untitled Document
screen.
3. Set:
○ Name: HelloWorld
○ Language: Kotlin
<TextView
android:id="@+id/helloText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:textSize="30sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
This makes a big "Hello World" text in the middle of the screen! 🎉
====================================================================================
Create an Android app that takes two numbers as input
and displays their sum.
Now fill:
● Name: AddNumbersApp
● Language: Kotlin
Click Finish.
Wait a bit while it sets everything up.
xml
CopyEdit
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
This creates:
● 2 input boxes 📦
● 1 button ➕
● 1 text for showing result 🧮
kotlin
CopyEdit
package com.example.addnumbersapp // Your package name might be different
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
● It adds them
Try this:
● Type 5 and 3
● Press "Add"
🎉 You’ll see:
ini
CopyEdit
Sum = 8
====================================================================================
Create an android app to demonstrate Activity lifecycle.
✅ Step-by-step Guide
🛠 Step 1: Open Android Studio and Create New Project
1. Open Android Studio
4. Fill:
○ Name: LifecycleDemoApp
○ Language: Kotlin
xml
CopyEdit
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
kotlin
CopyEdit
package com.example.lifecycledemoapp // Your package name
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
○ onCreate called
○ onStart called
○ onResume called
====================================================================================
Create an android app to demonstrate on Toast
4. Fill:
○ Name: ToastDemoApp
○ Language: Kotlin
xml
CopyEdit
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
🧁
This adds a button in the center of the screen.
When we press it — Toast time!
kotlin
CopyEdit
package com.example.toastdemoapp // Your package name might be different
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
====================================================================================
Create Android app which demonstrate implicit and explicit intent.
4. Fill:
○ Name: IntentDemoApp
○ Language: Kotlin
○ Click Finish
<Button
android:id="@+id/openSecondActivityButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to Second Screen"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="100dp"/>
<Button
android:id="@+id/openBrowserButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Browser"
app:layout_constraintTop_toBottomOf="@id/openSecondActivityButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
kotlin
CopyEdit
package com.example.intentdemoapp
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
xml
CopyEdit
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">
<TextView
android:id="@+id/secondScreenText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You are on the second screen!"
android:textSize="24sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
====================================================================================
Create an Android application using Kotlin that
demonstrates the use of different layouts
(LinearLayout, RelativeLayout, and
ConstraintLayout) .
4. Fill:
○ Name: LayoutDemoApp
○ Language: Kotlin
Relative activity_relative
Layout .xml
Constraint activity_main.xml
xml
CopyEdit
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="This is LinearLayout"
android:textSize="22sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Click Me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
xml
CopyEdit
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/titleText"
android:text="This is RelativeLayout"
android:textSize="22sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Click Me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/titleText"
android:layout_marginTop="20dp"/>
</RelativeLayout>
xml
CopyEdit
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/constraintText"
android:text="This is ConstraintLayout"
android:textSize="22sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<Button
android:text="Click Me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/constraintText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
🧩 Add LinearActivity:
1. Right-click java > your.package.name
kotlin
CopyEdit
package com.example.layoutdemoapp
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
🧩 Add RelativeActivity:
Same way, create RelativeActivity.kt and paste:
kotlin
CopyEdit
package com.example.layoutdemoapp
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
xml
CopyEdit
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnLinear"
android:text="Show LinearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
<Button
android:id="@+id/btnRelative"
android:text="Show RelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/btnLinear"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
kotlin
CopyEdit
package com.example.layoutdemoapp
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
btnLinear.setOnClickListener {
startActivity(Intent(this, LinearActivity::class.java))
}
btnRelative.setOnClickListener {
startActivity(Intent(this, RelativeActivity::class.java))
}
}
}
xml
CopyEdit
<activity android:name=".LinearActivity" />
<activity android:name=".RelativeActivity" />
So it looks like:
xml
CopyEdit
<application
...
<activity android:name=".RelativeActivity" />
<activity android:name=".LinearActivity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
====================================================================================
Create an Android application in Kotlin that uses a
button .
🛠 Step-by-Step 🧒
🟢 Step 1: Open Android Studio
1. Open Android Studio
○ Name: ButtonApp
○ Language: Kotlin
Go to this file:
nginx
CopyEdit
app > res > layout > activity_main.xml
xml
CopyEdit
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="20dp">
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!" />
</LinearLayout>
Go to:
nginx
CopyEdit
app > java > your_package_name > MainActivity.kt
kotlin
CopyEdit
package com.example.buttonapp // your app package
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
====================================================================================
Create a android "Hello World" app using Jetpack
Compose in Android Studio:
● Name: HelloWorldCompose
● Language: Kotlin
● Click Finish
Now wait a little bit. It will create your app with some code already.
nginx
CopyEdit
app > java > com.example.helloworldcompose > MainActivity.kt
Android Studio already adds some code to show “Android”. Let’s change that to “Hello World”
kotlin
CopyEdit
Greeting("Android")
kotlin
CopyEdit
Greeting("Hello World")
Also, if you want, you can customize the Greeting function like this:
kotlin
CopyEdit
@Composable
fun Greeting(name: String) {
Text(text = name)
}
kotlin
CopyEdit
package com.example.helloworldcompose
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import com.example.helloworldcompose.ui.theme.HelloWorldComposeTheme
@Composable
fun Greeting(name: String) {
Text(text = name)
}
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
HelloWorldComposeTheme {
Greeting("Hello World")
}
}
nginx
CopyEdit
Hello World
====================================================================================
Create a android app to create basic notification.
● Name: NotificationApp
● Language: Kotlin
● Click Finish
nginx
CopyEdit
app > res > layout > activity_main.xml
Replace with:
xml
CopyEdit
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/notifyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Notification" />
</LinearLayout>
nginx
CopyEdit
app > java > com.example.notificationapp > MainActivity.kt
kotlin
CopyEdit
package com.example.notificationapp
import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
createNotificationChannel()
with(NotificationManagerCompat.from(this)) {
notify(NOTIFICATION_ID, builder.build())
}
}
}
xml
CopyEdit
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
kotlin
CopyEdit
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
requestPermissions(arrayOf(android.Manifest.permission.POST_NOTIFICATIONS), 101)
}
====================================================================================
Create an Android application that demonstrates
the use of a ListView to display a list of items.
● Name: ListViewApp
● Language: Kotlin
● Click Finish
nginx
CopyEdit
app > res > layout > activity_main.xml
xml
CopyEdit
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<!-- ListView to show the list of items -->
<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
nginx
CopyEdit
app > java > com.example.listviewapp > MainActivity.kt
kotlin
CopyEdit
package com.example.listviewapp
import android.os.Bundle
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
● setOnItemClickListener: This makes the items clickable. When you tap an item, it will show a
Toast message.
Now, when you tap any item in the list, a Toast will show saying, “You clicked: [Fruit Name]”.
====================================================================================
Create an Android application that demonstrates the use of a
ListView to display a list of item and icon.
● Name: ListViewWithIcons
● Language: Kotlin
● Click Finish
1. Download some icons: You can search for free icons on sites like Flaticon. Let’s say we want to use
fruit icons.
○ Select Icon Type as Image and choose the fruit images you downloaded.
3. Name each image something meaningful like apple_icon.png, banana_icon.png, etc.
Go to res > layout > activity_main.xml and replace the code with this:
xml
CopyEdit
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
kotlin
CopyEdit
package com.example.listviewwithicons
import android.os.Bundle
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
textView.text = currentItem?.name
imageView.setImageResource(currentItem?.iconResId ?: 0)
return view
}
}
● CustomAdapter: This is a custom adapter that binds the name and icon to each item in the list.
● setOnItemClickListener: When you tap an item, it will show a Toast with the item's name.
Now you’ll see a list of items with icons next to them. When you tap any item, you’ll get a Toast saying "You
clicked: [Item Name]."
====================================================================================
Create an android app to create basic notification with reply.
● Name: NotificationWithReply
● Language: Kotlin
● Click Finish
xml
CopyEdit
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
This permission is required to post notifications starting from Android 13 (API level 33).
xml
CopyEdit
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
kotlin
CopyEdit
package com.example.notificationwithreply
import android.app.*
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.widget.Button
import android.widget.RemoteInput
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
// Create the Notification Channel for devices running Android 8.0 and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = "Notification Channel"
val descriptionText = "Channel for Reply Notification"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {
description = descriptionText
}
// Register the channel with the system
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
companion object {
const val KEY_REPLY = "key_reply"
}
}
kotlin
CopyEdit
package com.example.notificationwithreply
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.widget.Toast
import androidx.core.app.RemoteInput
This class will handle the reply and display a Toast message showing what the user typed.
xml
CopyEdit
<receiver android:name=".ReplyReceiver" android:exported="false" />
2. BroadcastReceiver: The ReplyReceiver handles the user's reply when they submit it. It shows a
Toast with the reply message.
When you press the "Show Notification" button, a notification will appear. You can reply directly from the
notification, and your reply will show in a Toast.
Create an Android application that demonstrates
the use of a Service to perform a
1) background task
2) forground task.
● Name: ServiceExample
● Language: Kotlin
● Click Finish.
xml
CopyEdit
<service android:name=".BackgroundService" android:enabled="true"
android:exported="false" />
<service android:name=".ForegroundService" android:enabled="true"
android:exported="false" />
Right-click on java > your.package.name and choose New > Kotlin Class. Name it BackgroundService.
kotlin
CopyEdit
package com.example.serviceexample
import android.app.Service
import android.content.Intent
import android.os.IBinder
import android.util.Log
import android.widget.Toast
import androidx.core.app.NotificationCompat
return START_STICKY
}
● It runs for 10 seconds, and each second, it logs a message to show that the task is happening in the
background.
Right-click on java > your.package.name and choose New > Kotlin Class. Name it ForegroundService.
kotlin
CopyEdit
package com.example.serviceexample
import android.app.*
import android.content.Intent
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat
return START_STICKY
}
● We create a notification channel for Android versions 8.0 (Oreo) and above.
● startForeground() is used to run the service in the foreground and show a notification.
xml
CopyEdit
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
2. Open MainActivity.kt and modify it to start the services when the buttons are clicked:
kotlin
CopyEdit
package com.example.serviceexample
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
● When you click the "Start Foreground Service" button, the ForegroundService starts in the
foreground with a notification.
====================================================================================
Create an Android application that demonstrates
the use of a Broadcast Receiver to listen for a wifi
status on or off and respond to it.
🛠️ Step-by-Step Guide
🟢 Step 1: Create a New Project
1. Open Android Studio.
● Name: WifiBroadcastReceiverExample
● Language: Kotlin
● Click Finish.
xml
CopyEdit
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
These permissions are required to monitor Wi-Fi state changes and manage the Wi-Fi state.
🔵 Step 3: Create the Broadcast Receiver
Now, let’s create the Broadcast Receiver to listen for Wi-Fi status changes.
○ Right-click on java > your.package.name and choose New > Kotlin Class. Name it
WifiReceiver.
kotlin
CopyEdit
package com.example.wifibroadcastreceiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.wifi.WifiInfo
import android.net.wifi.WifiManager
import android.widget.Toast
● When the Wi-Fi state changes, it displays a Toast message to show the status ("Wi-Fi is ON" or "Wi-Fi
is OFF").
🟠 Step 4: Register the Receiver in AndroidManifest.xml
Now, we need to register the Broadcast Receiver in the AndroidManifest.xml so that the app can listen
for Wi-Fi state changes.
2. Add the following <receiver> tag inside the <application> tag:
xml
CopyEdit
<receiver android:name=".WifiReceiver" android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
</intent-filter>
</receiver>
● When this event is triggered, the system calls the onReceive() method of the WifiReceiver class.
2. Modify it as follows to add a simple TextView (though it's not strictly necessary for the task, but for
clarity, we will show it):
xml
CopyEdit
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/statusTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wi-Fi Status: Unknown"
android:textSize="18sp"
android:layout_centerInParent="true"/>
</RelativeLayout>
We’ll use this TextView to show the Wi-Fi status, but for now, the Toast message is the main output.
2. Modify the code to display a message in the TextView when the Wi-Fi status changes:
kotlin
CopyEdit
package com.example.wifibroadcastreceiver
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.wifi.WifiManager
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
● When the Wi-Fi state changes, we call updateWifiStatus to update the UI with the new status.
● The BroadcastReceiver will respond to changes in Wi-Fi status, i.e., whether Wi-Fi is turned ON or
OFF.
🛠️ Step-by-Step Guide
🟢 Step 1: Create a New Project
1. Open Android Studio.
○ Name: RecyclerViewDemo
○ Language: Kotlin
gradle
CopyEdit
implementation 'androidx.recyclerview:recyclerview:1.2.1'
○ Right-click on java > your.package.name and choose New > Kotlin Class. Name it Item.
kotlin
CopyEdit
data class Item(
val title: String,
val imageUrl: String
)
1. In the res > layout folder, create a new layout file called item_layout.xml.
xml
CopyEdit
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:id="@+id/itemImage"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_marginEnd="16dp"
android:scaleType="centerCrop" />
<TextView
android:id="@+id/itemTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:gravity="center_vertical" />
</LinearLayout>
2. This adapter will be used to bind data (text and images) to each item in the RecyclerView.
kotlin
CopyEdit
package com.example.recyclerViewdemo
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
● The ItemViewHolder class holds references to the TextView and ImageView for each item.
● In onBindViewHolder(), the title and image are set for each item using Glide to load images from
URLs.
gradle
CopyEdit
implementation 'com.github.bumptech.glide:glide:4.12.0'
kapt 'com.github.bumptech.glide:compiler:4.12.0'
xml
CopyEdit
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
2. Initialize the RecyclerView and set up the ItemAdapter to bind the data.
kotlin
CopyEdit
package com.example.recyclerViewdemo
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
recyclerView = findViewById(R.id.recyclerView)
● The RecyclerView is set with a LinearLayoutManager (to display items in a vertical list).
====================================================================================
====================================================================================
xml
CopyEdit
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="24dp">
<EditText
android:id="@+id/nameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name" />
<Button
android:id="@+id/insertButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Insert Name" />
<Button
android:id="@+id/readButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read Names" />
<TextView
android:id="@+id/displayText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Saved names will appear here"
android:paddingTop="16dp" />
</LinearLayout>
kotlin
CopyEdit
package com.example.contentproviderdemo
import android.content.ContentProvider
import android.content.ContentValues
import android.database.Cursor
import android.net.Uri
import android.util.Log
companion object {
const val AUTHORITY = "com.example.contentproviderdemo.provider"
val CONTENT_URI: Uri = Uri.parse("content://$AUTHORITY/names")
}
kotlin
CopyEdit
package com.example.contentproviderdemo
import android.content.ContentValues
import android.net.Uri
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
nameInput = findViewById(R.id.nameInput)
insertButton = findViewById(R.id.insertButton)
readButton = findViewById(R.id.readButton)
displayText = findViewById(R.id.displayText)
val uri =
Uri.parse("content://com.example.contentproviderdemo.provider/names")
insertButton.setOnClickListener {
val name = nameInput.text.toString()
val values = ContentValues().apply {
put("name", name)
}
contentResolver.insert(uri, values)
nameInput.setText("")
}
readButton.setOnClickListener {
val cursor = contentResolver.query(uri, null, null, null, null)
val builder = StringBuilder()
cursor?.let {
while (it.moveToNext()) {
val name = it.getString(it.getColumnIndexOrThrow("name"))
builder.append(name).append("\n")
}
it.close()
}
displayText.text = builder.toString()
}
}
}