-1

I am not able to understand where am I doing mistake. I want to check it is a weekend or weekday. If it is weekend then it should prompt in the toast that it is a sunday or saturday weekend. I don't know what should I put in place of ??? mark. It should also display current date if it is weekend. Please, any one can help me. Thank you.

     View.OnClickListener//,
//       TimePickerDialog.OnTimeSetListener


      // TimePickerDialog.OnTimeSetListener
{
    //Declaration for class
    ButtonViews views;
    dpListener dpListenerView;
    // Declartion for member vairables
    int day, month, x_year;
    int hour;
    int minute;
    Calendar calendar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        views = new ButtonViews();
        dpListenerView = new dpListener();

        //ButtonListener
        views.button_date.setOnClickListener(this);
        views.button_time.setOnClickListener(this);
        //

       // pick up the default date using Calender class
         calendar =  GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.getDefault());

/*
        day = calendar.get(Calendar.DAY_OF_MONTH);
        month = calendar.get(Calendar.MONTH);
        x_year = calendar.get(Calendar.YEAR);
*/
        curr_date();

        hour = calendar.get(Calendar.HOUR_OF_DAY);
        minute = calendar.get(Calendar.MINUTE);

        setupDate(day, month, x_year);
        setupTime(hour, minute);
   }

    public void curr_date(){
        day = calendar.get(Calendar.DAY_OF_MONTH);
        month = calendar.get(Calendar.MONTH);
        x_year = calendar.get(Calendar.YEAR);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_date:
                showDatePickerDialog();
                break;
            case R.id.button_time:

                break;
        }
    }


    private void setupTime(int hours, int minutes) {
        views.button_time.setText(hours + ":" + minutes);
    }

    private void setupDate(int day, int month, int year) {

        String strMonth = ((month + 1) <= 9) ? ("0" + (month + 1)) : String.valueOf(month + 1);
                  views.button_date.setText(String.valueOf(day) + "/" + strMonth + "/" + String.valueOf(year));
    }
    private void showDatePickerDialog() {
        DatePickerDialog datepickerdialog = new DatePickerDialog
                (
                        this,
                         dpListenerView,
                       /* new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                            } },*/
                        //this,

                        x_year,
                        month,
                        day
                );

           datepickerdialog.show();
    }


  class ButtonViews {
        Button button_time;
        Button button_date;

        public ButtonViews() {
            button_date = (Button) findViewById(R.id.button_date);
            button_time = (Button) findViewById(R.id.button_time);
        }
    }

  class dpListener implements OnDateSetListener {
      @Override
      public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {


          Calendar c = Calendar.getInstance();
         // c.setTime();
          int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

         if(dayOfWeek == 7 || dayOfWeek == 1) {
              // as your requirement: you should display mesage they can not select the weekend" here
              // then you set the value in datepickerdialog by current date
              Toast.makeText(DateTimePickerActivity.this
                      , "You have selected weekend"
                      , Toast.LENGTH_SHORT).show();
          }


      }


   }
}
J. Vyas
  • 95
  • 1
  • 9

2 Answers2

0

Once you get your date you can do like this and get the day and proceed with your logic.

    Calendar c = Calendar.getInstance();
    c.setTime(yourDate);
    int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52
0

I think that your variable dayOfWeek is the same variable as the parameter dayOfMonth

The above code must work

public DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int selectedYear,
                          int selectedMonth, int selectedDay) {
        if (selectedDay == 1 || selectedDay == 7) {
            Toast.makeText(DateTimePickerActivity.this
                  , "You have selected weekend"
                  , Toast.LENGTH_SHORT).show();
            view.updateDate(selectedYear,selectedMonth,selectedDay);
        }
    }
};
Mario R.C.
  • 166
  • 1
  • 1
  • 11
  • I am editing my original code. Please, could you check and see what should I code. Thank you for the reply – J. Vyas Oct 06 '15 at 08:41
  • Maybe your problem is the type of event, check onDateChangedListener as this post: http://stackoverflow.com/questions/2051153/android-ondatechangedlistener-how-do-you-set-this – Mario R.C. Oct 06 '15 at 08:51