user11621177
user11621177

Reputation:

How to show spinner type Data Dialog Fragment?

i'm trying to make a spinner type Data Dialog. Something like this: spinner data dialog

And here is my code so far: DatePickerFragment:

public class DatePickerFragment extends DialogFragment {
    DatePickerDialog.OnDateSetListener ondateSet;
    private int year, month, day;

    public DatePickerFragment() {
    }

    public void setCallBack(DatePickerDialog.OnDateSetListener ondate) {
        ondateSet = ondate;
    }

    @SuppressLint("NewApi")
    @Override
    public void setArguments(Bundle args) {
        super.setArguments(args);
        year = args.getInt("year");
        month = args.getInt("month");
        day = args.getInt("day");
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(), ondateSet, year, month, day);
        return datePickerDialog;
    }
}

and part of the main code:

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDatePicker();
            }
        });



...



    private void showDatePicker() {
        DatePickerFragment date = new DatePickerFragment();
        Calendar calender = Calendar.getInstance();
        Bundle args = new Bundle();
        args.putInt("year", calender.get(Calendar.YEAR));
        args.putInt("month", calender.get(Calendar.MONTH));
        args.putInt("day", calender.get(Calendar.DAY_OF_MONTH));
        date.setArguments(args);
        date.setCallBack(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                editText.setText(String.valueOf(dayOfMonth) + "/" + String.valueOf(month + 1)
                        + "/" + String.valueOf(year));
            }
        });
        date.show(getFragmentManager(), "Date Picker");
    }

Can I set datePickerMode to spinner in this situation somehow? For your information, I'm using Fragments.

Thanks in advance!

Upvotes: 0

Views: 759

Answers (2)

user11621177
user11621177

Reputation:

Ok, I found an answer.

I just changed my AppTheme style:

<style name="AppTheme"  parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorControlActivated">@color/colorPrimary</item>
    <item name="android:timePickerDialogTheme">@style/PickerDialogCustom</item>
    <item name="android:datePickerDialogTheme">@style/PickerDialogCustom</item>
    <item name="alertDialogTheme">@style/AlertDialogCustom</item>
</style>

<style name="PickerDialogCustom" parent="AlertDialogCustom">
    <item name="android:textColor">@color/colorPrimary</item>
    <item name="android:textColorPrimary">@color/colorPrimaryDark</item>
    <item name="colorControlNormal">@color/greyLight500</item>
    <item name="android:layout_margin">2dp</item>
    <item name="android:datePickerMode">spinner</item>
</style>

<style name="AlertDialogCustom" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorAccent">@color/colorPrimary</item>
    <item name="android:positiveButtonText">@color/colorPrimary</item>
    <item name="android:negativeButtonText">@color/greyDark200</item>
    <item name="buttonBarNegativeButtonStyle">@style/negativeButton</item>
    <item name="android:datePickerStyle">@style/PickerDialogCustom</item>
</style>

Upvotes: 0

Omkar C.
Omkar C.

Reputation: 751

Yes you're right you can set android:datePickerMode to spinner just as below and you would get this output.

<DatePicker
    android:id="@+id/datePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:datePickerMode="spinner"/>

You can also find more information on how to customize this on this two pages.

  1. Datepicker 1
  2. Datepicker 2

Upvotes: 1

Related Questions