Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android datepicker get date and time

I am using datepicker for reserving a table in a restaurant

I have to use two date pickers, one to get date and one to get time

but its not user friendly i want to use both of them in the same time

public class MainActivity extends AppCompatActivity {

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Calendar c = Calendar.getInstance();
    final int year = c.get(Calendar.YEAR);
    final int month = c.get(Calendar.MONTH);
    final int day = c.get(Calendar.DAY_OF_MONTH);
    final int hour = c.get(Calendar.HOUR_OF_DAY);
    final int minute1 = c.get(Calendar.MINUTE);
    final Calendar v = Calendar.getInstance();
    final int year2 = v.get(Calendar.YEAR);
    final int month2 = v.get(Calendar.MONTH);
    final int day2 = v.get(Calendar.DAY_OF_MONTH);
    final int hour2 = v.get(Calendar.HOUR_OF_DAY);
    final int minute2 = v.get(Calendar.MINUTE);

    final String date;
    final EditText rt1 = (EditText) findViewById(R.id.f1);
    final EditText et1 = (EditText) findViewById(R.id.e1);
    et1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DatePickerDialog datepick = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

                }
            } ,year,month,day);
            datepick.setTitle("select date");
            datepick.show();
        }
    rt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TimePickerDialog timepick = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {


                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    rt1.setText(hourOfDay + ":" + minute);


                }
            }, hour, minute1, true
            );
            timepick.setTitle("select time");
            timepick.show();


        }
    });

}}
like image 730
Omar Elshirbini Avatar asked Mar 26 '26 03:03

Omar Elshirbini


2 Answers

There is no build-in Picker that can do this.

But have a look at the SublimePicker on GitHub.

like image 98
Chris Avatar answered Mar 28 '26 15:03

Chris


Android doesn't have anything built-in to do this, you can create your own date and time picker. Below is the code to do it:

date_time_picker_dialog.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="8dp"
android:layout_height="match_parent">

<DatePicker
    android:id="@+id/date_picker"
    android:layout_width="match_parent"
    android:calendarViewShown="true"
    android:spinnersShown="false"
    android:layout_weight="4"
    android:layout_height="0dp" />

<TimePicker
    android:id="@+id/time_picker"
    android:layout_weight="4"
    android:layout_width="match_parent"
    android:layout_height="0dp" />

<Button
    android:id="@+id/date_time_ok"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:text="OK"
    android:layout_height="0dp" />

</LinearLayout>

In your java code use the following code:

final View dialogView = View.inflate(activity, R.layout.date_time_picker_dialog, null);
final AlertDialog alertDialog = new AlertDialog.Builder(activity).create();

dialogView.findViewById(R.id.date_time_ok).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

     DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
     TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.time_picker);

     Calendar calendar = new GregorianCalendar(datePicker.getYear(),
                        datePicker.getMonth(),
                        datePicker.getDayOfMonth(),
                        timePicker.getCurrentHour(),
                        timePicker.getCurrentMinute());

     time = calendar.getTimeInMillis();
     alertDialog.dismiss();
}});
alertDialog.setView(dialogView);
alertDialog.show();
like image 31
Jaydroid Avatar answered Mar 28 '26 17:03

Jaydroid