Let’s build an Android App with Kotlin

So if you are not living under a rock, you probably heard about Kotlin and the the buzz surrounding it.

The response from general audience of Android Developers is amazing. I was reluctant about learning kotlin but once I started(2 weeks ago), I fell in love with the language!.

let me share with you my first Android project with Kotlin. It is a simple android App that i built while exploring the amazing features kotlin offers.

I had built this App with Java but now I built it entirely with Kotlin and the simplicity with Kotlin amazed me.

It is a Fun-fact App that displays random fun facts on button click.

Let’s Build the App Together

  1. Getting started

Create a new project in Android Studio and call it FunFact or whatever you like. At the bottom check Include Kotlin Support

Image gotten from the first-project-kotlin.pdf guide by Antonioleiva.com

Use the default Phone & Tablet options and select Empty Activity when prompted for the Activity Type.

Image gotten from the first-project-kotlin.pdf guide by Antonioleiva.com

You can use the default values in the last screen of the wizard. Press Finish.

2. Let us create the layout for the interface of the app. Open activity_main.xml and Replace the existing code with the following:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/relativeLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent
android:background="#51b46d"
android:padding="50dp">


<TextView
android:id="@+id/didYouKnow_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Did you know?"
android:textSize="24sp"
android:layout_alignParentTop="true"/>

<TextView
android:id="@+id/fact_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A group of pandas is called Embarrasment?"
android:textSize="24sp"
android:gravity="center"
android:layout_centerInParent="true"/>

<Button
android:id="@+id/nextButton"
android:layout_above="@id/Show_fact_List_Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:padding="20dp"
android:text="Show another fun fact" />


</RelativeLayout>

3. create a new class and name FactBook. This is where we will store the facts to be displayed. Add the following code:

class FactBook {

val facts = arrayOf(
"Ants stretch when they wake up in the morning.",
"Ostriches can run faster than horses.",
"Olympic gold medals are actually made mostly of silver.",
"You are born with 300 bones; by the time you are an adult you will have 206.",
"It takes about 8 minutes for light from the Sun to reach Earth.",
"Some bamboo plants can grow almost a meter in just one day.",
"The state of Florida is bigger than England.",
"Some penguins can leap 2-3 meters out of the water.",
"On average, it takes 66 days to form a new habit.",
"Mammoths still walked the earth when the Great Pyramid was being built.")

fun getFact(): String {// we are returning a string

//displays a new fact
//Randomly displays the facts
val randomGenerator = Random()
val randomNumber = randomGenerator.nextInt(facts.size)
return facts[randomNumber]
}

}

4. create a new class and name it color. This where we will store the colors to be displayed. Add the following code:

class color {


//an array to store our random generated colors
internal var colors = arrayOf(
"#39add1", // light blue
"#3079ab", // dark blue
"#c25975", // mauve
"#e15258", // red
"#f9845b", // orange
"#838cc7", // lavender
"#7d669e", // purple
"#53bbb4", // aqua
"#51b46d", // green
"#e0ab18", // mustard
"#637a91", // dark gray
"#f092b0", // pink
"#b7c0c7" // light gray
)

fun getColor(): Int {
//displays a new color
//Randomly displays the colors
val randomGenerator = Random()
val randomNumber = randomGenerator.nextInt(colors.size)
return Color.parseColor(colors[randomNumber])

}

5. Finally let us integrate all these together!. notice that Kotlin allows us to use a layout view without using “findViewById”! and it simplifies the “setOnClickListener” code :

class MainActivity : AppCompatActivity() {

private var factbook = FactBook()
private var colorWheel = color()

private var mFacts: String = factbook.facts[0]
private var mColor: Int = Color.parseColor(colorWheel.colors[2])


public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)


nextButton.setOnClickListener {
mFacts = factbook.getFact()
indeterminateProgressDialog("Loading").show()
fact_textView.text = mFacts

//displays random colors
mColor = colorWheel.getColor()
relativeLayout.setBackgroundColor(mColor)
nextButton.setTextColor(mColor)


}


}

}

FINALLY our App is ready!!!!!!

Building Apps with Kotln is fun. To learn more about kotlin check out the official documention : https://kotlinlang.org/docs/tutorials/ or this article from the Android team : https://developer.android.com/kotlin/index.html

I am on a journey of exploring the amazing world of Kotlin. You can catch me up on Twitter

--

--

--

DevRel @bunzzdev | Founder @beacamphq | I love building products & supporting those who build.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Install And Configure SQL Server Reporting Services (SSRS)

Classic Offensive on FACEIT & a lot of bug fixing

Dev Day 30: Creating a Spawn Manager for the Triple Shot Power Up in Unity3D

The Spartan Diaries part 2

Beginner’s guide to Git

GitHub Gist

Ember Composable Concurrency

Notion API — Laravel Authentication Example

PlugShare is selling your data

The user is the product

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Trust Onyekwere

Trust Onyekwere

DevRel @bunzzdev | Founder @beacamphq | I love building products & supporting those who build.

More from Medium

RecycleView Kotlin — Android Studio

CameraX in Android

Kotlin for Android Development(Part-2)

Dispatchers in Kotlin Coroutines