Android Open Source - CriminalIntent Date Picker Fragment






From Project

Back to project page CriminalIntent.

License

The source code is released under:

MIT License

If you think the Android project CriminalIntent 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.ambergleam.criminalintent.controller;
//from   w w w.j  av  a 2  s.  c  om
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

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.view.WindowManager;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker;

import com.ambergleam.criminalintent.R;

public class DatePickerFragment extends DialogFragment {

  public static final String EXTRA_DATE = "com.ambergleam.criminalintent.date";

  private Date mDate;
  private int year, month, day, hour, minute;

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

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

    return fragment;
  }

  @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);
    this.year = calendar.get(Calendar.YEAR);
    this.month = calendar.get(Calendar.MONTH);
    this.day = calendar.get(Calendar.DAY_OF_MONTH);
    this.hour = calendar.get(Calendar.HOUR);
    this.minute = calendar.get(Calendar.MINUTE);

    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 OnDateChangedListener() {

      @Override
      public void onDateChanged(DatePicker view, int year, int month, int day) {
        // Translate year, month, day, hour, and minute into a Date object using a calendar
        mDate = new GregorianCalendar(year, month, day, hour, minute).getTime();
        // Update argument to preserve the selected value on rotation
        getArguments().putSerializable(EXTRA_DATE, mDate);
      }

    });

    TimePicker timePicker = (TimePicker) v.findViewById(R.id.dialog_date_timePicker);
    timePicker.setCurrentHour(hour);
    timePicker.setCurrentMinute(minute);
    timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {

      @Override
      public void onTimeChanged(TimePicker view, int hour, int minute) {
        // Translate year, month, day, hour, and minute into a Date object using a calendar
        mDate = new GregorianCalendar(year, month, day, hour, minute).getTime();
        // Update argument to preserve the selected value on rotation
        getArguments().putSerializable(EXTRA_DATE, mDate);
      }

    });

    AlertDialog ad = 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();
    ad.show();

    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(ad.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.MATCH_PARENT;
    ad.getWindow().setAttributes(lp);

    return ad;

  }

  private void sendResult(int resultCode) {
    if (getTargetFragment() == null)
      return;

    Intent i = new Intent();
    i.putExtra(EXTRA_DATE, mDate);

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

}




Java Source Code List

com.ambergleam.criminalintent.controller.CrimeFragment.java
com.ambergleam.criminalintent.controller.CrimeListActivity.java
com.ambergleam.criminalintent.controller.CrimeListFragment.java
com.ambergleam.criminalintent.controller.CrimePagerActivity.java
com.ambergleam.criminalintent.controller.DatePickerFragment.java
com.ambergleam.criminalintent.helper.CriminalIntentJSONSerializer.java
com.ambergleam.criminalintent.helper.SingleFragmentActivity.java
com.ambergleam.criminalintent.model.CrimeLab.java
com.ambergleam.criminalintent.model.Crime.java