how can i style just a part of a timeformat?
The hours and minutes shoud me in a darkblue and the rest in a lightblue.
It should be also in the users local format, like on this image.
Anyone know how to do something like this?
Asked
Active
Viewed 26 times
0

Freddy Ochner
- 165
- 1
- 10
1 Answers
2
You can color parts of a TextView
text in different colors using Spannable
. See some examples here.
The example below shows how to get time string according to local format by calling the method getTimeString(Context context, Date date)
.
All the methods in the example are static so you can put them in some Utils
class and call it like Utils.getTimeString(context, date)
.
public static String getTimeString(Context context, Date date) {
DateFormat dateFormat = new SimpleDateFormat(getTimePattern(context), currentLocale(context));
return dateFormat.format(date);
}
public static boolean is24HourTimeFormat(Context context) {
return android.text.format.DateFormat.is24HourFormat(context);
}
private static String getTimePattern(Context context) {
return is24HourTimeFormat(context) ? "HH:mm" : "hh:mm a";
}
private static Locale currentLocale(Context context) {
return context.getResources().getConfiguration().locale;
}

dzikovskyy
- 5,027
- 3
- 32
- 43