100% found this document useful (1 vote)
146 views6 pages

Android TimePicker & DatePicker Guide

The document describes a program to display time pickers with different modes and select dates and times using buttons. It includes the XML layout file with TimePickers and buttons, and the Java code file with onclick listeners to show date and time pickers on button clicks and set the selections to text views. The program allows selecting dates on clicking a button using a date picker dialog, and times on another button using a time picker dialog, displaying the results in text views.

Uploaded by

Ganesh Ekambe
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
100% found this document useful (1 vote)
146 views6 pages

Android TimePicker & DatePicker Guide

The document describes a program to display time pickers with different modes and select dates and times using buttons. It includes the XML layout file with TimePickers and buttons, and the Java code file with onclick listeners to show date and time pickers on button clicks and set the selections to text views. The program allows selecting dates on clicking a button using a date picker dialog, and times on another button using a time picker dialog, displaying the results in text views.

Uploaded by

Ganesh Ekambe
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

Name: Mohan Lobhaji Yedatkar

SUB: MAD
Practical No: 16
Roll.No: 58
X. Exercise:
1. Write a Program to display following output. Use TimePicker with Spinnermode.
 activity_pr16_time_picker.xml
android:layout_marginBottom="10dp"
<?xml version="1.0" encoding="utf-8"?>
android:timePickerMode="spinner"
<LinearLayout xmlns:android="http://schemas.android.com
tools:ignore="MissingConstraints" />
/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
<TimePicker
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/timepicker2"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginStart="50dp"
tools:context=".Pr16_TimePicker">
android:layout_marginTop="10dp"
android:layout_marginEnd="50dp"
<ScrollView
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:timePickerMode="spinner" />
android:layout_height="match_parent"
tools:ignore="UselessParent">
<TimePicker
<LinearLayout
android:id="@+id/timepicker3"
android:layout_width="match_parent"
android:layout_width="220dp"
android:layout_height="match_parent"
android:layout_height="365dp"
android:orientation="vertical">
android:layout_marginStart="50dp"
<TimePicker
android:layout_marginTop="10dp"
android:id="@+id/timepicker1"
android:layout_marginEnd="50dp"
android:layout_width="wrap_content"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content"
android:timePickerMode="clock" />
android:layout_marginStart="50dp"
</LinearLayout>
android:layout_marginTop="50dp"
</ScrollView>
android:layout_marginEnd="50dp"
</LinearLayout>
 Pr16_TimePicker.java
package com.example.javaapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TimePicker;

public class Pr16_TimePicker extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pr16_time_picker);
TimePicker timePicker = findViewById(R.id.timepicker2);
timePicker.setIs24HourView(true);
}
}
OUTPUT
2. Write a Program to display following output. Select and display date and time on click
of “select date” , “select time” buttons respectively
 activity_pr16_time_picker2.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" <Button
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/ButtonTime"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="match_parent" android:layout_width="wrap_content"
tools:context=".Pr16_TimePicker2" android:text="Select Time"
android:orientation="vertical"> />
<TableRow </TableRow>
android:layout_marginTop="100dp"
android:layout_gravity="center" </LinearLayout>
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
<TextView
android:id="@+id/inputDate"
android:layout_width="250dp"
android:layout_height="50dp"
android:text="Selected Date: "
android:textSize="20dp"
android:textStyle="bold"
/>

<Button
android:id="@+id/ButtonDate"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Select Date"
/>
</TableRow>
<TableRow
android:layout_marginTop="100dp"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="match_parent"
>
<TextView
android:id="@+id/inputTime"
android:layout_width="250dp"
android:layout_height="50dp"
android:text="Selected Time: "
android:textSize="20dp"
android:textStyle="bold"
/>
 Pr16_TimePicker2.java
package com.example.javaapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import java.util.Calendar;

public class Pr16_TimePicker2 extends AppCompatActivity {


Button TimePicker,Datepicker;
TextView SelTime,SelDate;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pr16_time_picker2);
TimePicker=findViewById(R.id.ButtonTime);
Datepicker=findViewById(R.id.ButtonDate);
SelDate=findViewById(R.id.inputDate);
SelTime=findViewById(R.id.inputTime);

Datepicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDatePicker();
}
});
TimePicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showTimePicker();

}
});

}
private void showDatePicker() {
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
String selectedDate = dayOfMonth + "/" + (month + 1) + "/" + year;
SelDate.setText("Selected Date: " + selectedDate);
}
}, year, month, day);
datePickerDialog.show();
}
private void showTimePicker() {
final Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener()
{
@Override
public void onTimeSet(android.widget.TimePicker view, int hourOfDay, int minute) {
String selectedTime = hourOfDay + ":" + minute;
SelTime.setText("Selected Time: " + selectedTime);
}
}, hour, minute, false);
timePickerDialog.show();
}

}
OUTPUT

You might also like