Android Programming
Android Programming
Xml
<TextView
android:id="@+id/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" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</[Link]>
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
setContentView([Link].activity_main)
Go to About phone
Go back to Settings
OR
adb devices
Battery low
SMS received
[Link] file
//first line
private lateinit var airplaneModeReceiver: BroadcastReceiver
if (isAirplaneModeOn) {
[Link](context, "Airplane Mode Enabled", Toast.LENGTH_SHORT).show()
} else {
[Link](context, "Airplane Mode Disabled", Toast.LENGTH_SHORT).show()
}
}
}
Click on package – app – src -main-java -your package name would be visible right click on that new
Kotlin class file name AirplaneModeReceiver
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
if (isAirplaneModeOn) {
[Link](context, "Airplane Mode Enabled", Toast.LENGTH_SHORT).show()
} else {
[Link](context, "Airplane Mode Disabled", Toast.LENGTH_SHORT).show()
}
}
}
step 3 open the [Link]
Click on package – app – src -main below res you will see the [Link] file
<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]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
<receiver
android:name=".AirplaneModeReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="[Link].AIRPLANE_MODE_CHANGED" />
</intent-filter>
</receiver>
</application>
</manifest>
Output
Practical 0 a 4
A Service in Android is a component that runs in the background, without a user interface.
It’s useful for tasks that need to keep running even if the app is minimized, like:
Step 1
App – src – main – res – now right click on res – new – android resource directory - in resource type
choose raw – click on ok
App – src – main – java – you’ll see your package name, right click on the package – new Kotlin class
and name it [Link]
[Link]
package [Link]
import [Link].*
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
startForeground(1, notification)
mediaPlayer?.start()
return START_STICKY
}
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/btnPlay"
android:text="Play Music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnStop"
android:text="Stop Music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Step 4 [Link]
<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]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
<service
android:name=".MusicService"
android:exported="false"
android:foregroundServiceType="mediaPlayback"
tools:targetApi="29" />
</application>
</manifest>
Step 5 [Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
[Link] {
val intent = Intent(this, MusicService::[Link])
[Link] {
stopService(Intent(this, MusicService::[Link]))
}
}
}
As you run the song by playing PLAY MUSIC make sure the Emulator Volume is full
Practical 1
Step 1
[Link]
[Link]
<resources>
<string name="app_name">ResourceDemo</string>
<string name="welcome_message">Welcome to Android Resources Demo!</string>
<string name="button_text">Click Me</string>
</resources>
[Link]
<resources xmlns:tools="[Link]
<style name="[Link]" parent="[Link]">
<item name="colorPrimary">@color/primaryColor</item>
<item name="colorSecondary">@color/secondaryColor</item>
<item name="android:textColor">@color/whiteColor</item>
</style>
</resources>
Step 2
myapplication – app – src- main – res- drawable – right click on drawable – new drawable resource
file
<solid android:color="@color/primaryColor"/>
<corners android:radius="16dp"/>
<padding
android:left="12dp"
android:top="8dp"
android:right="12dp"
android:bottom="8dp"/>
</shape>
Step 3
Here it is [Link]
Step 4
[Link]
<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]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
</application>
</manifest>
Step 5
Activity_main.xml
<LinearLayout xmlns:android="[Link]
android:orientation="vertical"
android:padding="@dimen/padding_small"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvWelcome"
android:text="@string/welcome_message"
android:textColor="@color/primaryColor"
android:textSize="@dimen/text_size_large"
android:layout_marginBottom="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnClick"
android:text="@string/button_text"
android:background="@drawable/rounded_button"
android:textColor="@color/whiteColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ImageView
android:id="@+id/imgDemo"
android:src="@drawable/aman"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="16dp"/>
</LinearLayout>
Step 6
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
Step 1
Open [Link]
package [Link]
import [Link]
import [Link]
import [Link]
now to check the output you’ve to click on logcat which is available at the bottom left of the screen
Step 3
Myapplication- src- main – res – layout – right click – new – layout resource file – name of the layout
– ok
A layout will be created, now depending upon the topic you’ve to create the layout, here I’ve
created a demo layout, which is [Link]
1)constraintlayout
<TextView
android:id="@+id/tvHello"
android:text="Hello Android"
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:id="@+id/btnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="196dp"
android:text="Click Me"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvHello" />
</[Link]>
2) FrameLayout
<ImageView
android:src="@android:color/darker_gray"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TextView
android:text="Splash Screen"
android:textSize="24sp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</FrameLayout>
3) GridLayout
<Button android:text="One"/>
<Button android:text="Two"/>
<Button android:text="Three"/>
<Button android:text="Four"/>
</GridLayout>
4) LinearLayout
<TextView
android:text="Login"
android:textSize="24sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:hint="Username"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:hint="Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:text="Submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
5) RelativeLayout
<TextView
android:id="@+id/tvTitle"
android:text="Welcome"
android:textSize="22sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Continue"
android:layout_below="@id/tvTitle"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
6) ScrollView
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item 1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item 2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item 3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item 4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item 5" />
</LinearLayout>
</ScrollView>
7) TableLayout
<EditText
android:layout_width="163dp"
android:hint="Enter name" />
</TableRow>
<TableRow>
<TextView android:text="Age"/>
<EditText android:hint="Enter age"/>
</TableRow>
</TableLayout>
Step 2
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
}
}
Output Constraint layout
Output Framelayout
Output : GridLayout
Output : Linearlayout
Output : relativelayout
Output : Scrollview
Output : Tablelayout
Practical 4
Step 1
Activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="User Registration"
android:textSize="22sp"
android:textStyle="bold"
android:layout_marginBottom="12dp" />
<EditText
android:id="@+id/etName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name" />
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Email"
android:inputType="textEmailAddress" />
<RadioGroup
android:id="@+id/rgGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/rbMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/rbFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female" />
</RadioGroup>
<CheckBox
android:id="@+id/cbJava"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java" />
<CheckBox
android:id="@+id/cbKotlin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kotlin" />
<Spinner
android:id="@+id/spCity"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Switch
android:id="@+id/swNotify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enable Notifications" />
<SeekBar
android:id="@+id/sbAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100" />
<RatingBar
android:id="@+id/rbRating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:stepSize="1.0" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
</ScrollView>
Step 2
Main [Link]
package [Link]
import [Link]
import [Link].*
import [Link]
import [Link]
import [Link]
import [Link]
[Link] {
[Link](
this,
"Name: $name\nEmail: $email\nGender: $gender\nSkills: $skills\nCity: $city\nAge:
$age\nRating: $rating\nNotifications: $notification",
Toast.LENGTH_LONG
).show()
}
}
}
Output
Practical 5
Step 1
This pop up will open make sure to type menu in both of the pop up
Step 2
Step 3
[Link]
<menu xmlns:android="[Link]
<item
android:id="@+id/menu_delete"
android:title="Delete"/>
<item
android:id="@+id/menu_about"
android:title="About"/>
</menu>
Step 4
Activity_main.xml
<[Link]
android:id="@+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:title="Menu Example"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</[Link]>
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
[Link].menu_delete -> {
showDeleteDialog()
}
[Link].menu_about -> {
[Link](this, "Menu & Dialog Example", Toast.LENGTH_SHORT).show()
}
}
return true
}
Step 1
App – src -main -[Link] – right click – new – activity- empty-view activity
Main [Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
Activity_main.xml
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="First Activity1" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next" />
</LinearLayout>
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
[Link] {
//finish()
val intent = Intent(this, MainActivity::[Link])
startActivity(intent)
}
}
}
Activity_second.xml
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Previous Activity" />
</LinearLayout>
Practical 0 a 3
Battery low
SMS received
[Link] file
//first line
private lateinit var airplaneModeReceiver: BroadcastReceiver
if (isAirplaneModeOn) {
[Link](context, "Airplane Mode Enabled", Toast.LENGTH_SHORT).show()
} else {
[Link](context, "Airplane Mode Disabled", Toast.LENGTH_SHORT).show()
}
}
}
Click on package – app – src -main-java -your package name would be visible right click on that new
Kotlin class file name AirplaneModeReceiver
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
if (isAirplaneModeOn) {
[Link](context, "Airplane Mode Enabled", Toast.LENGTH_SHORT).show()
} else {
[Link](context, "Airplane Mode Disabled", Toast.LENGTH_SHORT).show()
}
}
}
step 3 open the [Link]
Click on package – app – src -main below res you will see the [Link] file
<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]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
<receiver
android:name=".AirplaneModeReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="[Link].AIRPLANE_MODE_CHANGED" />
</intent-filter>
</receiver>
</application>
</manifest>
Output
Practical 0 a 4
A Service in Android is a component that runs in the background, without a user interface.
It’s useful for tasks that need to keep running even if the app is minimized, like:
Step 1
App – src – main – res – now right click on res – new – android resource directory - in resource type
choose raw – click on ok
App – src – main – java – you’ll see your package name, right click on the package – new Kotlin class
and name it [Link]
[Link]
package [Link]
import [Link].*
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
startForeground(1, notification)
mediaPlayer?.start()
return START_STICKY
}
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/btnPlay"
android:text="Play Music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnStop"
android:text="Stop Music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Step 4 [Link]
<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]">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="[Link]" />
<category android:name="[Link]" />
</intent-filter>
</activity>
<service
android:name=".MusicService"
android:exported="false"
android:foregroundServiceType="mediaPlayback"
tools:targetApi="29" />
</application>
</manifest>
Step 5 [Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
[Link] {
val intent = Intent(this, MusicService::[Link])
[Link] {
stopService(Intent(this, MusicService::[Link]))
}
}
}
As you run the song by playing PLAY MUSIC make sure the Emulator Volume is full
Practical 8: Database Programing with Sqlite using Kotlin
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
companion object {
private const val DATABASE_NAME = "StudentDB"
private const val DATABASE_VERSION = 1
private const val TABLE_NAME = "Student"
private const val COL_ID = "id"
private const val COL_NAME = "name"
private const val COL_MARKS = "marks"
}
activity_main.xml
<LinearLayout
xmlns:android="[Link]
android:orientation="vertical"
android:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/etName"
android:hint="Enter Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/etMarks"
android:hint="Enter Marks"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnInsert"
android:text="Insert"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnView"
android:text="View Data"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
[Link]
package [Link]
import [Link]
import [Link].*
import [Link]
db = DatabaseHelper(this)
etName = findViewById([Link])
etMarks = findViewById([Link])
btnInsert = findViewById([Link])
btnView = findViewById([Link])
[Link] {
val name = [Link]()
val marks = [Link]().toIntOrNull()
[Link] {
val cursor = [Link]()
if ([Link] == 0) {
[Link](this, "No Data Found", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
activity_main.xml
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Start"
android:textSize="22sp"
android:padding="20dp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Thread"/>
</LinearLayout>
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
val t = findViewById<TextView>([Link])
val b = findViewById<Button>([Link])
[Link] {
val thread = Thread {
for (i in 1..5) {
[Link](1000)
runOnUiThread {
[Link] = "Count: $i"
}
}
}
[Link]()
}
}
}
Practical 10
[Link]
<manifest xmlns:android="[Link]
xmlns:tools="[Link]
<uses-permission android:name="[Link].CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="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>
Activity_main.xml
<LinearLayout
xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="@+id/callButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Direct Call"/>
</LinearLayout>
[Link]
package [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
import [Link]
callNumber()
[Link](savedInstanceState)
setContentView([Link].activity_main)
[Link] {
[Link]([Link].CALL_PHONE)
[Link] = [Link]("[Link]
startActivity(intent)