Skip to content Skip to sidebar Skip to footer

How To Create Spinner To Show Current And Next 30 Dates

How can I create a date Spinner, which shows current date in EditText as default and future dates in Spinner (like, for next 30 days) I used date picker in many apps, so I am famil

Solution 1:

There are a simple example of adapter for Spinner below.

publicclassCalendarSpinnerAdapterextendsBaseAdapter {

    privateSimpleDateFormatmDateFormat=newSimpleDateFormat("d MMM yyyy");

    private LayoutInflater mInflater;
    private Calendar mCalendar;
    privateint mDayCount;
    privateintmLastRequestedDay=0;

    publicCalendarSpinnerAdapter(Context context, int dayCount) {
        mInflater = LayoutInflater.from(context);
        mDayCount = dayCount;
        mCalendar = Calendar.getInstance();
    }

    @OverridepublicintgetCount() {
        return mDayCount;
    }

    @Overridepublic Calendar getItem(int position) {
        mCalendar.add(Calendar.DAY_OF_YEAR, position - mLastRequestedDay);
        mLastRequestedDay = position;
        return mCalendar;
    }

    @OverridepubliclonggetItemId(int position) {
        return position;
    }

    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mInflater.inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);
        }

        Calendaritem= getItem(position);
        ((TextView) convertView).setText(mDateFormat.format(item.getTimeInMillis()));

        return convertView;
    }

    @Overridepublic View getDropDownView(int position, View convertView, ViewGroup parent) {
        return getView(position, convertView, parent);
    }
}

How to use it for display current date and next 29 days:

mDateSpinner.setAdapter(newCalendarSpinnerAdapter(getActivity(), 30));

UPD:

We should add a parameter to adapter's constructor for setting the starting date:

publicCalendarSpinnerAdapter(Context context, Calendar startDate, int dayCount) {
    mInflater = LayoutInflater.from(context);
    mDayCount = dayCount;
    mCalendar = Calendar.getInstance();
    mCalendar.setTimeInMillis(startDate.getTimeInMillis());
}

Then add listener for spinnerDateIn where we can initialize the second spinner:

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_date);

    SpinnerspinnerDateIn= (Spinner) findViewById(R.id.spinnerDateIn);
    finalSpinnerspinnerDateOut= (Spinner) findViewById(R.id.spinnerDateOut);

    mSpinnerDateInAdapter = newCalendarSpinnerAdapter(SpinnerDateActivity.this, Calendar.getInstance(), 30);
    spinnerDateIn.setAdapter(mSpinnerDateInAdapter);

    spinnerDateIn.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {
        @OverridepublicvoidonItemSelected(AdapterView<?> parent, View view, int position, long id) {
            CalendardateIn= Calendar.getInstance();
            dateIn.setTimeInMillis(mSpinnerDateInAdapter.getItem(position).getTimeInMillis());
            dateIn.add(Calendar.DAY_OF_YEAR, 1); // add one day
            mSpinnerDateOutAdapter = newCalendarSpinnerAdapter(SpinnerDateActivity.this, dateIn, 30);
            spinnerDateOut.setAdapter(mSpinnerDateOutAdapter);
        }

        @OverridepublicvoidonNothingSelected(AdapterView<?> parent) {

        }
    });
}

I didn't try this code but it should work.

UPD2:

spinnerDate.setSelection(position) returns Calendar class instance. If you want to get selected date as String you should format it. Try to use following method:

privateStringgetSelectedDateAsString(Spinner dateSpinner) {
    Calendar selectedDate = (Calendar) dateSpinner.getSelectedItem();
    returnnewSimpleDateFormat("d MMM yyyy").format(selectedDate.getTimeInMillis());
}

Solution 2:

Spinner works with an Adapter. If you want to show in your spinner the date from today up to the end of the month you can do. Let's create a model class, with one can use to feed the Adapter,

publicclassMyDateInterval {
    publicString mDateString;
    publicDate mDate;

    publicMyDateInterval(String dateString, Date date) {
        mDateString = dateString;
        mDate = date;
    }

    @OverridepublicStringtoString() {
        return mDateString;
    }
}

Now we define an ArrayList<MyDateInterval>, that we will fill up with the date you want to show:

ArrayList<MyDateInterval> items = new ArrayList<>();

Retrieve the end date on the format of your example:

SimpleDateFormatdateFormat=newSimpleDateFormat("dd MMM yyyy");
Calendarc= Calendar.getInstance();
c.set(Calendar.MONTH, 6);
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
// for the sake of the example 31 Jul 2015DateendDate= c.getTime();
Log.e(getClass().getSimpleName(), dateFormat.format(endDate));

Now we calculated the interval from today up to the end date

Calendar todayCalendar = Calendar.getInstance();
while(endDate.after(todayCalendar.getTime())) {
    items.add(new MyDateInterval(dateFormat.format(todayCalendar.getTime()), todayCalendar.getTime());
    todayCalendar.add(Calendar.DAY_OF_MONTH, 1);
 }

Now we all the pieces. You have to create the Adapter for the Spinner.

ArrayAdapter<MyDateInterval> dataAdapter = new ArrayAdapter<>(this,
                            android.R.layout.simple_spinner_item, items);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);

and that should be all.

Post a Comment for "How To Create Spinner To Show Current And Next 30 Dates"