0% found this document useful (0 votes)
7 views12 pages

MAD Programs

Uploaded by

Amit Upadhyay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views12 pages

MAD Programs

Uploaded by

Amit Upadhyay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

1. Write a program to demonstrate Date and Time picker.

Ans.
 XML File:
<?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">

<!-- Button to open DatePickerDialog -->


<Button
android:id="@+id/btnPickDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Date"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"/>

<!-- Button to open TimePickerDialog -->


<Button
android:id="@+id/btnPickTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick Time"
android:layout_below="@id/btnPickDate"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"/>

<!-- TextView to display selected date and time -->


<TextView
android:id="@+id/tvSelectedDateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Selected Date and Time will be displayed here"
android:textSize="18sp"
android:layout_below="@id/btnPickTime"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"/>
</RelativeLayout>

 Java Code:
package com.example.dateandtimepicker

import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import java.util.*

class MainActivity : AppCompatActivity() {

private lateinit var tvSelectedDateTime: TextView


private lateinit var btnPickDate: Button
private lateinit var btnPickTime: Button

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initialize UI components
tvSelectedDateTime = findViewById(R.id.tvSelectedDateTime)
btnPickDate = findViewById(R.id.btnPickDate)
btnPickTime = findViewById(R.id.btnPickTime)

// Set up the Date Picker


btnPickDate.setOnClickListener {
val calendar = Calendar.getInstance()
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH)
val dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH)

val datePickerDialog = DatePickerDialog(this, { _, selectedYear,


selectedMonth, selectedDayOfMonth ->
// Format the selected date
val formattedDate = "${selectedDayOfMonth}/${selectedMonth +
1}/$selectedYear"
tvSelectedDateTime.text = "Selected Date: $formattedDate"
}, year, month, dayOfMonth)

datePickerDialog.show()
}

// Set up the Time Picker


btnPickTime.setOnClickListener {
val calendar = Calendar.getInstance()
val hour = calendar.get(Calendar.HOUR_OF_DAY)
val minute = calendar.get(Calendar.MINUTE)
val timePickerDialog = TimePickerDialog(this, { _, selectedHour,
selectedMinute ->
// Format the selected time
val formattedTime = String.format("%02d:%02d", selectedHour,
selectedMinute)
tvSelectedDateTime.text = "Selected Time: $formattedTime"
}, hour, minute, true)

timePickerDialog.show()
}
}
}

2. Write a program to create a Hello World Activity using all lifecycles method to
display messages using Log.d.
Ans.
 XML File:
<?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/helloWorldText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textSize="24sp"
android:layout_centerInParent="true"/>
</RelativeLayout>

 Java Code:
package com.example.helloworldactivity

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

// Tag for log messages


private val TAG = "MainActivityLifecycle"

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Log the creation lifecycle method


Log.d(TAG, "onCreate called")
}

override fun onStart() {


super.onStart()
// Log the start lifecycle method
Log.d(TAG, "onStart called")
}

override fun onResume() {


super.onResume()
// Log the resume lifecycle method
Log.d(TAG, "onResume called")
}

override fun onPause() {


super.onPause()
// Log the pause lifecycle method
Log.d(TAG, "onPause called")
}

override fun onStop() {


super.onStop()
// Log the stop lifecycle method
Log.d(TAG, "onStop called")
}

override fun onRestart() {


super.onRestart()
// Log the restart lifecycle method
Log.d(TAG, "onRestart called")
}

override fun onDestroy() {


super.onDestroy()
// Log the destroy lifecycle method
Log.d(TAG, "onDestroy called")
}
}
3. Write a program to calculate factorial of a given number.
Ans.
 XML File:
<?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">

<!-- EditText to input the number -->


<EditText
android:id="@+id/etNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter a number"
android:inputType="number"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"/>

<!-- Button to calculate the factorial -->


<Button
android:id="@+id/btnCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate Factorial"
android:layout_below="@id/etNumber"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>

<!-- TextView to display the result -->


<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Factorial will be displayed here"
android:textSize="18sp"
android:layout_below="@id/btnCalculate"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"/>

</RelativeLayout>

 Java Code:
package com.example.factorialapp

import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import android.widget.Toast

class MainActivity : AppCompatActivity() {

private lateinit var etNumber: EditText


private lateinit var btnCalculate: Button
private lateinit var tvResult: TextView

override fun onCreate(savedInstanceState: Bundle?) {


super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// Initialize UI components
etNumber = findViewById(R.id.etNumber)
btnCalculate = findViewById(R.id.btnCalculate)
tvResult = findViewById(R.id.tvResult)

// Set button click listener


btnCalculate.setOnClickListener {
val number = etNumber.text.toString()

if (number.isNotEmpty()) {
val num = number.toInt()
if (num >= 0) {
val result = factorial(num)
tvResult.text = "Factorial of $num is: $result"
} else {
Toast.makeText(this, "Please enter a non-negative number",
Toast.LENGTH_SHORT).show()
}
} else {
Toast.makeText(this, "Please enter a number",
Toast.LENGTH_SHORT).show()
}
}
}

// Function to calculate factorial recursively


private fun factorial(n: Int): Long {
if (n == 0 || n == 1) {
return 1
}
return n * factorial(n - 1)
}
}

4. Write a program to create a text field and a button “Navigate”. Whenyou enter
“www.google.com” and press navigate button it should open google page.
Ans.
XML File:
<?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=”20dp”>

<EditText
Android:id=”@+id/urlEditText”
Android:layout_width=”match_parent”
Android:layout_height=”wrap_content”
Android:hint=”Enter URL”
Android:inputType=”textUri”/>

<Button
Android:id=”@+id/navigateButton”
Android:layout_width=”wrap_content”
Android:layout_height=”wrap_content”
Android:text=”Navigate”
Android:layout_gravity=”center”
Android:padding=”10dp”/>
</LinearLayout>

Java Code:

Import android.content.Intent;
Import android.net.Uri;
Import android.os.Bundle;
Import android.view.View;
Import android.widget.Button;
Import android.widget.EditText;
Import android.widget.Toast;
Import androidx.appcompat.app.AppCompatActivity;

Public class MainActivity extends AppCompatActivity {


Private EditText urlEditText;
Private Button navigateButton;

@Override
Protected void onCreate(Bundle savedInstanceState) {
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

urlEditText = findViewById(R.id.urlEditText);
navigateButton = findViewById(R.id.navigateButton);

navigateButton.setOnClickListener(new View.OnClickListener() {
@Override
Public void onClick(View v) {
String url = urlEditText.getText().toString().trim();
// Ensure URL is valid
If (!url.startsWith(http://) && !url.startsWith(https://)) {
url = https:// + url;
}

Try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
} catch (Exception e) {
Toast.makeText(MainActivity.this, “Invalid URL”, Toast.LENGTH_SHORT).show();
}
}
});
}
}

5.

You might also like