In Java, i can easily pass static function to layout xml using:
public static String formatUnixTime(long timeInSeconds, String pattern) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, Locale.US);
String value = simpleDateFormat.format(new Date(timeInSeconds * 1000));
return value;
}
in xml:
android:text='@{Utils.formatUnixTime(model.start_time, "hh:mm:ss")}'
But i tried in Kotlin with companion
but no luck. It said
error: cannot find symbol
import my.package.name.HistoryItemBindingImpl;
^
symbol: class HistoryItemBindingImpl
location: package my.package.name
This is what i tried in kotlin
class Utils {
companion object {
fun formatUnixTime(timeInSeconds : Long, pattern: String) : String {
val simpleDateFormat = SimpleDateFormat(pattern, Locale.US)
val value = simpleDateFormat.format(Date(timeInSeconds * 1000))
return value
}
}
And in xml
android:text='@{Utils.Companion.formatUnixTime(model.start_time, "hh:mm:ss")}'
Really hope someone can help. Thanks!
Update With @Max Aves help. I fixed my code and below code will work. Maybe it will help someone.
class Utils {
companion object {
@JvmStatic
fun formatUnixTime(timeInSeconds : Long, pattern: String) : String {
val simpleDateFormat = SimpleDateFormat(pattern, Locale.US)
val value = simpleDateFormat.format(Date(timeInSeconds * 1000))
return value
}
And you can use this in xml
android:text='@{Utils.formatUnixTime(model.start_time, "hh:mm:ss")}'