Question

here's my fragment's code on which i'm trying to call datepicker using DialogFragment

public class ExpensesDaily extends Fragment{

    public ExpensesDaily(){}
    Button button;
    Calendar now;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.expenses_daily, container, false);

        Button button = (Button) rootView.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogFragment picker = new DatePickerFragment();
                picker.show(getFragmentManager(), "datePicker");
            }
        });
        return rootView;
    }

}

and here's my DatePickerFragment code

public class DatePickerFragment extends DialogFragment  {

    DatePickerDialog.OnDateSetListener onDateSet;
    TextView tvDate;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the 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);

// Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), onDateSet, year, month, day);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.expenses_daily, container, false);
        tvDate = (TextView) rootView.findViewById(R.id.textViewDate);

        return rootView;


    }
    public void setCallBack(DatePickerDialog.OnDateSetListener onDate) {
        onDateSet = onDate;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month, day);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = sdf.format(c.getTime());
        tvDate.setText("date: " + formattedDate);
    }    

}

but everytime I click on the button to display the datepicker, the app would crash. any pointer to what is still incomplete?

update: I'm trying to implement as this article here http://www.vogella.com/tutorials/AndroidFragments/article.html, but couldn't find a way to do it that suits datepicker

thanks

Was it helpful?

Solution

ok so here's how I did it. I paste my whole code here to help anyone having the same problem with me. I refer to this article here http://www.solardriftsolutions.co.uk/android-fragment-and-dialogfragment/ to fix the requestFeature() error and article here http://androidtrainningcenter.blogspot.com/2012/10/creating-datepicker-using.html to get the value set by the date picker

code for DatePickerFragment.java which extends DialogFragment

public class DatePickerFragment extends DialogFragment  {

    DatePickerDialog.OnDateSetListener onDateSet;
    private boolean isModal = false;

    public static DatePickerFragment newInstance()
    {
        DatePickerFragment frag = new DatePickerFragment();
        frag.isModal = true; // WHEN FRAGMENT IS CALLED AS A DIALOG SET FLAG
        return frag;
    }

    public DatePickerFragment(){}

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        // Use the current date as the default date in the 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);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), onDateSet, year, month, day);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(isModal) // AVOID REQUEST FEATURE CRASH
        {
            return super.onCreateView(inflater, container, savedInstanceState);
        }
        else {
            View rootView = inflater.inflate(R.layout.expenses_daily, container, false);
            return rootView;
        }
    }

    public void setCallBack(DatePickerDialog.OnDateSetListener onDate) {
        onDateSet = onDate;
    }

}

and here's the code for ExpensesDaily.java, which is a fragment which calls the datepickerfragment

public class ExpensesDaily extends Fragment{

    public ExpensesDaily(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View rootView = inflater.inflate(R.layout.expenses_daily, container, false);

        Button button = (Button) rootView.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DatePickerFragment dpf = new DatePickerFragment().newInstance();
                dpf.setCallBack(onDate);
                dpf.show(getFragmentManager().beginTransaction(), "DatePickerFragment");
                //DialogFragment picker = new DatePickerFragment();
                //picker.show(getFragmentManager(), "datePicker");
            }
        });
        return rootView;
    }

    DatePickerDialog.OnDateSetListener onDate = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {

            TextView tvDate = (TextView) getActivity().findViewById(R.id.tvDate);
            tvDate.setText(String.valueOf(year) + "-" + String.valueOf(monthOfYear)
                    + "-" + String.valueOf(dayOfMonth));
        }
    };

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top