Reputation: 19
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?
Upvotes: 0
Views: 76
Reputation: 5835
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;
}
Upvotes: 0
Reputation: 1957
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));
}
Upvotes: 2
Reputation:
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.
Upvotes: 0