0

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();

        }}
MetaldroiD
  • 436
  • 2
  • 5
  • 17
  • just to understand better, on the selection of date from date picker you want to set the spinners correct ? – Rat-a-tat-a-tat Ratatouille Mar 21 '14 at 06:58
  • yes, like for example the current time here is 12:30PM and date is 21.03.2014, so if i choose the date as today's from the `DatePicker`, all times post 12:30PM should be disabled on the `Spinner`s or maybe error `Toast` on selection. – MetaldroiD Mar 21 '14 at 07:03

2 Answers2

0

First of all, It is always recommendable that you use the inbuilt DatePicker and TimePicker widgets of android.

Still if you wish to use spinners then populate the spinners programmatically

 System.currentTimeMillis();

will give you current time of device then check current hour and minute and accordingly populate the spinners

Other Method would be by validating the same at the submit button click event

means check the selected time with current system time

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
0

Depending on the clock system, either 24 hours or 12 hours system, get

Calendar c = Calendar.getInstance(); 
int seconds = c.get(Calendar.SECOND);
int minutes = c.get(Calendar.MINUTE);
int hour = c.get(Calendar.HOUR); // or HOUR_OF_DAY

When the spinenrs onItemSelected listener gets called then, compare the values with what you have in the seconds, hours, and minutes variables, and accordingly display the message.

  • Nice. But i have a question. How do i add the current hours,minutes and seconds in the `onItemSelected` listener along with these values? `public void onItemSelected(AdapterView> parent, View v, int position, long id)` – MetaldroiD Mar 21 '14 at 08:41
  • and why do you want to add them ? you mentioned that the user would be selecting them himself/herself isnt it ? – Rat-a-tat-a-tat Ratatouille Mar 21 '14 at 08:42
  • For comparing inside the listener? Isn't that required? – MetaldroiD Mar 21 '14 at 08:44
  • 1
    OnItemSelected will get called the moment the user selects an item from the spinner, so you could just get the values of the hours, min from the variables inside it. In order to get the selected item value, do pickup_hour[position] (for the hour, for example) – Rat-a-tat-a-tat Ratatouille Mar 21 '14 at 08:46
  • Okay cool. But i'm having a hard time fixing it inside the listener. It doesn't work properly as desired. See `EDIT` above. Where did i go wrong – MetaldroiD Mar 21 '14 at 12:17