0% found this document useful (0 votes)
9 views3 pages

Time Picker

This document provides a step-by-step guide to creating an Android application with a TimePicker widget. It includes instructions for setting up the layout in XML, handling time selection in the MainActivity.java file, and running the application to display the selected time. The guide emphasizes the simplicity of implementing a Time Picker and encourages further customization.

Uploaded by

riteshguest
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)
9 views3 pages

Time Picker

This document provides a step-by-step guide to creating an Android application with a TimePicker widget. It includes instructions for setting up the layout in XML, handling time selection in the MainActivity.java file, and running the application to display the selected time. The guide emphasizes the simplicity of implementing a Time Picker and encourages further customization.

Uploaded by

riteshguest
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/ 3

1.

*Create a new Android Project*:

Open Android Studio and create a new Android project.

2. *Add a TimePicker Widget to your Layout*:

Open the activity_main.xml layout file and add the TimePicker widget.

xml

<?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/timePicker"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerInParent="true"

android:timePickerMode="spinner" />

</RelativeLayout>

3. *Handle Time Selection in MainActivity.java*:


Open MainActivity.java and write the code to handle time selection.

java

import android.os.Bundle;

import android.widget.TimePicker;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private TimePicker timePicker;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

timePicker = findViewById(R.id.timePicker);

timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

@Override

public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

// Display the selected time

String selectedTime = hourOfDay + ":" + minute;

Toast.makeText(MainActivity.this, "Selected Time: " + selectedTime,


Toast.LENGTH_SHORT).show();

});
}

4. *Run the Application*:

Run your Android application on an emulator or a physical device. You should see a TimePicker widget
where you can select a time. Once a time is selected, a toast message will display the selected time.

That's it! You have successfully implemented a simple Time Picker in your Android application. You can
further customize the Time Picker widget and handle the selected time as needed for your application.

You might also like