1

A ListView in my application has many string elements like days, days name etc. I just want to make days bold.I want to only bold the days in the my app. To bold the string

I know that I can bold such text with help of Spannable. And the links can be displayed as follows :

private String getHeaderDate(String dateString) {
    try {


        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        Date date = dateFormat.parse(dateString);

        SimpleDateFormat dayFormat = new SimpleDateFormat("EEE");
        SimpleDateFormat monthFormat = new SimpleDateFormat("MMM");

        String dayName = String.valueOf(dayFormat.format(date));
        String month = String.valueOf(monthFormat.format(date));

        String day = dateString.split("/")[1];

        Calendar calendarGivenDate = Calendar.getInstance();
       // 
Date d1 = calendarGivenDate.getTime();

        Date d1 = calendarGivenDate.getTime();
        Date date1 = new Date();
        String stringSecond = dateFormat.format(d1);
        //
String stringSecond = dateFormat.format(d1);


        Date dateLater = dateFormat.parse(dateString);
        Date dateCurrent = dateFormat.parse(stringSecond);
      //  Date dateCurrent = dateFormat.parse(dateString);

       // Date dateLater = dateFormat.parse(stringSecond);


        long difference = dateLater.getTime() - dateCurrent.getTime();
        long daysDiff = TimeUnit.DAYS.convert(difference, TimeUnit.MILLISECONDS);

       // long daysDiff = TimeUnit.DAYS.convert(difference, TimeUnit.MILLISECONDS);
        String days = "";

        if(daysDiff == 1) {
            days = "Tomorrow";
            String finalString= days;
            Spannable sb = new SpannableString( finalString );
            sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), finalString.indexOf(days), days.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


        }

           else if(daysDiff == 0) {
            days = "Today";
            String finalString= days;
            Spannable sb = new SpannableString( finalString );
         sb.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), finalString.indexOf(days), days.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


        }
//
        return StringUtils.join(days.equals("") ? "" : StringUtils.join(days, " ", "•", " ") , dayName, " ", month, " ", day);

    } catch (Exception e) {
        e.printStackTrace();
        return "";
    }
}
Niki van Stein
  • 10,564
  • 3
  • 29
  • 62
Pooja Puri
  • 77
  • 5

0 Answers0