Thursday, December 5, 2019

Date Picker in Android

Android Date Picker allows you to select the date consisting of day, month and year in your custom user interface. For this functionality, android provides DatePicker and DatePickerDialog components.
Here is an XML file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <EditText
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Here is a whole activity with a date picker.
 public class SampleActivity extends AppCompatActivity {

    private EditText editText;


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

        getSupportActionBar().setTitle("");

        editText = findViewById(R.id.text);

        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                datePicker();
            }
        });


    }

    private void datePicker() {

        Calendar calendar = Calendar.getInstance();
        final SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);
        final SimpleDateFormat sendDate = new SimpleDateFormat("yyyy-MM-dd", Locale.US);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog
                .OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar selectedDate = Calendar.getInstance();
                selectedDate.set(year, monthOfYear, dayOfMonth);

                editText.setText(dateFormatter.format(selectedDate.getTime()).toString());
                editText.setTag(sendDate.format(selectedDate.getTime()).toString());
                editText.setError(null);
                editText.setCompoundDrawables(null, null, getResources().getDrawable(android.R.drawable.ic_menu_my_calendar), null);

            }

        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.getDatePicker().setMaxDate(Calendar.getInstance().getTimeInMillis());

        if (editText.getTag() != null)
            if (!editText.getTag().toString().isEmpty()) {
                String[] values = editText.getTag().toString().split("-");
                datePickerDialog.getDatePicker().updateDate(Integer.parseInt(values[0]), Integer.parseInt(values[1]) - 1, Integer.parseInt(values[2]));
            }
        datePickerDialog.show();
    }
}
Validation for editText when a button is clicked(Use this code if a date is compulsory in a form).
    public boolean isCheck() {
    boolean flag = true;
    if (editText.getText().toString().isEmpty()) {
        flag = false;
        editText.setError("Please Select Record Date.");
    }
    return flag;
    }
Output Date Picker
EditText
That is all. If any help related to this post please comment.
Thank you, guys.
Enjoy coding.

No comments:

Post a Comment