InnocentKiller
InnocentKiller

Reputation: 5234

How to get current day of week like monday etc and compare it in android

I have 7 different images of monday, tuesday, .... sunday. Now what i want to do is when screen open then current day image should be in different color.

I have tried with below code but not getting different color.

SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Date d = new Date();
String dayOfTheWeek = sdf.format(d);

TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(dayOfTheWeek);

Now in textview, I am getting Thursday as today is thursday. Now i have given condition like below.

    imageViewMon = (ImageView) findViewById(R.id.mon);
    imageViewTue = (ImageView) findViewById(R.id.tue);
    imageViewWed = (ImageView) findViewById(R.id.wed);
    imageViewThu = (ImageView) findViewById(R.id.thu);
    imageViewFri = (ImageView) findViewById(R.id.fri);
    imageViewSat = (ImageView) findViewById(R.id.sat);
    imageViewSun = (ImageView) findViewById(R.id.sun);



    if (dayOfTheWeek == "Monday") {
        imageViewMon.setColorFilter(0xFFFF3D60);
    }
    if (dayOfTheWeek == "Tuesday") {
        imageViewTue.setColorFilter(0xFFFF3D60);
    }
    if (dayOfTheWeek == "Wednesday") {
        imageViewWed.setColorFilter(0xFFFF3D60);
    }
    if (dayOfTheWeek == "Thursday") {
        imageViewThu.setColorFilter(0xFFFF3D60);
    }
    if (dayOfTheWeek == "Friday") {
        imageViewFri.setColorFilter(0xFFFF3D60);
    }
    if (dayOfTheWeek == "Saturday") {
        imageViewSat.setColorFilter(0xFFFF3D60);
    }
    if (dayOfTheWeek == "Sunday") {
        imageViewSun.setColorFilter(0xFFFF3D60);
    }

But I am not getting the image colour as i want, nothing happens just text view shows today's day but i am not getting different colour in my image.

Thanks in advance.

Upvotes: 1

Views: 2495

Answers (4)

Srneczek
Srneczek

Reputation: 2193

Question already answered but how can all of you use if or switch... Use array (or collection...) with desired values (strings, colors, whatever you want) and get number of the day, then just use the number as position in that array. This way you can switch just one array instead of rewriting all the long ifs/switches etc.

Upvotes: 0

flx
flx

Reputation: 14226

Try this:

Calendar c = Calendar.getInstance();
int d = c.get(Calendar.DAY_OF_WEEK);
switch (d) {
    case Calendar.MONDAY:
        imageViewMon.setColorFilter(0xFFFF3D60);
        break;
        // ...
}

You have two problems in your code:

  1. Comparing strings needs to be done with equals(), not ==.
  2. Depending on the locale, the user runs your app, it will never match, because 'Monday' is not 'Montag' or any other translation.

Upvotes: 1

silvia_aut
silvia_aut

Reputation: 1493

Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DAY_OF_WEEK);

switch(day) {
    case Calendar.MONDAY:
        ...
}

And you must compare strings with if(string1.compareTo(string2) == 0) { ...}

Upvotes: 1

mvieghofer
mvieghofer

Reputation: 2896

Try this:

Calendar cal = Calendar.getInstance();
int day = cal.get(Calendar.DAY_OF_WEEK);

switch(day) {
    case Calendar.MONDAY:
        imageViewMon.setColorFilter(0xFFFF3D60);
        break;
        ...
}

Upvotes: 4

Related Questions