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 familiar with date picker dialog but don't have any idea about date spinner.
Note
Please don't tell me How to style EditText as Spinner
EDITED: 1 AS RECOMMENDED BY @erakitin
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_date);
Spinner spinnerDateIn = (Spinner) findViewById(R.id.spinnerDateIn);
Spinner spinnerDateOut = (Spinner) findViewById(R.id.spinnerDateOut);
spinnerDateIn.setAdapter(new CalendarSpinnerAdapter(SpinnerDateActivity.this, 30));
}
EDITED: 2 AS RECOMMENDED BY @erakitin
in new update
CalendarSpinnerAdapter mSpinnerDateInAdapter = new CalendarSpinnerAdapter(SpinnerDateActivity.this, 30);
spinnerDate.setAdapter(mSpinnerDateInAdapter);
spinnerDate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
spinnerDate.setSelection(position);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Now, trying to get value of selected Item in String but getting: java.util.GregorianCalendar
, where i am doing mistake ?
strDate = spinnerDate.getSelectedItem().toString();
There are a simple example of adapter for
Spinner
below.How to use it for display current date and next 29 days:
UPD:
We should add a parameter to adapter's constructor for setting the starting date:
Then add listener for
spinnerDateIn
where we can initialize the second spinner:I didn't try this code but it should work.
UPD2:
spinnerDate.setSelection(position)
returnsCalendar
class instance. If you want to get selected date asString
you should format it. Try to use following method: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,
Now we define an
ArrayList<MyDateInterval>
, that we will fill up with the date you want to show:Retrieve the end date on the format of your example:
Now we calculated the interval from today up to the end date
Now we all the pieces. You have to create the Adapter for the Spinner.
and that should be all.