0

I am making a calendar for android, I picked up this tutorial on this site: http://www.androidhub4you.com/2012/10/custom-calendar-in-android.html

I'm having trouble changing some code lines, three problems:

  • one: the color that indicates the today's day is appearing every month. I would like to only appear in the current month.

  • two: change the color of the day on Sunday and Monday to red.

  • three: Do not allow clicks in the days of Sunday and Monday


Code:

Problems one and two:

    for (int i = 1; i <= daysInMonth; i++) {
        Log.d(currentMonthName, String.valueOf(i) + " " 
            + getMonthAsString(currentMonth) + " " + yy);

        if (i == getCurrentDayOfMonth()) {
            list.add(String.valueOf(i) + "-BLUE" + "-"
                + getMonthAsString(currentMonth) + "-" + yy);
        }
        //else if not working
        else if (i == Calendar.SUNDAY){
            list.add(String.valueOf(i) + "-RED" + "-"
                + getMonthAsString(currentMonth) + "-" + yy);
        } else {
            list.add(String.valueOf(i) + "-WHITE" + "-"
                + getMonthAsString(currentMonth) + "-" + yy);
        }
    }

Problem three:

    Override
    public void onClick(View view) {

        // i cant implement the if statament for that :(

        String date_month_year = (String) view.getTag();

        Intent intent = new Intent(getBaseContext(), func_agenda.class);
          intent.putExtra("user_auth", "Paulo");
          intent.putExtra("user_date", date_month_year);
          startActivity(intent);
        try {
            Date parsedDate = dateFormatter.parse(date_month_year);
            Log.d(tag, "Parsed Date: " + parsedDate.toString());

        } catch (ParseException e) {

              e.printStackTrace();
        }



    }
Ryan Vincent
  • 4,483
  • 7
  • 22
  • 31
Frank021
  • 121
  • 2
  • 11

1 Answers1

0

Problem 1:

Currently you are checking if the number of the day (i) is the same as the current day number of the month (getCurrentDayOfMonth()), so, for example; today Sept-2 the number of the day is 2, regardless the month. You have to be more strict about your condition. You have to check not just the day but also the month where you are.

Probably you will have to change your code from:

if (i == getCurrentDayOfMonth()) {

to something like:

if (i == getCurrentDayOfMonth() && mm == _calendar.get(Calendar.MONTH) ) { //Check if mm is from zero to 11. If not the condition should be mm-1 ==... 


Problem 2:

The values of i will be from 1 to 30&something (or 28/29 in some cases). The value of the constant Calendar.SUNDAY is 1. So, in your code you are asking if the current value of i is equal to 1. You have to see if the current value of i on the current month that you are displaying is SUNDAY, how? this post can hep you: how-to-determine-day-of-week-by-passing-specific-date

Probably you will have to change your code from:

else if (i == Calendar.SUNDAY){

to something like:

else if ( newMethodToGetDayOfWeek(i,mm,yy) == Calendar.SUNDAY){


Problem 3:

Here you have to check, after click on the day of the calendar if the selected button, is Sunday or Monday. In the code that you posted before, you can see that they are creating a Date object, (parseDate). You can check if this Date is MONDAY/SUNDAY and based on that start or not the activity..

From your code to:

Override
public void onClick(View view) {

    String date_month_year = (String) view.getTag();

    try {
        Date parsedDate = dateFormatter.parse(date_month_year);
        Log.d(tag, "Parsed Date: " + parsedDate.toString());

        if( anotherNewMethodToSeeIfTheDateIsSundayOrMonday( parsedDate ) == false )
        {
            Intent intent = new Intent(getBaseContext(), func_agenda.class);
            intent.putExtra("user_auth", "Paulo");
            intent.putExtra("user_date", date_month_year);
            startActivity(intent);
        } 
    } catch (ParseException e) {
          e.printStackTrace();
    }
}

Where anotherNewMethodToSeeIfTheDateIsSundayOrMonday should be something like this:

private bool anotherNewMethodToSeeIfTheDateIsSundayOrMonday(Date date)
{
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);
    int doWeek = cal.get(Calendar.DAY_OF_WEEK)
    if( doWeek == Calendar.SUNDAY || doWeek == Calendar.MONDAY )
    {
        return true;
    }

    return false;
}

From here

Community
  • 1
  • 1
mayo
  • 3,845
  • 1
  • 32
  • 42