4

I am trying to use a DialogFragment to host a date picker as recommended by Google here: http://developer.android.com/guide/topics/ui/controls/pickers.html

Using the sample code at the page above works OK, but I want to be able to set a specific theme for the date picker dialog.

If I modify the OnCreateDialog method in the dialog fragment from their sample, so that it returns a date picker with specific theme like this

...
// Create a new instance of DatePickerDialog and return it
DatePickerDialog dlg = new DatePickerDialog(getActivity(),
   Android.R.style.Theme_DeviceDefault_Light_Dialog, this, year, month, day); 
return dlg;
...

(i.e. just using the other constructor for DatePickerDialog, where you can provide a theme) I get an ugly non-transparent border around my date picker when it is displayed:

(screenshot here: https://i.stack.imgur.com/WO1YD.png)

What can I do to get rid of the border?

vanangelov
  • 164
  • 2
  • 7
  • It turned out the problem is not with fragments, I get the same broken layout even without fragments, when I create the dialog using the `DatePickerDialog (Context context, **int theme**, DatePickerDialog.OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth)` constructor – vanangelov Jul 05 '12 at 17:31

2 Answers2

3

This maybe a bit old, but at least I'll answer here, just in case other people find this question.
I believe you can find the answer here, in this other stack overflow question (probably a duplicate, but don't know how to mark it as one).

The answer there was setting the window background like this:
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

Hope it helps.

Community
  • 1
  • 1
Juan
  • 936
  • 8
  • 10
2

I know it is already a year ago but maybe someone else need a solution for that problem. Yesterday I had the same problem and I solved it with a small workaround. I made my own DatePickerDialog instead of the provided one from android.

Here is the code:

dialog_date_picker.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

<DatePicker
        android:id="@+id/dlgDatePicker"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:spinnersShown="true"
        android:calendarViewShown="false"/>

<View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"/>

<Button
        android:id="@+id/dlgDatePickerBtnDone"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/dlg_date_picker_button"
        />

</LinearLayout>

DialogDatePicker.java

public class DialogDatePicker extends DialogFragment {

private DatePicker dp;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog_date_picker, container);
    dp = (DatePicker) view.findViewById(R.id.dlgDatePicker);
    view.findViewById(R.id.dlgDatePickerBtnDone).setOnClickListener(setDate);
    getDialog().setTitle(R.string.dlg_date_picker_title);
    return view;
}

private View.OnClickListener setDate = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        int dayOfMonth = dp.getDayOfMonth();
        int monthOfYear = dp.getMonth()+1; //0 = jan, 1 = feb, ...
        int year = dp.getYear();

        String date = String.format(Locale.GERMANY,"%s.%s.%s", dayOfMonth, monthOfYear, year);
        ((TextView) getActivity().findViewById(R.id.route_setting_date))
                .setText(date);

        dismiss();
    }
};

}

and finally the call with the new theme

DialogDatePicker ddp = new DialogDatePicker();
ddp.setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Holo_Dialog);
ddp.show(getFragmentManager(),"DialogDatePicker");
rack
  • 21
  • 3