Android Programming Lab
Practical 08
Aim: To develop an Android application using Alert Dialog, Date Picker, and Time Picker.
Theory: Dialogs are used in Android to interact with users and get confirmation or input. AlertDialog is used
to display messages and options. DatePickerDialog allows users to select a date, while TimePickerDialog
allows users to select time. These components enhance user interaction in applications.
Procedure:
1. Open Android Studio
2. Create a new project
3. Design layout with buttons (Dialog, Date, Time)
4. Implement AlertDialog
5. Implement DatePickerDialog
6. Implement TimePickerDialog
7. Display selected values in TextView
8. Run the application
Code:
Below is the implementation of the application:
• [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MainActivity extends AppCompatActivity {
Button btnDialog, btnDate, btnTime;
TextView txtResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
[Link](this);
setContentView([Link].activity_main);
1
Android Programming Lab
[Link](findViewById([Link]), (v, insets) ->
{
Insets systemBars = [Link]([Link]());
[Link]([Link], [Link], [Link],
[Link]);
return insets;
});
btnDialog = findViewById([Link]);
btnDate = findViewById([Link]);
btnTime = findViewById([Link]);
txtResult = findViewById([Link]);
// Alert Dialog
[Link](new [Link]() {
@Override
public void onClick(View view) {
[Link] builder = new [Link]([Link]);
[Link]("Confirmation");
[Link]("Do you want to continue?");
[Link]("Yes", (dialog, which) -> {
[Link]([Link], "You clicked YES",
Toast.LENGTH_SHORT).show();
});
[Link]("No", (dialog, which) -> {
[Link]([Link], "You clicked NO",
Toast.LENGTH_SHORT).show();
});
[Link]();
}
});
// Date Picker
[Link](view -> {
Calendar calendar = [Link]();
int year = [Link]([Link]);
int month = [Link]([Link]);
int day = [Link](Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(
[Link], (view1, year1, month1, dayOfMonth) -> {
[Link]("Selected Date: " + dayOfMonth + "/" + (month1 + 1) +
"/" + year1);
}, year, month, day
);
[Link]();
});
// Time Picker
[Link](view -> {
Calendar calendar = [Link]();
int hour = [Link](Calendar.HOUR_OF_DAY);
int minute = [Link]([Link]);
TimePickerDialog timePickerDialog = new TimePickerDialog(
[Link], (view12, hourOfDay, minute1) -> {
[Link]("Selected Time: " + hourOfDay + ":" + minute1);
}, hour, minute, true
);
[Link]();
});
}
}
• [Link]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
2
Android Programming Lab
android:padding="20dp">
<Button
android:id="@+id/btnDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Dialog" />
<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:layout_marginTop="20dp"
android:text="Select Time" />
<TextView
android:id="@+id/txtResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Result will appear here"
android:textSize="18sp" />
</LinearLayout>
Output:
• Dialog box appears on button click
• Date picker allows selecting date
• Time picker allows selecting time
• Selected values displayed on screen
3
Android Programming Lab
4
Android Programming Lab
Result: The application successfully implemented dialog boxes, date picker, and time picker for user
interaction.