I have a DatePicker
with a button that gives a DialogBox to select date and an EditText txtDate
to set the date on it :
Layout
:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/date_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.50"
android:text="Pickup Date*"
android:textStyle="bold" />
<EditText
android:id="@+id/txtDatee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2.28" >
</EditText>
<Button
android:id="@+id/btnCalendar"
android:layout_width="30dp"
android:layout_height="42dp"
android:layout_marginRight="50dp"
android:background="@drawable/date" >
</Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="62dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView5"
android:layout_width="126dp"
android:layout_height="wrap_content"
android:text="Pickup Time"
android:textStyle="bold" />
<Spinner
android:id="@+id/hh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Spinner
android:id="@+id/mm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
onClick[DatePicker
]:
public void onClick(View v) {
if (v == btnCalendar) {
// Process to get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// Display Selected date in textbox
if (year < mYear)
view.updateDate(mYear,mMonth,mDay);
if (monthOfYear < mMonth && year == mYear)
view.updateDate(mYear,mMonth,mDay);
if (dayOfMonth < mDay && year == mYear && monthOfYear == mMonth)
view.updateDate(mYear,mMonth,mDay);
txtDate.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
Date newDate2 = c.getTime();
dpd.getDatePicker().setMinDate(newDate2.getTime());
dpd.show();
} }
And also two spinners with id's hh
and mm
for the hours and minutes which have static array values.
String[] pickup_hour = { "HH", "00", "01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17",
"18", "19", "20", "21", "22", "23" };
String[] pickup_min = { "MM", "00", "05", "10", "15", "20", "25", "30",
"35", "40", "45", "50", "55" };
sp3 = (Spinner) findViewById(R.id.hh);
sp3.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {
edsp3.setText(pickup_hour[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
ArrayAdapter b3 = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, pickup_hour);
b3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp3.setAdapter(b3);
sp4 = (Spinner) findViewById(R.id.mm);
sp4.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {
edsp4.setText(pickup_min[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
ArrayAdapter b4 = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, pickup_min);
b4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp4.setAdapter(b4);
The DatePicker
is disabled for previous dates based on the current date.
So how do fix the hours and minutes based on the DatePicker
Date?
Thank you for your help.
EDIT:
This is what i've done inside the hour spinner :
sp3 = (Spinner) findViewById(R.id.hh);
sp3.setOnItemSelectedListener(new OnItemSelectedListener() {
Calendar c1 = Calendar.getInstance();
int hour = c1.get(Calendar.HOUR_OF_DAY);
@Override
public void onItemSelected(AdapterView<?> parent, View v,
int position, long id) {
Log.d("aaa", ""+hour);
edsp3.setText(pickup_hour[position]);
if(position<hour)
{
Toast.makeText(OnlineBooking.this, "Choose greater hour",
Toast.LENGTH_LONG).show();
}}