Skip to content

Instantly share code, notes, and snippets.

@alexanza
Last active March 10, 2021 08:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanza/c680e95fbe8bf3f45608 to your computer and use it in GitHub Desktop.
Save alexanza/c680e95fbe8bf3f45608 to your computer and use it in GitHub Desktop.
Expiration date picker for credit card (tested on api 19 and up)
public class ExpirationDatePickerDialog extends DatePickerDialog implements DatePicker.OnDateChangedListener {
public ExpirationDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
super(
context,
Build.VERSION.SDK_INT >= 21 ? R.style.MyDialogTheme : 0,
callBack,
year,
monthOfYear,
dayOfMonth
);
init(context);
}
private void init(Context context) {
setTitle("");
getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
getDatePicker().setCalendarViewShown(false);
int day = context.getResources().getIdentifier("android:id/day", null, null);
if(day != 0){
View dayPicker = getDatePicker().findViewById(day);
if(dayPicker != null){
dayPicker.setVisibility(View.GONE);
}
}
}
public void onDateChanged(DatePicker view, int year, int month, int day) {
}
}
<resources>
<style name="MyDialogTheme" parent="android:Theme.Material.Light.Dialog">
<item name="android:datePickerStyle">@style/MyDatePicker</item>
</style>
<style name="MyDatePicker" parent="android:Widget.Material.Light.DatePicker">
<item name="android:datePickerMode">spinner</item>
</style>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment