1

I want to create an activity in which I want to have two button and multiple label. a user can click on buttons to select dates from and to. while those selected dates can be shown in label so that user might know which dates already picked by user.

Can anybody guide me how can I do this.

  1. using multiple date-picker functionality with only two date pickers on two buttons.
  2. how can I save those dates. so whenever user leave this activity and visit again he might know.
Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
Aiman Batul
  • 144
  • 11
  • 2
    Have you tried anything? There are many tutorials on internet e.g http://androidexample.com/In_this_example_creating_a_date_picker_to_pick_day__month_year_of_date/index.php?view=article_discription&aid=89&aaid=113 – Shahzeb May 25 '15 at 04:32
  • have u tried anything?? – Jignesh Jain May 25 '15 at 04:36
  • I have tried this http://stackoverflow.com/questions/3734981/multiple-datepickers-in-same-activity – Aiman Batul May 25 '15 at 04:48
  • But I dont get it, that when I pas listener on button click I can only set one textview as a label for one date. How can I click on a button that at first it should set one textview. Later another and so on. – Aiman Batul May 25 '15 at 04:49
  • apart from saving the date, I want to achieve this functionality first. – Aiman Batul May 25 '15 at 04:49

2 Answers2

1

I have created DatePickerFragment helper class, With this you can select any number of date with just single DatePickerFragment class.

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    /**
     * The callback interface used to indicate the user is done filling in the date (they clicked on the 'Set' button).
     */
    public interface OnDateSetListener {

        /**
         * @param dialog The view associated with this listener.
         * @param year The year that was set.
         * @param monthOfYear The monthOfYear that was set.
         * @param dayOfMonth The dayOfMonth that was set.
         */
        void onDateSet(DatePicker dialog, int year, int monthOfYear, int dayOfMonth, int reqCode);
    }

    private OnDateSetListener mListener;
    private int reqCode;
    public void setOnDateSetListener(OnDateSetListener mListener, int reqCode){
        this.mListener  = mListener;
        this.reqCode = reqCode;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        int year;
        int month;
        int day;
        final Calendar c = Calendar.getInstance();
        Bundle bundle = this.getArguments();
        if(bundle!=null) {
            // Use the given date as the default date in the picker
            year = bundle.getInt("YEAR");
            month = bundle.getInt("MONTH");
            day = bundle.getInt("DAY");
        }else{
            // Use the current date as the default date in the picker
            year = c.get(Calendar.YEAR);
            month = c.get(Calendar.MONTH);
            day = c.get(Calendar.DAY_OF_MONTH);
        }

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

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        if(mListener!=null)
            mListener.onDateSet(view, year, monthOfYear, dayOfMonth, reqCode);
        this.dismiss();
    }
}

Use it in Activity or Fragment

public class MainActivity extends FragmentActivity implements DatePickerFragment.OnDateSetListener {
    private static final int FROM_DATE_TAG = 2404;
    private static final int TO_DATE_TAG = 2405;

    @Override
    public void onClick(View view) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        DatePickerFragment fragment;
        switch(view.getId()){
        case R.id.fromDate:
            fragment = new DatePickerFragment();
            fragment.setOnDateSetListener(this, FROM_DATE_TAG);
            fragment.show(fragmentManager, "Date Picker");
            break;
        case R.id.toDate:
            fragment = new DatePickerFragment();
            fragment.setOnDateSetListener(this, TO_DATE_TAG);
            fragment.show(fragmentManager, "Date Picker");
            break;
        }
    }

    //If you want to update the alredy selected date, Specify it in budle. DatePickerFragment will take specified bundle date as default date.
    private void updateDate() {
        //get to date stored in SharedPrefrence
        String toDate = SharedPrefrenceUtils.getString(mContext, "FROM_DATE");
        SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy", Locale.getDefault());
        Date parse = sdf.parse(toDate);
        Calendar c = Calendar.getInstance();
        c.setTime(parse);
        FragmentManager fragmentManager = this.getActivity().getSupportFragmentManager();
        DatePickerFragment fragment = new DatePickerFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("YEAR", c.get(Calendar.YEAR));
        bundle.putInt("MONTH", c.get(Calendar.MONTH));
        bundle.putInt("DAY", c.get(Calendar.DATE));
        fragment.setArguments(bundle);
        fragment.setOnDateSetListener(this, FROM_DATE_TAG);
        fragment.show(fragmentManager, "Date Picker");
    }

    @Override
    public void onDateSet(DatePicker dialog, int year, int monthOfYear, int dayOfMonth, int reqCode) {
        if(reqCode == FROM_DATE_TAG){
            Calendar myCalendar  = Calendar.getInstance();
            myCalendar.set(year, monthOfYear, dayOfMonth);
            SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy", Locale.getDefault());
            String dateString = formatter.format(myCalendar.getTime());
            Log.i("From Date","DATE:"+dateString);//"Result = DATE:May 24, 2015"
            //Store FROM_DATE in SharedPrefrence
            SharedPrefrenceUtils.putString(mContext,"FROM_DATE", dateString);
        } else if(reqCode == TO_DATE_TAG){
            Calendar myCalendar  = Calendar.getInstance();
            myCalendar.set(year, monthOfYear, dayOfMonth);
            SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy", Locale.getDefault());
            String dateString = formatter.format(myCalendar.getTime());
            Log.i("To Date","DATE:"+dateString);//"Result = DATE:May 25, 2015"
            //Store TO_DATE in SharedPrefrence
            SharedPrefrenceUtils.putString(mContext,"TO_DATE", dateString);
        }
    }
}

Refer How to display SharedPreference stored data in Fragment? for SharedPrefrenceUtils Class.

Community
  • 1
  • 1
Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46
0

you can use Shared pref to store the last date picker value.