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

Practical 16 MAD

The document provides code examples for two Android applications: one using a TimePicker to select and display time, and another that allows users to select both date and time using dialogs. The first part includes XML layout and Java code for a simple TimePicker interface, while the second part features a layout with buttons to trigger date and time selection dialogs. Both applications demonstrate basic user interaction with time and date inputs in Android development.

Uploaded by

Payal Jadhav
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)
4 views6 pages

Practical 16 MAD

The document provides code examples for two Android applications: one using a TimePicker to select and display time, and another that allows users to select both date and time using dialogs. The first part includes XML layout and Java code for a simple TimePicker interface, while the second part features a layout with buttons to trigger date and time selection dialogs. Both applications demonstrate basic user interaction with time and date inputs in Android development.

Uploaded by

Payal Jadhav
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

Practical No.16.

Program on TimePicker
Xml code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity">
<TimePicker
android:id="@+id/simpleTimePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:padding="20dp"
android:timePickerMode="spinner" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_marginTop="10dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Time Is ::"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/ampm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/time"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp"
android:layout_marginBottom="-46dp"
android:text="AM/PM"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
</RelativeLayout>
Java Code:
package com.example.prac16;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
TextView time;
TimePicker simpleTimePicker;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time = (TextView) findViewById(R.id.time);
simpleTimePicker = (TimePicker)
findViewById(R.id.simpleTimePicker);
simpleTimePicker.setIs24HourView(false);
simpleTimePicker.setOnTimeChangedListener(new
TimePicker.OnTimeChangedListener(){
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int
minute){
Toast.makeText(getApplicationContext(), hourOfDay + " " + minute,
Toast.LENGTH_SHORT).show();time.setText("Time is :: " + hourOfDay + " : "
+ minute);
}
}); }}
Output:
Program on Date and Time
Xml code:
<?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:gravity="center"
android:padding="16dp">
<EditText
android:id="@+id/editText"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:focusable="false"
android:hint="Select Date and Time"
android:textSize="18sp" />
<Button
android:id="@+id/btnDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Select Date" />
<Button
android:id="@+id/btnTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Time"
android:layout_marginTop="10dp" />
</LinearLayout>
Java Code:
package com.example.prac16_1;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
EditText editText;
Button btnDate, btnTime;
Calendar calendar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
btnDate = findViewById(R.id.btnDate);
btnTime = findViewById(R.id.btnTime);
calendar = Calendar.getInstance();
btnDate.setOnClickListener(v -> showDatePicker());
btnTime.setOnClickListener(v -> showTimePicker());
}
private void showDatePicker() {
DatePickerDialog datePickerDialog = new DatePickerDialog(this, (view,
year, month, dayOfMonth) ->
editText.setText(dayOfMonth + "/" + (month + 1) + "/" + year),
calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}
private void showTimePicker() {
TimePickerDialog timePickerDialog = new TimePickerDialog(this, (view,
hourOfDay, minute) ->
editText.append(" " + hourOfDay + ":" + minute),
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE), false);
timePickerDialog.show();
}
}
Output:

You might also like