Hi im Trying to implement a Route Preview with a Dummy Coordinate but I get this Error
FATAL EXCEPTION: main Process: com.pkg.ir, PID: 23089 java.lang.RuntimeException: java.lang.IllegalArgumentException: All rows must have either a distance or duration span attached to either its title or texts
This is My code
And im using this library: implementation "androidx.car.app:app:1.4.0-rc02"
class RoutePreviewScreen(carContext: CarContext) : Screen(carContext) {
@SuppressLint("UnsafeOptInUsageError")
override fun onGetTemplate(): Template {
val builder = RoutePreviewNavigationTemplate.Builder()
.setTitle("Route Details")
.setHeaderAction(Action.APP_ICON)
val currentLocation = CarLocation.create(19.269160, -103.754996)
val locationA = CarLocation.create(19.2689369, -103.7636865)
val markerA = PlaceMarker.Builder().setColor(CarColor.RED).build()
val placeA = Place.Builder(locationA).setMarker(markerA).build()
val distancia = 5
val duracion = 20
val distanceSpan = DistanceSpan.create(Distance.create(distancia.toDouble(), Distance.UNIT_KILOMETERS))
val durationSpan = DurationSpan.create(duracion.toLong())
val routeItem = Row.Builder()
.setTitle(CarText.create("From current to A"))
.addText(CarText.create("Distance: $distanceSpan"))
.addText(CarText.create("Duration: $durationSpan"))
.setMetadata(
Metadata.Builder()
.setPlace(placeA)
.build()
)
.build()
val itemList = ItemList.Builder()
.addItem(routeItem)
.build()
builder.setItemList(itemList)
val navigateAction = Action.Builder()
.setTitle("Iniciar NavegaciĆ³n")
.setOnClickListener {
//TODO IMPLEMENT NAVIGATION
}
.build()
builder.setNavigateAction(navigateAction)
return builder.build()
}
}
Step 10 of the Learn Car App Library Fundamentals codelab shows an example of how to do this!
The issue with what you're doing right now is that "Distance: $distanceSpan"
doesn't actually apply the span, it just uses Kotlin string interpolation, resulting in a regular string, not a spanned string.
Spantastic text styling with Spans has more details on working with spans in general, which also applies to the car specific spans like the DistanceSpan
, DurationSpan
mentioned here, and others like the CarIconSpan