Last active
November 16, 2015 18:30
-
-
Save Sirelon/34eebd0babd9326ce950 to your computer and use it in GitHub Desktop.
Custom view EditTextDatePicker, which show datepicker dialog, when it clicked and set correctly date for this EditText. Add copability to shows current date and string prompt beside date. Return valid date on method getDate().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<resources> | |
<declare-styleable name="EditTextDatePicker"> | |
<attr name="dateFormat" format="string" /> | |
<attr name="dialogDate" format="string" /> | |
<attr name="dialogDateInsteadText" format="boolean"/> | |
<attr name="datePrompt" format="string" /> | |
<attr name="useCurrentIfEmpty" format="boolean" /> | |
</declare-styleable> | |
</resources> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sirelon; | |
import android.app.DatePickerDialog; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.text.TextUtils; | |
import android.util.AttributeSet; | |
import android.widget.DatePicker; | |
import android.widget.EditText; | |
import com.sirelon.R; | |
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.GregorianCalendar; | |
public class EditTextDatePicker extends EditText { | |
private static final String DEFAULT_FORMAT = "dd/MM/yyyy"; | |
boolean dialogDateInsteadText = false; | |
private SimpleDateFormat mDateFormat; | |
private Calendar mDialogCalendar = Calendar.getInstance(); | |
private String mDatePrompt; | |
public EditTextDatePicker(Context context) { | |
this(context, null); | |
} | |
public EditTextDatePicker(Context context, AttributeSet attrs) { | |
this(context, attrs, R.style.Widget_AppCompat_EditText); | |
} | |
public EditTextDatePicker(Context context, AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
init(attrs, defStyleAttr); | |
} | |
private void init(AttributeSet attrs, int defStyle) { | |
setFocusable(false); | |
setFocusableInTouchMode(false); | |
final TypedArray a = getContext().obtainStyledAttributes( | |
attrs, R.styleable.EditTextDatePicker, defStyle, 0); | |
String dateFormat = a.getString( | |
R.styleable.EditTextDatePicker_dateFormat); | |
if (TextUtils.isEmpty(dateFormat)) { | |
dateFormat = DEFAULT_FORMAT; | |
} | |
String dialogDate = a.getString( | |
R.styleable.EditTextDatePicker_dialogDate); | |
dialogDateInsteadText = a.getBoolean(R.styleable.EditTextDatePicker_dialogDateInsteadText, false); | |
mDatePrompt = a.getString( | |
R.styleable.EditTextDatePicker_datePrompt | |
); | |
boolean useCurrentIfEmpty = a.getBoolean( | |
R.styleable.EditTextDatePicker_useCurrentIfEmpty, | |
false | |
); | |
a.recycle(); | |
mDateFormat = new SimpleDateFormat(dateFormat); | |
if (TextUtils.isEmpty(dialogDate)) { | |
if (useCurrentIfEmpty) | |
dialogDate = getCurrentDate(mDateFormat); | |
else { | |
if (mDatePrompt != null) setText(mDatePrompt); | |
return; | |
} | |
} | |
tryParseText(dialogDate); | |
} | |
private String getCurrentDate(SimpleDateFormat mDateFormat) { | |
Date date = new Date(System.currentTimeMillis()); | |
return mDateFormat.format(date); | |
} | |
private void tryParseText(String dialogDate) { | |
try { | |
Date date = mDateFormat.parse(dialogDate); | |
mDialogCalendar.setTime(date); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
throw new RuntimeException("You date format and dialog date cannot be parsed!", e); | |
} | |
if (mDatePrompt != null) dialogDate = mDatePrompt + dialogDate; | |
if (dialogDateInsteadText) | |
setText(dialogDate); | |
} | |
@Override | |
public boolean performClick() { | |
boolean b = super.performClick(); | |
if (!isClickable()) { | |
return b; | |
} | |
showDatePicker(mDialogCalendar, new DatePickerDialog.OnDateSetListener() { | |
@Override | |
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { | |
String formatedDate = getFormattedDate(year, monthOfYear, dayOfMonth); | |
EditTextDatePicker.this.setText(formatedDate); | |
} | |
}); | |
return b; | |
} | |
public String getDate() { | |
String editable = getText().toString(); | |
if (mDatePrompt != null) { | |
editable = editable.substring(mDatePrompt.length(), editable.length() - 1); | |
} | |
return editable; | |
} | |
public void setDateFormat(SimpleDateFormat dateFormat) { | |
this.mDateFormat = dateFormat; | |
} | |
public void setDialogCalendar(Calendar dialogCalendar) { | |
this.mDialogCalendar = dialogCalendar; | |
} | |
public void setDialogDate(String dialogDate) { | |
tryParseText(dialogDate); | |
} | |
private String getFormattedDate(int year, int monthOfYear, int dayOfMonth) { | |
Calendar calendar = new GregorianCalendar(year, monthOfYear, dayOfMonth); | |
return (mDatePrompt != null ? mDatePrompt : "") + mDateFormat.format(calendar.getTime()); | |
} | |
private void showDatePicker(Calendar calendar, DatePickerDialog.OnDateSetListener onDateSetListener) { | |
DatePickerDialog datePickerDialog = new DatePickerDialog( | |
getContext(), | |
onDateSetListener, | |
calendar.get(Calendar.YEAR), | |
calendar.get(Calendar.MONTH), | |
calendar.get(Calendar.DAY_OF_MONTH)); | |
datePickerDialog.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO
Add setLimitDate and setMaxDate