Gal Nezah
Gal Nezah

Reputation: 33

Fullscreen DialogFragment with DatePicker in Android

I'm developing an app in android that shows a datepicker. The problem is when I tried to get a fullscreen datepicker dialogfragment. I get the screen that you can see in the image below. Is there a way to get a bigger datepicker? Thanks in advance!

https://i.sstatic.net/gSj4F.png

Here is my code:

DatePickerFragment.java

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState){
    Dialog dialog = getDialog();
    if (dialog != null)
    {
        int width = ViewGroup.LayoutParams.MATCH_PARENT;
        int height = ViewGroup.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setLayout(width, height);
    }
    //Use the current date as the default date in the date picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);
    return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onStart()
{
    super.onStart();
    Dialog dialog = getDialog();
    if (dialog != null)
    {
        //Fullscreen dialog
        int width = ViewGroup.LayoutParams.MATCH_PARENT;
        int height = ViewGroup.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setLayout(width, height);
    }
}
public void onDateSet(DatePicker view, int year, int month, int day) {
    //Do something with the date chosen by the user
    TextView tvFecha = (TextView) getActivity().findViewById(R.id.tvFecha);
    month = month+1;
    String formattedMonth = "" + month;
    String formattedDay = "" + day;

    if(month < 10){

        formattedMonth = "0" + month;
    }
    if(day < 10){

        formattedDay = "0" + day;
    }
    String stringOfDate = formattedDay + "-" + formattedMonth + "-" + year;
    tvFecha.setText(stringOfDate);
}
}

Upvotes: 1

Views: 3980

Answers (2)

Thriveni
Thriveni

Reputation: 771

You can use the style in the dialog

<style name="DialogTheme" parent="Theme.AppCompat.Light">
        <item name="colorAccent">@color/primary</item>
        <item name="android:windowIsFloating">true</item>
</style>

Upvotes: 2

earthw0rmjim
earthw0rmjim

Reputation: 19417

Add the following style to your styles.xml:

<style name="FullScreenDialog" parent="Theme.AppCompat.Light">
    <item name="android:windowFrame">@null</item>
</style>

And onCreateDialog() should look something like this:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // calling getDialog() here does not make much sense

    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // pass the style defined above to the constructor of DatePickerDialog
    return new DatePickerDialog(getActivity(),
        R.style.FullScreenDialog,
        this,
        year, month, day);
}

The result:

enter image description here

Upvotes: 0

Related Questions