Search code examples
androidandroid-studioandroid-calendar

how to change backgrounds color of cardview day wise?


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?


Solution

  • 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));
    }