0

I'm working on one app. In that app, there is a card view. I want to change background color of the card view day wise.

Like Monday = red color Tuesday = Green color

till Sunday! and from next Monday, it will then start from first like Monday=red color

Any idea? how to achieve this?

I tried this

Calendar calendar = Calendar.getInstance();
        String currentDate = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());

It gives me output like this

Sunday, April 19, 2020

now, how can I use this to change the background color?

3 Answers3

2

Use SimpleDateFormat to format dates and times into a human-readable string, with respect to the user's locale.

Example to get the current day of the week (e.g. "Sunday"):

Calendar c= Calendar.getInstance();
SimpleDateFormat sd=new SimpleDateFormat("EEEE");
String dayofweek=sd.format(c.getTime());

Now change the background color :

if (dayofweek.equals("Saturday")) {
    cardview.setBackgroundColor(getResources().getColor(color.white));
}
alexrnov
  • 2,346
  • 3
  • 18
  • 34
Muntasir Aonik
  • 1,800
  • 1
  • 9
  • 22
0

int dayOfWeek=bday.get(Calendar.DAY_OF_WEEK); // Returns 3, for Tuesday

Then use if ,else if statements for different colours for different days accordingly.

0

You can achieve it with the following code

Calendar calendar = Calendar.getInstance();
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);


switch (dayOfWeek) {
  case Calendar.MONDAY:
    cardView.setBackground(ContextCompat.getColor(context, R.color.color_monday));
    break;
  case Calendar.TUESDAY:
    cardView.setBackground(ContextCompat.getColor(context, R.color.color_tuesday));
    break;
  case Calendar.WEDNESDAY:
    cardView.setBackground(ContextCompat.getColor(context, R.color.color_wednesday));
    break;
  case Calendar.THURSDAY:
    cardView.setBackground(ContextCompat.getColor(context, R.color.color_thursday));
    break;
  case Calendar.FRIDAY:
    cardView.setBackground(ContextCompat.getColor(context, R.color.color_friday));
    break;
  case Calendar.SATURDAY:
    cardView.setBackground(ContextCompat.getColor(context, R.color.color_saturday));
    break;
  case Calendar.SUNDAY:
    cardView.setBackground(ContextCompat.getColor(context, R.color.color_sunday));
    break;
}
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92