Android Tv News gets its own app, written in Kotlin. Read, watch, and listen as much as you want.

There’s one more app that we’re launching for Android TV, one that fits pretty well into the overall brand. That is “News About Android TV”, an app which curates all of the site’s content from various sources and makes it available for our users in a simple TV app.

It pulls data from content feeds, making everything fresh and up-to-date. Users can browse our news in a TV-optimized article viewer, watch our YouTube videos, or listen to our podcasts. The bottom row is a selection of apps we’ve published, quickly linking users to the Play Store.

This is free to download on Google Play, and we have published the source code on GitHub. What’s interesting about this app is that a lot of it was written in Kotlin. Starting with the TV Leanback sample app, I converted a lot of code to Kotlin, writing my own when necessary. Some of the conversion was done using the new code conversion tool in Android Studio, and a fair amount was done by studying and researching the language.

Kotlin is neat. It uses a variety of syntactic sugar features to make code a lot more nimble and stable. I’ll illustrate some examples of this.

In MainFragment.kt I have to load items from different sources and insert them into the UI. I have a general class for this, called Card, which has a variety of parameters to fit every situation. Not every parameter needs to be used in every case, and constructors can now have defined parameters which allow me to specify what is included and not. This gives me better context for what each value is as well as reduces the need to create a variety of different constructors.

Notice how the example below doesn’t have a bgImageUrl.

Card(type = Card.TYPE_APP, primaryText = "NeoDash", secondaryText = "Custom Screensaver", extra = "news.androidtv.neodash", imageUrl = "")

Another neat feature is the ability to insert the value of a variable inside of a string without having to do any direct concatenation.

Card(type = Card.TYPE_PODCAST,
primaryText = podcast.getString("title"),
secondaryText = podcast.getString("created_at"),
imageUrl = "",
bgImageUrl = "",
extra = "${podcast.getString("stream_url")}?client_id=$SOUNDCLOUD_API_KEY")

Perhaps the language feature that will save the most time is around standard objects. Java and Kotlin, being object-oriented, allows us to create a Card and manipulate it in various ways. In Java, these manipulations had to be created manually. In Kotlin, they’re provided by default. Kotlin goes further to make the getters and setters even nicer to view. These features allow our Card class to become much smaller than it would in its Java equivalent.

class Card(val type: Int = Card.TYPE_ARTICLE, val imageUrl: String? = null,
val bgImageUrl: String? = null, val primaryText: String = "",
val secondaryText: String = "", val extra: String = "") {
companion object {
val TYPE_VIDEO = 1
val TYPE_APP = 30

As you can see, the constructor has a variety of parameters that can have defaults. This allows us to set parameters by name, as above. Some of them have question marks at the end, indicating these values can be null. The ones without question marks are marked as non-null and can never be null. This should reduce NullPointerExceptions, one of the most common types of errors in Java.

Kotlin compiles to Java, and runs on top of the Java Virtual Machine. This means that any developer who wants to get started with Kotlin doesn’t need to worry about OS compatibility or app problems. The Kotlin files should run just as well as in Java. However, these optimizations allow the code to look better and be smaller. For developers who are working on large projects, the types of improvements with Kotlin may be worthwhile. If you’re hesitant to overhaul your entire app, you can start with one class. Kotlin and Java work well together and projects can have a mix of both.

What do you think? Are you going to give Kotlin a try? Let us know in the comments below.

News about Android Tv
News about Android Tv
Developer: Felker Tech
Price: Free

Nick Felker

Nick Felker

Nick Felker is a student Electrical & Computer Engineering student at Rowan University (C/O 2017) and the student IEEE webmaster. When he's not studying, he is a software developer for the web and Android (Felker Tech). He has several open source projects on GitHub ( Devices: Moto G-2013 Moto G-2015, Moto 360, Google ADT-1, Nexus 7-2013 (x2), Lenovo Laptop, Custom Desktop. Although he was an intern at Google, the content of this blog is entirely independent and his own thoughts.

More Posts - Website

Follow Me:
TwitterLinkedInGoogle PlusReddit