Reputation: 536
I am triggering a DatePickerDialog, It's working and showing fine till api 22 (Android 5.1), I am setting mix and max dates on it (min = current date, max = 1 month starting from current date), but It's just showing current date in Api 23, I attached code and images.
///////////////////////////////datepickerdialog///////////////////////////////
public static class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{
@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
/*
Create a DatePickerDialog using Theme.
DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener listener,
int year, int monthOfYear, int dayOfMonth)
*/
// DatePickerDialog THEME_DEVICE_DEFAULT_LIGHT
DatePickerDialog dpd = new DatePickerDialog(getActivity(),this,year,month,day);
if (Build.VERSION.SDK_INT < 22){
dpd.getDatePicker().setCalendarViewShown(true);
dpd.getDatePicker().setSpinnersShown(false);
dpd.getDatePicker().getCalendarView().setShowWeekNumber(false);
}
calendar.setTimeInMillis(calendar.getTimeInMillis());
long mindate = calendar.getTime().getTime();
calendar.add(Calendar.MONTH, 1);
long maxdate = calendar.getTime().getTime();
dpd.getDatePicker().setMinDate(mindate);
dpd.getDatePicker().setMaxDate(maxdate);
dpd.getDatePicker().setMinDate(mindate);
if(Build.VERSION.SDK_INT < 23){
dpd.setTitle("");
}
// Return the DatePickerDialog
return dpd;
}
@SuppressLint("SimpleDateFormat")
public void onDateSet(DatePicker view, int year, int month, int day){
// Do something with the chosen date
month++;
evento_fecha = year+"-"+month+"-"+day;
month--;
datetime.set(Calendar.YEAR, year);
datetime.set(Calendar.MONTH, month);
datetime.set(Calendar.DAY_OF_MONTH, day);
SimpleDateFormat mSDF = new SimpleDateFormat("dd/MM/yyyy");
fecha = mSDF.format(datetime.getTime());
//fecha_seleccionada.setText(dia+"/"+mes+"/"+year);
fecha_seleccionada.setText(fecha);
}
}
Upvotes: 5
Views: 6048
Reputation: 2241
DatePicker / DatePickerDialog not appearing properly on Android Marshmallow / API 23 is a theme issue. I suggest, instead of using default theme or writing custom styles, instantiate the DatePickerDialog using 'theme' option:
DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth)
API 23 onwards, Material theme should be used for the dialog. Use android.R.style.Theme_Material_Light_Dialog_Alert
for the theme parameter.
For Xamarin Android - use Android.Resource.Style.ThemeMaterialLightDialogAlert
.
Upvotes: 3
Reputation: 536
I was using this style in my theme:
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="colorPrimary">@color/green_sheet</item>
<item name="android:textColorPrimary">@color/textColorPrimary</item>
<item name="android:textAllCaps">false</item>
</style>
The textColorPrimary property was showing all days numbers in white, I used this property in order to set the action bar text to white. I was wrong because I had to use Theme.AppCompat.Light.DarkActionBar for this purpose, so I changed my theme style to this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="colorPrimary">@color/green_sheet</item>
<item name="android:textAllCaps">false</item>
</style>
and the result was as expected.
Upvotes: 1
Reputation: 20930
In your DatePicker
you have set MinDate
to currentTimeMillis()
and in your MaxDate
you have add 1 MONTH
to your Calendar
.
Just simple use this code.
public class MainActivity extends Activity {
int mYear, mMonth, mDay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
final TextView textView = (TextView)findViewById(R.id.textview_totalStone);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialog dpd = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
c.set(year, month, day);
String date = new SimpleDateFormat("MM/dd/yyyy").format(c.getTime());
textView.setText(date);
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
}
}, mYear, mMonth, mDay);
dpd.getDatePicker().setMinDate(System.currentTimeMillis());
Calendar d = Calendar.getInstance();
d.add(Calendar.MONTH,1);
dpd.getDatePicker().setMaxDate(d.getTimeInMillis());
dpd.show();
}
});
}
In my DatePicker I have Date June 25
and it select and as your requirement you have MaxDate
1 Month it's show in ScreenShot.
ScreenShot : 1
ScreenShot : 2
Update :
Replace your this code
calendar.setTimeInMillis(calendar.getTimeInMillis());
long mindate = calendar.getTime().getTime();
dpd.getDatePicker().setMinDate(mindate);
with this
dpd.getDatePicker().setMinDate(System.currentTimeMillis());
Upvotes: 0