Activity - Main - XML: Practical # 1 Introduction To Android, Android Studio Ide Application Fundamentals
Activity - Main - XML: Practical # 1 Introduction To Android, Android Studio Ide Application Fundamentals
activity_main.xml
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello students!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
______________________________________________________________________
__________________________
MainActivity.kt
package com.example.practical11
import android.content.Context
import android.net.ConnectivityManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<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.Practical11"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Practical # 2
Programing resources: color,theme, string, drawable, dimension,image
strings.xml
<resources>
<string name="app_name">Android Resources</string>
<string name="my_text">TYIT</string>
</resources>
___________________________________________________________________
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#FFEB3B</color>
<color name="colorPrimaryDark">#18463F</color>
<color name="colorAccent">#D81B60</color>
</resources>
______________________________________________________________________
themes.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.Practical21"
parent="Theme.AppCompat.DayNight.DarkActionBar">
<!-- Customize your light theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
Practical 3
Programing activities and fragments, activity life cycle, method, multiple activities
life cycle of fragments and multiple fragments
activity_main.xml
<?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">
<!-- <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello TYIT"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> -->
<Button
android:id="@+id/about_us"
android:layout_width="130dp"
android:layout_height="102dp"
android:text="AboutUs"
tools:layout_editor_absoluteX="154dp"
tools:layout_editor_absoluteY="323dp"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
___________________________________________________________________
MainActivity.kt
package com.example.myapplication111
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
package com.example.myapplication111
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
}
}
______________________________________________________________________
_________________________
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<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.MyApplication111"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Practical 4
Programing related to different layouts: linear, relative, table
Linear Layout
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="@color/material_dynamic_primary70"
>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<EditText
android:id="@+id/editTextText2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="@string/search"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/search" />
</LinearLayout>
<EditText
android:id="@+id/editTextText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"
android:text="Name" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="164dp"
android:layout_gravity="bottom"
app:srcCompat="@drawable/ic_launcher_background" />
</LinearLayout>
____________________________________________________________________
Relative Layout
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="@color/material_dynamic_primary70"
>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<EditText
android:id="@+id/editTextText2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="@string/search"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/search" />
</LinearLayout>
<EditText
android:id="@+id/editTextText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text"
android:text="Name" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="164dp"
android:layout_gravity="bottom"
app:srcCompat="@drawable/ic_launcher_background" />
</RelativeLayout>
______________________________________________________________________
_______
Table Layout(activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="150dp"
android:layout_marginLeft="50dp">
<EditText
android:id="@+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="username"
android:inputType="text" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp">
<EditText
android:id="@+id/editTextText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="passward" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp">
<Button
android:text="Login"
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
</TableLayout>
_______________________________________________________
Table Layout(MainActivity.kt)
package com.example.myapplicationtable
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
var text=findViewById<EditText>(R.id.mytext)
var btn=findViewById<Button>(R.id.login)
btn.setOnClickListener{
var strMessage:String
strMessage=text.getText().toString()
Toast.makeText(this,"hello $strMessage",Toast.LENGTH_LONG).show()
}
}
}
______________________________________________________________________
_____________
PRACTICAL NO 5
Programing ui elements
ACTIVITY_MAIN.XML
No changes
MainActivity.kt
package com.example.appbar
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.appbar.R.menu.menu
menuInflater.inflate(R.menu.menu,menu)
return super.onCreateOptionsMenu(menu)
}
if (id==R.id.search)
{
Toast.makeText(this,"search",Toast.LENGTH_LONG).show()
}
if (id==R.id.aboutus)
{
Toast.makeText(this,"AboutUs",Toast.LENGTH_LONG).show()
}
if (id==R.id.contactus)
{
Toast.makeText(this,"Contact Us",Toast.LENGTH_LONG).show()
}
return super.onOptionsItemSelected(item)
}
}
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/search"
android:title="@string/search"
app:showAsAction="always" />
<item
android:id="@+id/aboutus"
android:title="@string/about_us" />
<item
android:id="@+id/contactus"
android:title="@string/contact_us" />
</menu>
String.xml
<resources>
<string name="search">Search</string>
<string name="about_us">About Us</string>
<string name="contact_us">Contact Us</string>
</resources>
Design part
PRACTICAL 6 : FRAGMENT
ACTIVITY_MAIN.XML
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="432dp"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:layout_editor_absoluteX="-19dp"
tools:layout_editor_absoluteY="54dp">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView"
android:name="com.example.fragment.Fragmentone"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView2"
android:name="com.example.fragment.Fragmenttwo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MAINACTIVITY.KT
package com.example.fragment
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
package com.example.fragment
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
/**
* A simple [Fragment] subclass.
* Use the [Fragmentone.newInstance] factory method to
* create an instance of this fragment.
*/
class Fragmentone : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragmentone, container, false)
}
FRAGMENTTWO.KT
package com.example.fragment
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
/**
* A simple [Fragment] subclass.
* Use the [Fragmenttwo.newInstance] factory method to
* create an instance of this fragment.
*/
class Fragmenttwo : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragmenttwo, container, false)
}
FRAGMENTONE.XML
</FrameLayout>
FRAGMENTTWO.XML
</FrameLayout>
OUTPUT
Activity_main.xml
MainActivity.xml
package com.example.intent
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge() // Make sure this is available for
your Android version
setContentView(R.layout.activity_main)
// Initialize buttons
val btn1 = findViewById<Button>(R.id.btn1)
val btn2 = findViewById<Button>(R.id.btn2)
val btn3 = findViewById<Button>(R.id.btn3)
val btn4 = findViewById<Button>(R.id.btn4)
val btn5 = findViewById<Button>(R.id.btn5)
val btn6 = findViewById<Button>(R.id.btn6)
val btn7 = findViewById<Button>(R.id.btn7)
val btn8 = findViewById<Button>(R.id.btn8)
val btn9 = findViewById<Button>(R.id.btn9)
// Set click listeners
btn1.setOnClickListener { toast("1") }
btn2.setOnClickListener { toast("2") }
btn3.setOnClickListener { toast("3") }
btn4.setOnClickListener { toast("4") }
btn5.setOnClickListener { toast("5") }
btn6.setOnClickListener { toast("6") }
btn7.setOnClickListener { toast("7") }
btn8.setOnClickListener { toast("8") }
btn9.setOnClickListener { toast("9") }
}
private fun toast(s: String) {
Toast.makeText(this, s, Toast.LENGTH_SHORT).show()
}
}
Output
Practical 8
Notification:
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp"
android:gravity="center_vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/btnGetNonClickableNotification"
android:layout_width="match_parent"
android:textSize="20sp"
android:padding="15dp"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnGetClickableNotification"
android:layout_width="match_parent"
android:textSize="20sp"
android:padding="15dp"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.kt
package com.example.notification
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
createNotificationChannel()
getClickableNotificationButton =
findViewById(R.id.btnGetClickableNotification)
getNonClickableNotificationButton =
findViewById(R.id.btnGetNonClickableNotification)
getClickableNotificationButton.setOnClickListener {
showClickableNotification()
getNonClickableNotificationButton.setOnClickListener {
showNonClickableNotification()
}
}
description = descriptionText
getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
PendingIntent.FLAG_IMMUTABLE)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("WELCOME TO SN COLLEGE")
.setContentIntent(pendingIntent)
.setAutoCancel(true)
getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(NOTIFICATION_ID_CLICKABLE, builder.build())
}
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("HELLO TYIT")
.setAutoCancel(true)
getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(NOTIFICATION_ID_NON_CLICKABLE, builder.build())
Androidmanifest.xml
Output
Practical 8 :
Notification
<LinearLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="20dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/infoText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textStyle="bold"
android:textSize="30sp"
</LinearLayout>
AndroidManifest.xml
Mainactivity.kt
package com.example.broadcast
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
infoText = findViewById(R.id.infoText)
getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
when {
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> {
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> {
else -> {
} else {
output:
Practical 10: Programming Security and permissions
AndroidManifest.xml
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission
android:name="android.permission.READ_CONTACTS"/>
<uses-permission
android:name="android.permission.READ_CALENDAR"/>
Activity_main.xml
MainActivity.kt
package com.example.permission
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class MainActivity : AppCompatActivity() {
lateinit var cameraBtn: Button
lateinit var calendarBtn: Button
lateinit var contactsBtn: Button
private val MY_CAMERA_REQUEST_CODE = 100;
private val MY_CALENDAR_REQUEST_CODE = 101;
private val MY_CONTACTS_REQUEST_CODE = 102;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
cameraBtn = findViewById(R.id.cameraButton)
calendarBtn = findViewById(R.id.calendarButton)
contactsBtn = findViewById(R.id.contactsButton)
cameraBtn.setOnClickListener {
checkAndRequestPermission("Camera",
Manifest.permission.CAMERA,MY_CAMERA_REQUEST_CODE)
}
calendarBtn.setOnClickListener {
checkAndRequestPermission("Calendar",
Manifest.permission.READ_CALENDAR,MY_CALENDAR_REQUEST_CODE)
}
contactsBtn.setOnClickListener {
checkAndRequestPermission("Contacts",
Manifest.permission.READ_CONTACTS,
MY_CONTACTS_REQUEST_CODE)
}
}
private fun checkAndRequestPermission(whichButton: String,
whichPermission:
String, requestCode: Int) {
if (checkSelfPermission(whichPermission) !=
PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "$whichButton Button Clicked",
Toast.LENGTH_SHORT).show()
requestPermissions(arrayOf(whichPermission),
requestCode);
} else {
Toast.makeText(this, "Already has $whichButton
Access",
Toast.LENGTH_SHORT).show()
}
}
}
Output