Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
18 views6 pages

EXP 16 To 18

The document contains code snippets for Android applications, including progress simulation, date and time selection, and URL navigation. It demonstrates the use of XML layouts and Kotlin for handling user interactions like button clicks and displaying selected values. Additionally, it covers activity lifecycle methods and intent usage for navigating between screens.

Uploaded by

gaurav singh
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)
18 views6 pages

EXP 16 To 18

The document contains code snippets for Android applications, including progress simulation, date and time selection, and URL navigation. It demonstrates the use of XML layouts and Kotlin for handling user interactions like button clicks and displaying selected values. Additionally, it covers activity lifecycle methods and intent usage for navigating between screens.

Uploaded by

gaurav singh
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
You are on page 1/ 6

EXP 16 // Simulating progress

Thread {
XII
while (progressStatus < 100)
Q1 Ans: progressStatus += 5
handler.post {
activity_main.xml
progressBar.progress =
<?xml version="1.0" encoding="utf-8"?> progressStatus
}
<LinearLayout
Thread.sleep(500) // Delay for progress
xmlns:android="http://schemas.android.com/a
simulation }}.start()
pk/res/android"
}
android:layout_width="match_parent" }
android:layout_height="match_parent" Out Put:
android:gravity="center"
android:orientation="vertical">
<!-- Circular Progress Bar -->
<ProgressBar
android:id="@+id/progressBarCircular"

