Make Your First Android App with Kotlin (Android Kotlin Tutorial for Beginners) (Code)

Kotlin is a new official language for Android development. It’s certainly a step towards safer, readable and quicker to write code.

Kotlin supports properties, you don’t have to call “findViewById()”, and it also tries to get rid of the dreaded null reference exception.

Whether you are a complete beginner who is just starting out in Android app development or you are a Java Android developer looking for something new, you cannot go wrong with Kotlin in Android.

 

This post contains all the code that’s been written in this YouTube video.

 

You can also check out this GitHub repository: https://github.com/ResoCoder/FirstKotlinAndroidApp

 

MainActivity.kt

package com.resocoder.firstapptut

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.SeekBar
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

        val initialTextViewTranslationY = textView_progress.translationY

        seekBar.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener{
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                textView_progress.text = progress.toString()

                val translationDistance = (initialTextViewTranslationY +
                        progress * resources.getDimension(R.dimen.text_anim_step) * -1)

                textView_progress.animate().translationY(translationDistance)
                if (!fromUser)
                    textView_progress.animate().setDuration(500).rotationBy(360f)
                            .translationY(initialTextViewTranslationY)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {

            }

        })

        button_reset.setOnClickListener { v ->
            seekBar.progress = 0
        }
    }
}

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.resocoder.firstapptut.MainActivity">

    <TextView
        android:id="@+id/textView_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="60sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.369" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="239dp"
        android:layout_height="40dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:max="10"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView_progress" />

    <Button
        android:id="@+id/button_reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Reset"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/seekBar"
        app:layout_constraintVertical_bias="0.112" />

</android.support.constraint.ConstraintLayout>

 

dimens.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="text_anim_step">5dp</dimen>
</resources>

 

About the author 

Matt Rešetár

Matt is an app developer with a knack for teaching others. Working as a freelancer and most importantly developer educator, he is set on helping other people succeed in their Flutter app development career.

You may also like

  • Hi,
    there seems to be a type conflict.

    val translationDistance = (initialTextViewTranslationY +
    progress * resources.getAnimation(R.dimen.text_anim_step) * -1)

    /home/toraus/StudioProjects/AppTutorial/app/src/main/java/com/example/toraus/apptutorial/MainActivity.kt: (20, 34): None of the
    following functions can be called with the arguments supplied:
    public final operator fun times(other: Byte): Int defined in kotlin.Int
    public final operator fun times(other: Double): Double defined in kotlin.Int
    public final operator fun times(other: Float): Float defined in kotlin.Int
    public final operator fun times(other: Int): Int defined in kotlin.Int
    public final operator fun times(other: Long): Long defined in kotlin.Int
    public final operator fun times(other: Short): Int defined in kotlin.Int

  • {"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
    >