Android Date picker example
How to pick date from the user in Android? Write an android example to pick date from the user and display it. Android provides control for the user to pick a date through a pre-defined dialog box. Picker provides control for selecting each part of the date i.e. month, day, and year. Pickers help us 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 date with the help of date picker and with the simple one click show button user can view the date entered. Date class is required to hold the data set by the picker. Data members inside the Date 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. OUTPUT SCREEN: public class DatePickerDialog extends AlertDialog implements DialogInterface.OnClickListener,DatePicker.OnDateChangedListener
java.lang.Object | |||
? | android.app.Dialog | ||
? | android.app.AlertDialog | ||
? | android.app.DatePickerDialog |
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; import java.util.Calendar; 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 DatePickerFragment(); newFragment.show(getSupportFragmentManager(), "datePicker"); editText=findViewById(R.id.editText); button=findViewById(R.id.show); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { date dv=new date(); String date=new String(); date=""; date=date+dv.getDay()+"/"+dv.getMonth()+"/"+dv.getYear(); editText.setText(date); } }); } }JAVA FILE 2: date.java
package com.example.android.tutorialsandexample; public class date { static int year; static int month; static int day; public static int getYear() { return year; } public static void setYear(int year) { date.year = year; } public static int getMonth() { return month; } public static void setMonth(int month) { date.month = month; } public static int getDay() { return day; } public static void setDay(int day) { date.day = day; } }JAVA FILE 3: DatePickerFragment.java
package com.example.android.tutorialsandexample; import android.app.DatePickerDialog; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.widget.DatePicker; import java.util.Calendar; public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // Use the current date as the default date in the picker final Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); // Create a new instance of DatePickerDialog and return it return new DatePickerDialog(getActivity(), this, year, month, day); } public void onDateSet(DatePicker view, int year, int month, int day) { // Do something with the date chosen by the user date date=new date(); date.setDay(day); date.setMonth(month); date.setYear(year); } }XML FILE:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" <?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="date" android:text="This is blank text"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show date" android:id="@+id/show"/> </LinearLayout>