Android Time picker example
How to pick Time from the user in android? Write an android example to pick Time from the user and display the date to the user. Android provides controls for the user to pick a Time through a pre-defined dialog box. Picker provides controls for selecting each part of the Time i.e. hour, and minute. Picker time helps to ensure that users can pick a date that is valid, formatted correctly, and adjusted to the user's locale. In this tutorial, we are going to ask the user to input the Time with the help of Time picker and with the simple one click show button user can view the Time entered. Time class is required to hold the data set by the picker. Data members inside the Time class are static to access the data from the MainActivity. Static data members of the class were created only for the first time when the object of that class has been created. Use DialogFragment class to host each time or date picker. The DialogFragment class manages the dialog lifecycle for you and allows you to display the pickers in different layout configurations. public class TimePickerDialog extends AlertDialog implements DialogInterface.OnClickListener,TimePicker.OnTimeChangedListener
java.lang.Object | |||
? | android.app.Dialog | ||
? | android.app.AlertDialog | ||
? | android.app.TimePickerDialog |
package com.example.android.tutorialsandexample; import android.support.v4.app.DialogFragment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { EditText editText; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final DialogFragment newFragment = new TimePickerFragment(); newFragment.show(getSupportFragmentManager(), "timePicker"); editText=findViewById(R.id.editText); button=findViewById(R.id.show); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Time dv=new Time(); String time=new String(); time=""; time=dv.getHourOfDay()+":"+dv.getMinute();//insert the hour and minute editText.setText(time);// DIsplay the time to the user } }); } }JAVA FILE : TimePickerFragment
package com.example.android.tutorialsandexample; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.TimePickerDialog; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.text.format.DateFormat; import android.widget.DatePicker; import android.widget.TimePicker; import java.util.Calendar; public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current time as the default values for the picker final Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); // Create a new instance of TimePickerDialog and return it return new TimePickerDialog(getActivity(), this, hour, minute, DateFormat.is24HourFormat(getActivity())); } public void onTimeSet(TimePicker view, int hourOfDay, int minute) { // Do something with the time chosen by the user Time time=new Time(); time.setHourOfDay(hourOfDay); time.setMinute(minute); } }JAVA FILE: Time.java
package com.example.android.tutorialsandexample; public class Time { static int hourOfDay; static int minute; public int getHourOfDay() { return hourOfDay; } public void setHourOfDay(int hourOfDay) { this.hourOfDay = hourOfDay; } public int getMinute() { return minute; } public void setMinute(int minute) { this.minute = minute; } } XML FILE:<?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="time" android:text="This is blank text"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show time" android:id="@+id/show"/> </LinearLayout>