android datepickerdialog


package com.journaldev.datetimepickerdialog;

import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity implements
        View.OnClickListener {

    Button btnDatePicker, btnTimePicker;
    EditText txtDate, txtTime;
    private int mYear, mMonth, mDay, mHour, mMinute;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnDatePicker=(Button)findViewById(R.id.btn_date);
        btnTimePicker=(Button)findViewById(R.id.btn_time);
        txtDate=(EditText)findViewById(R.id.in_date);
        txtTime=(EditText)findViewById(R.id.in_time);

        btnDatePicker.setOnClickListener(this);
        btnTimePicker.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        if (v == btnDatePicker) {

            // Get Current Date
            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);


            DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                    new DatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker view, int year,
                                              int monthOfYear, int dayOfMonth) {

                            txtDate.setText(dayOfMonth + "-" + (monthOfYear + 1) + "-" + year);

                        }
                    }, mYear, mMonth, mDay);
            datePickerDialog.show();
        }
        if (v == btnTimePicker) {

            // Get Current Time
            final Calendar c = Calendar.getInstance();
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);

            // Launch Time Picker Dialog
            TimePickerDialog timePickerDialog = new TimePickerDialog(this,
                    new TimePickerDialog.OnTimeSetListener() {

                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay,
                                              int minute) {

                            txtTime.setText(hourOfDay + ":" + minute);
                        }
                    }, mHour, mMinute, false);
            timePickerDialog.show();
        }
    }
}

4
6
Zwi 90 points

                                    @NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Calendar calendar = Calendar.getInstance();
    int year    = calendar.get(Calendar.YEAR);
    int month   = calendar.get(Calendar.MONTH);
    int day     = calendar.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog dialog = new DatePickerDialog(getContext(), listener, year, month, day);
    Field mDatePickerField;
    try {
            mDatePickerField = dialog.getClass().getDeclaredField("mDatePicker");
            mDatePickerField.setAccessible(true);
    } catch (Exception e) {
            e.printStackTrace();
    }
    dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
    return dialog;
}

4 (6 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
android datepickerdialog kotlin android datepickerdialog set min date datepicker dialog android android timepickerdialog timepickerdialog android example datepicker in dialog android DatePickerDialog to view android android datepicker dialog example android studio datepickerdialog example android DialogDatePicker android DatePickerDialog onselect datepickerdialog set min date timepickerdialog set min time android update datepickerdialog android android datepickerdialog date range android datepickerdialog from date datetimepicker dialog android date picker dialogs in android datepickerdialog in android studio timepickerdialog android allows what DatePickerDialog android activity Create DatePickerDialog in Android Studio android datepickerdialog example android datepickerdialog spinner set min date in datepicker dialog android set minimum time in timepickerdialog android android datepicker dialog android studio date picker dialog android timepickerdialog example timepickerdialog android android datetime picker dialog android datepickerdialog setbackground androdi date picker dialog datepickerdialog andorid date picker dialog on date set Android DatePickerDialog: Set min and max date for selection datepicker android example how to open date dialog box in android on click of text view xml datepicker android set min date Datepicker Dialog import datepickerdialog set min date android date select from box in android studio android studio select date dialog android open timepicker dialog after datepciker datepickerdialog android example DatePickerDialog android setOnDateSetListener datepicker android how to set limit for date picker android date dialog android monthdialog in android with java android show DatePicker as a dialog DatePickerDialog set max date android code calendar date selection in android date time picker widget android datepickerdialog in android android datepicker set mindate today Geysermc for m how to set minimum date on date picker dialog android date picker example DatePickerDialog set max date today android datepickerdialog tutorial datepicker mindate today android android dialog date xml today date picker in android programmatically how to use on date picker dialog in android android how to use date picker dialog date picker dialog in android how to create date picker for android application how to show date picker dialog in android android date picker dialog example set mindate and maxdate in datepicker android calander picker androdi set min and max date calendar dialog android datepickerdialog in androidx android open date picker dialog how to get date dialog from custom dialog datepicker dialog datepicker dialog in android how to show min and max date in calender in android how to show datepickerdialog on button click android datepicker example date picker dialog max date how to set minimum 7 days range in datepicker android android calendar set max date max date in android studio adult datedialog in android studio date dialog android example show date picker dialog android DatePickerDialog Dialog classes in Android? * date picker dialog android making datepicker dialog android android studio button to select date in android date listener should open calender as set date show selected date on dialog android datepickerdialog android DataPickerDialog android datepickerdialog dialogue date button android studio how to let user pick date and time android DatePickerDialog date xml adnroid android date picker dialog kotlin android game tutorial geysermc for android android stor soundpool android studio android preloader substratum ios emoji android 9 appcompat v7 library for android studio android contextmenu rxjava android tutorial android imagebutton vs button android setVisibility programatically date pickerdiaglog android open calendar dialog in android
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source