Professional Documents
Culture Documents
Experiment 05
Experiment 05
Experiment 05
Aim: To write a program to design Date and Time Picker in Android Studio.
Theory:
Android provides controls for the user to pick a time or pick a date as ready-to-use dialogs.
Each picker provides controls for selecting each part of the time (hour, minute, AM/PM) or
date (month, day, year). Using these pickers helps ensure that your users can pick a time or
date that is valid, formatted correctly, and adjusted to the user's locale.
The DialogFragment manages the dialog lifecycle for you and allows you to display the
pickers in different layout configurations, such as in a basic dialog on handsets or as an
embedded part of the layout on large screens.
Although DialogFragment was first added to the platform in Android 3.0 (API level 11), if
your app supports versions of Android older than 3.0—even as low as Android 1.6—you can
use the DialogFragment class that's available in the support library for backward
compatibility.
Implementation:
MainActivity.java
package com.example.exp5; import android.widget.DatePicker;
tp =(Button)findViewById(R.id.b2); datePickerDialog.show();
txtDate=(EditText)findViewById(R.id.e1); }
txtTime=(EditText)findViewById(R.id.e2); if (v == tp) {
} mMinute = c.get(Calendar.MINUTE);
if (hourOfDay==0) timePickerDialog.show();
hourOfDay=12; }}
hourOfDay=hourOfDay-12; if (i > 9)
else
txtTime.setText(zeroX(hourOfDay) + ":" +
zeroX(minute)+" "+AM_PM); str = "0" + Integer.toString(i);
return str;
}}
activity_main.xml
<RelativeLayout android:gravity="center"
android:gravity="center_horizontal"
android:layout_marginTop="25dp" <EditText
xmlns:android="http://schemas.android.com android:id="@+id/e1"
/apk/res/android"> android:layout_width="150dp"
android:layout_height="wrap_content"
<TextView android:layout_marginTop="60dp"
android:layout_width="wrap_content" android:layout_marginRight="10dp"
android:fontFamily="serif" android:layout_marginBottom="20dp"
android:gravity="center"/> android:layout_marginRight="10dp"
<Button android:fontFamily="serif"
android:layout_width="wrap_content" android:gravity="center"/>
android:layout_height="wrap_content" <Button
android:paddingLeft="10dp" android:id="@+id/b2"
android:paddingRight="10dp" android:layout_width="wrap_content"
android:fontFamily="serif" android:textSize="20dp"
android:layout_toRightOf="@id/e1" android:paddingLeft="10dp"
android:layout_alignBaseline="@id/e1" android:paddingRight="10dp"
android:textColor="@color/white"/> android:fontFamily="serif"
<EditText android:layout_toRightOf="@id/e2"
android:id="@+id/e2" android:layout_alignBaseline="@id/e2"
android:layout_width="150dp" android:textColor="@color/white"/>
android:layout_height="wrap_content" </RelativeLayout>
android:layout_below="@id/b1"
Output:
Conclusion: