Trusted answers to developer questions

Zero to Kotlin hero: More on Anko Commons

Get Started With Data Science

Learn the fundamentals of Data Science with this free course. Future-proof your career by adding Data Science skills to your toolkit — or prepare to land a job in AI, Machine Learning, or Data Analysis.

The previous shot introduced the Anko library and briefly covered Anko Commons.

(If you are not familiar with Anko or how to add it to your Android project, please read that article before reading this).

In this article, we will cover the rest of the features that Anko Commons has. These features include helpers for logging, resources, and dimensions.

widget

AnkoLogger

AnkoLogger is part of anko-commons. If you have not added the anko-commons artifact to your project, I have written about how to add it here.

In my opinion, AnkoLogger does not necessarily affect the length of your code. However, it does make the code easier to read if you have an Android beginner trying to go through your codebase.

I mean, Log.d(TAG, "Message"); is cool, but for someone with a technical background in other domains other than Android, what does Log.d even mean?

Well, AnkoLogger to the rescue!

widget

The table below shows the translation of the android.util.Log class to its AnkoLogger equivalent.

widget

There are two ways to add AnkoLogger to your project:

  • Using AnkoLogger trait-like interface
  • Using AnkoLogger as an object

The trait-like interface

class MainActivity : Activity(), AnkoLogger {
fun doSomething() {
...
info("This is some information")
warn("Be careful")
}
}

AnkoLogger as an object

class MainActivity : Activity() {
private val log = AnkoLogger(this.javaClass)
private fun doSomething() {
...
log.info("This is some information")
log.warn("Be careful")
}

There are some helpers that are not grouped into any of the Anko subsystems (e.g., colors and dimensions).

RELATED TAGS

kotlin

CONTRIBUTOR

Adora Nwodo
Attributions:
  1. undefined by undefined
Did you find this helpful?