Android Open Source - notes Date Picker Fragment






From Project

Back to project page notes.

License

The source code is released under:

Apache License

If you think the Android project notes listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.donnemartin.android.notes.notes;
//w w  w.ja  va  2 s.c  o  m
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.widget.DatePicker;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DatePickerFragment extends DialogFragment {

    public static final String EXTRA_DATE =
        "com.donnemartin.android.notes.date";

    private Date mDate;

    public static DatePickerFragment newInstance(Date date) {
        Bundle args = new Bundle();
        args.putSerializable(EXTRA_DATE, date);

        DatePickerFragment fragment = new DatePickerFragment();
        fragment.setArguments(args);

        return fragment;
    }

    private void sendResult(int resultCode) {
        if (getTargetFragment() != null) {
            Intent intent = new Intent();
            intent.putExtra(EXTRA_DATE, mDate);

            getTargetFragment()
                .onActivityResult(getTargetRequestCode(), resultCode, intent);
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        mDate = (Date)getArguments().getSerializable(EXTRA_DATE);

        // Create a Calendar to get the year, month, and day
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(mDate);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        View v = getActivity().getLayoutInflater()
            .inflate(R.layout.dialog_date, null);

        DatePicker datePicker =
            (DatePicker)v.findViewById(R.id.dialog_date_datePicker);
        datePicker
            .init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view,
                                      int year,
                                      int month,
                                      int day) {
                // Translate year, month, day into a
                // Date object using a calendar
                mDate = new GregorianCalendar(year, month, day).getTime();

                // Update argument to preserve selected value on rotation
                // Can also do this in onSaveInstanceState(...)
                // Note: There is a bug with DatePickerFragment
                // and retained instances
                getArguments().putSerializable(EXTRA_DATE, mDate);
            }
        });

        return new AlertDialog.Builder(getActivity())
            .setView(v)
            .setTitle(R.string.date_picker_title)
            .setPositiveButton(
                android.R.string.ok,
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        sendResult(Activity.RESULT_OK);
                    }
                })
            .create();
    }
}




Java Source Code List

com.donnemartin.android.notes.notes.AudioPlayer.java
com.donnemartin.android.notes.notes.AudioRecorder.java
com.donnemartin.android.notes.notes.DatePickerFragment.java
com.donnemartin.android.notes.notes.ImageFragment.java
com.donnemartin.android.notes.notes.NoteCameraActivity.java
com.donnemartin.android.notes.notes.NoteCameraFragment.java
com.donnemartin.android.notes.notes.NoteFragment.java
com.donnemartin.android.notes.notes.NoteIntentJSONSerializer.java
com.donnemartin.android.notes.notes.NoteListActivity.java
com.donnemartin.android.notes.notes.NoteListFragment.java
com.donnemartin.android.notes.notes.NotePagerActivity.java
com.donnemartin.android.notes.notes.Note.java
com.donnemartin.android.notes.notes.Notebook.java
com.donnemartin.android.notes.notes.Photo.java
com.donnemartin.android.notes.notes.PictureUtils.java
com.donnemartin.android.notes.notes.SingleFragmentActivity.java