Android has been created a specific dialog to select a date from but, sometimes we can’t use that or we have to implement a view which is more simple or can become more custom.
In one of my project I decided to design and develop my own Date Picker cause I didn’t find any sutible date picker library. Here is my date picker that consist of a Java file which named MyDatePicker.java and a XML file which named view_date_picker.xml that must be located in layout directory. I would be happy if anyone help me to improve this class.
MyDatePicker.java
public class MyDatePicker extends FrameLayout { private TextView tvYear; private TextView tvMonth; private TextView tvDay; private Context context; private ArrayList<String> year = new ArrayList<String>(); private ArrayList<String> month = new ArrayList<String>(); private ArrayList<String> day = new ArrayList<String>(); private int yearFrom = 2000; private int yearUntil = 2019; public MyDatePicker(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); this.context = context; initView(); } public void initYear(int yearFrom, int yearUntil) { this.yearFrom = yearFrom; this.yearUntil = yearUntil; fillYear(); } public void initView() { final View view = inflate(getContext(), R.layout.view_date_picker, null); tvYear = view.findViewById(R.id.tvYear); tvMonth = view.findViewById(R.id.tvMonth); tvDay = view.findViewById(R.id.tvDay); fillDay(); fillMonth(); fillYear(); tvMonth.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog(view, tvMonth, month); } }); tvDay.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog(view, tvDay, day); } }); tvYear.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dialog(view, tvYear, year); } }); addView(view); } public void setYear(int year) { tvYear.setText(year + ""); } public void setMonth(String month) { tvMonth.setText(month); } public void setMonth(int month) { tvMonth.setText(convertNumToMonth(month)); } public void setDay(int day) { tvDay.setText(day + ""); } public ArrayList<String> getMonthList() { return month; } private void dialog(final View view, final TextView tv, final ArrayList<String> array) { //convert arraylist to string array: String[] convertedArray = new String[array.size()]; convertedArray = array.toArray(convertedArray); AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext()); builder.setItems(convertedArray, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { tv.setText(array.get(which)); } }); AlertDialog dialog = builder.create(); dialog.show(); } public int getDay() { if (tvDay.getText().toString().equals("Day")) return 0; else return Integer.parseInt(tvDay.getText().toString()); } public int getMonthInNum() { if (tvMonth.getText().toString().equals("Month")) return 0; else return convertMonthToNum(tvMonth.getText().toString()); } public String getMonthInString() { if (tvMonth.getText().toString().equals("Month")) return null; else return tvMonth.getText().toString(); } public int getYear() { if (tvYear.getText().toString().equals("Year")) return 0; else return Integer.valueOf(tvYear.getText().toString()); } public boolean isDatePickerValid() { if (tvYear.getText().toString().equals("Year") || tvMonth.getText().toString().equals("Month") || tvDay.getText().toString().equals("Day") || tvYear.getText().toString().length() < 4 || Integer.parseInt(tvYear.getText().toString()) < yearFrom || Integer.parseInt(tvYear.getText().toString()) > yearUntil) return false; else return true; } private void fillDay() { day.clear(); day.add("Day"); for (int i = 1; i < 32; i++) { day.add(i++ + ""); } } private void fillMonth() { month.clear(); month.add("Month"); month.add("January"); month.add("February"); month.add("March"); month.add("April"); month.add("May"); month.add("June"); month.add("July"); month.add("August"); month.add("September"); month.add("October"); month.add("November"); month.add("December"); } private void fillYear() { year.clear(); year.add("Year"); for (int i = yearFrom; i < yearUntil; i++) { year.add(i++ + ""); } } private static int convertMonthToNum(String s) { switch (s) { case "January": case "january": case "Jan": case "jan": return 1; case "February": case "february": case "Feb": case "feb": return 2; case "March": case "march": case "Mar": case "mar": return 3; case "April": case "april": case "Apr": case "apr": return 4; case "May": case "may": return 5; case "June": case "june": case "Jun": case "jun": return 6; case "July": case "july": case "Jul": case "jul": return 7; case "August": case "august": case "Aug": case "aug": return 8; case "September": case "september": case "Sep": case "sep": return 9; case "October": case "october": case "Oct": case "oct": return 10; case "November": case "november": case "Nov": case "nov": return 11; case "December": case "december": case "Dec": case "dec": return 12; } return 0; } private static String convertNumToMonth(int num) { switch (num) { case 1: return "January"; case 2: return "February"; case 3: return "March"; case 4: return "April"; case 5: return "May"; case 6: return "June"; case 7: return "July"; case 8: return "August"; case 9: return "September"; case 10: return "October"; case 11: return "November"; case 12: return "December"; } return null; } }
view_date_picker.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:layout_margin="5dp" android:background="@drawable/btn_white" android:elevation="2dp" android:gravity="center_vertical" android:orientation="horizontal" android:padding="2dp"> <TextView android:id="@+id/tvDay" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginRight="5dp" android:layout_weight="1" android:gravity="center" android:text="Day" android:textColor="#424242" android:textSize="12dp" /> <ImageView android:layout_width="7dp" android:layout_height="7dp" android:layout_marginBottom="5dp" android:layout_marginLeft="5dp" android:layout_marginRight="15dp" android:layout_marginTop="5dp" android:background="@drawable/ic_dropdown" /> <LinearLayout android:layout_width="1dp" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:background="#e0e0e0"></LinearLayout> <TextView android:id="@+id/tvMonth" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginLeft="4dp" android:layout_marginRight="5dp" android:layout_weight="1" android:gravity="center" android:text="Month" android:textColor="#424242" android:textSize="12dp" /> <ImageView android:layout_width="7dp" android:layout_height="7dp" android:layout_marginLeft="2dp" android:background="@drawable/ic_dropdown" /> <LinearLayout android:layout_width="1dp" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:background="#e0e0e0"></LinearLayout> <TextView android:id="@+id/tvYear" android:layout_width="0dp" android:layout_height="match_parent" android:layout_marginRight="5dp" android:layout_weight="1" android:gravity="center" android:text="Year" android:textColor="#424242" android:textSize="12dp" /> <ImageView android:layout_width="7dp" android:layout_height="7dp" android:layout_marginBottom="5dp" android:layout_marginLeft="5dp" android:layout_marginRight="15dp" android:layout_marginTop="5dp" android:background="@drawable/ic_dropdown" /> </LinearLayout> </LinearLayout>
Usage
MyDatePicker myDatePicker; myDatePicker = findViewById(R.id.datePicker); myDatePicker.initYear(1995,2019); //Optional (default is 2000 to 2019) myDatePicker.initView();//Important part! myDatePicker.setDay(23); myDatePicker.setMonth(2); myDatePicker.setYear(2018); //functions : myDatePicker.getDay(); myDatePicker.getMonthInNum(); myDatePicker.getMonthInString(); myDatePicker.getYear(); myDatePicker.invalidate();
1 Comment. Leave new
[…] 16, 2019vahidnghUncategorizedNo Comments In my previous post, I wrote about Android Date Picker class which I developed on my own. Now I’m gonna introduce another class which I developed to […]