Android DatePicker dialog Box to Get ToDate and FromDate
Android DatePicker dialog Box to Get ToDate and FromDate
Table of Contents
Hey in this article I am sharing how to use a date picker in android and select a date .is just simply way to implement the date picker in your android app. in this I am working on a show date picker to click a textView and show selected date on textView.
Step 1: To Create Android Project DatePicker Example ;
Step 2; Create A New Project è ClickOn ==> File ==> NEW ==> New Project
Step3: Create an Activity DatePicker
Step 4: Open Your Activity XML file and design the screen using layout a
Activity_datepicker.xml
<?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:orientation="vertical"
android:gravity="center"
android:padding="20dp"
android:layout_height="match_parent"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/ToDate"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_weight="1"
android:gravity="center"
android:hint="Select date "
android:inputType="text"
android:background="@drawable/editbox"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:textSize="20dp"
android:text="To"/>
<EditText
android:id="@+id/FromDate"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:hint="Select date "
android:inputType="text"
android:background="@drawable/editbox"/>
</LinearLayout>
<Button
android:id="@+id/SendDateBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="@drawable/button"
android:textColor="@color/text"
android:textAppearance="?android:textAppearanceLarge"
android:text="Submit"/>
</LinearLayout>
Step: 5 Open Your Datepicker.java class
public class Datepicker extends AppCompatActivity{
EditText ToDate,FromDate;
private SimpleDateFormat dateFormatter;
Button SendButton;
String FilterToDate,FilterFromDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datepicker);
ToDate=(EditText).findViewById(R.id.ToDate);
FromDate=(EditText) findViewById(R.id.FromDate);
ToDate.setInputType(InputType.TYPE_NULL);
FromDate.setInputType(InputType.TYPE_NULL);
ToDate.requestFocus();
SendButton=(Button) findViewById(R.id.SendDateBtn);
dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
ToDate.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance(Locale.getDefault());
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
//todo
Calendar newDate = Calendar.getInstance();
newDate.set(year, month, dayOfMonth);
ToDate.setText(dateFormatter.format(newDate.getTime()));
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}
});
FromDate.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance(Locale.getDefault());
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
//todo
Calendar newDate = Calendar.getInstance();
newDate.set(year, month, dayOfMonth);
FromDate.setText(dateFormatter.format(newDate.getTime()));
}
},calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}
});
getDialog().setTitle("Filter Leads");
SendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FilterToDate = ToDate.getText().toString().trim();
FilterFromDate = FromDate.getText().toString().trim();
}
});
}