style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"/>
</LinearLayout>
MainActivity.kt
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.ProgressBar
import
androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var progressBar: ProgressBar
private var progressStatus = 0
private val handler =
Handler(Looper.getMainLooper())
override fun onCreate(savedInstanceState:
Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
progressBar =
indViewById(R.id.progressBarCircular)
Q2 Ans: private lateinit var btnSelectTime: Button
override fun onCreate(savedInstanceState:
<?xml version="1.0" encoding="utf-8"?>
Bundle?) {
<LinearLayout
super.onCreate(savedInstanceState)
xmlns:android="http://schemas.android.com/a
setContentView(R.layout.activity_main)
pk/res/android"
android:layout_width="match_parent"
// Initialize UI Elements
android:layout_height="match_parent"
tvDate = findViewById(R.id.tvDate)
android:orientation="vertical"
tvTime = findViewById(R.id.tvTime)
android:gravity="center"
btnSelectDate =
android:padding="20dp">
findViewById(R.id.btnSelectDate)
<!-- TextView to Display Selected Date -->
btnSelectTime =
<TextView
findViewById(R.id.btnSelectTime)
android:id="@+id/tvDate"
android:layout_width="match_parent"
// Handle Date Selection
android:layout_height="wrap_content"
btnSelectDate.setOnClickListener {
android:text="Selected Date: "
val calendar = Calendar.getInstance()
android:textSize="18sp"
val year = calendar.get(Calendar.YEAR)
android:padding="10dp"/>
val month =
<!-- Button to Open Date Picker -->
calendar.get(Calendar.MONTH)
<Button
val day =
android:id="@+id/btnSelectDate"
calendar.get(Calendar.DAY_OF_MONTH)
android:layout_width="wrap_content"
android:layout_height="wrap_content"
val datePickerDialog =
android:text="SELECT DATE"/>
DatePickerDialog(this, { _, selectedYear,
<!-- TextView to Display Selected Time -->
selectedMonth, selectedDay ->
<TextView
tvDate.text = "Selected Date:
android:id="@+id/tvTime"
$selectedDay/${selectedMonth +
android:layout_width="match_parent"
1}/$selectedYear"
android:layout_height="wrap_content"
}, year, month, day)
android:text="Selected Time: "
android:textSize="18sp"
datePickerDialog.show()
android:padding="10dp"/>
}
<!-- Button to Open Time Picker -->
// Handle Time Selection
<Button
btnSelectTime.setOnClickListener {
android:id="@+id/btnSelectTime"
val calendar = Calendar.getInstance()
android:layout_width="wrap_content"
val hour =
android:layout_height="wrap_content"
calendar.get(Calendar.HOUR_OF_DAY)
android:text="SELECT TIME"/>
val minute =
</LinearLayout> calendar.get(Calendar.MINUTE)
import android.app.DatePickerDialog
import android.app.TimePickerDialog val timePickerDialog =
import android.os.Bundle TimePickerDialog(this, { _, selectedHour,
import android.widget.Button selectedMinute ->
import android.widget.TextView tvTime.text = "Selected Time:
import $selectedHour:$selectedMinute"
androidx.appcompat.app.AppCompatActivity }, hour, minute, true)
import java.util.*
class MainActivity : AppCompatActivity() { timePickerDialog.show()
private lateinit var tvDate: TextView }
private lateinit var tvTime: TextView }
private lateinit var btnSelectDate: Button }
EXP 17 }
XII }
Q1 Ans: <?xml version="1.0" encoding="utf-8"?>
MainActivity.kt <LinearLayout
import android.os.Bundle xmlns:android="http://schemas.android.com/a
import android.util.Log pk/res/android"
android:layout_width="match_parent"
import
android:layout_height="match_parent"
androidx.appcompat.app.AppCompatActivi
android:orientation="vertical"
ty android:gravity="center">
class MainActivity : AppCompatActivity() {
private val TAG = "LifecycleMethods <TextView
override fun android:layout_width="wrap_content"
onCreate(savedInstanceState: Bundle?) { android:layout_height="wrap_content"
super.onCreate(savedInstanceState) android:text="Hello, World!"
setContentView(R.layout.activity_main) android:textSize="24sp"
Log.d(TAG, "onCreate: Activity is android:textStyle="bold"/>
created")
</LinearLayout>
}
<?xml version="1.0" encoding="utf-8"?>
override fun onStart() {
<LinearLayout
super.onStart() xmlns:android="http://schemas.android.com/a
Log.d(TAG, "onStart: Activity is visible") pk/res/android"
} android:layout_width="match_parent"
override fun onResume() { android:layout_height="match_parent"
super.onResume() android:orientation="vertical"
Log.d(TAG, "onResume: Activity is in android:gravity="center">
foreground and interactive")
} <TextView
override fun onPause() { android:layout_width="wrap_content"
android:layout_height="wrap_content"
super.onPause()
android:text="Hello, World!"
Log.d(TAG, "onPause: Activity is
android:textSize="24sp"
partially visible (Another activity is on android:textStyle="bold"/>
top)")
} </LinearLayout>
override fun onStop() {
super.onStop() OUT PUT:
Log.d(TAG, "onStop: Activity is no
longer visible")
}
override fun onRestart() {
super.onRestart()
Log.d(TAG, "onRestart: Activity is
restarting")
}
override fun onDestroy() {
super.onDestroy()
Log.d(TAG, "onDestroy: Activity is
destroyed")
EXP18 val fullUrl = if (!url.startsWith("http://")
XII && !url.startsWith("https://")) {
Q1 Ans: "https://$url"
<?xml version="1.0" encoding="utf-8"?> } else {
<LinearLayout url
xmlns:android="http://schemas.android.com/a }
pk/res/android" // Create an intent to open the URL in a
android:layout_width="match_parent" browser
android:layout_height="match_parent" val intent = Intent(Intent.ACTION_VIEW,
android:orientation="vertical" Uri.parse(fullUrl))
android:gravity="center" startActivity(intent)
android:padding="20dp"> }
}
<!-- Input field for URL --> }
<EditText
android:id="@+id/etUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter URL"
android:text="https://www.google.com"
android:inputType="textUri"/>

<!-- Button to navigate -->


<Button
android:id="@+id/btnNavigate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Navigate"
android:layout_marginTop="20dp"/>
</LinearLayout>
import android.content.Intent
import android.net.Uri
Q2 Ans :
import android.os.Bundle <?xml version="1.0" encoding="utf-
import android.widget.Button 8"?>
import android.widget.EditText <LinearLayout
import xmlns:android="http://schemas.android.com/a
androidx.appcompat.app.AppCompatActivity pk/res/android"
class MainActivity : AppCompatActivity() { android:layout_width="match_parent"
override fun onCreate(savedInstanceState:
android:layout_height="match_parent"
Bundle?) {
android:gravity="center"
super.onCreate(savedInstanceState) android:orientation="vertical">
setContentView(R.layout.activity_main) <!-- Button to Open Dialer -->
val etUrl = <Button
findViewById<EditText>(R.id.etUrl)
android:id="@+id/btnStartDialer"
val btnNavigate =
android:layout_width="wrap_content"
findViewById<Button>(R.id.btnNavigate) android:layout_height="wrap_content"
android:text="Start Dialer"/>
btnNavigate.setOnClickListener {
</LinearLayout>
val url = etUrl.text.toString()
import android.content.Intent
import android.net.Uri
// Ensure URL starts with "http://" or import android.os.Bundle
"https://"
import android.widget.Button <Button
import android:id="@+id/btnFactorial"
androidx.appcompat.app.AppCompatActivity android:layout_width="wrap_content"
class MainActivity : AppCompatActivity() { android:layout_height="wrap_content"
override fun onCreate(savedInstanceState: android:text="Factorial"
Bundle?) { android:layout_marginTop="20dp"/>
super.onCreate(savedInstanceState) </LinearLayout>
setContentView(R.layout.activity_main) import android.content.Intent
val btnStartDialer = import android.os.Bundle
findViewById<Button>(R.id.btnStartDialer) import android.widget.Button
btnStartDialer.setOnClickListener { import android.widget.EditText
val intent = Intent(Intent.ACTION_DIAL) import android.widget.Toast
// Opens the dialer import
intent.data = Uri.parse("tel:") // Empty androidx.appcompat.app.AppCompatActivity
number, opens dialer only class MainActivity : AppCompatActivity() {
startActivity(intent)} }} override fun onCreate(savedInstanceState:
Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
l etNumber =
findViewById<EditText>(R.id.etNumber)
val btnFactorial =
findViewById<Button>(R.id.btnFactorial)
btnFactorial.setOnClickListener {
val numberStr =
etNumber.text.toString()
if (numberStr.isNotEmpty()) {
val number = numberStr.toInt()
val intent = Intent(this,
FactorialActivity::class.java)
intent.putExtra("number", number) //
Passing data using intent
Q3 Ans: startActivity(intent) // Navigate to
<?xml version="1.0" encoding="utf-8"?> second screen
<LinearLayout } else {
xmlns:android="http://schemas.android.com/a Toast.makeText(this, "Please enter a
pk/res/android" number", Toast.LENGTH_SHORT).show()
android:layout_width="match_parent" }
android:layout_height="match_parent" }
android:orientation="vertical" }
android:gravity="center" OUTPUT:
android:padding="20dp">

<!-- Input field for number -->


<EditText
android:id="@+id/etNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a number"
android:inputType="number"/>

<!-- Button to navigate to second screen -->

You might also like