This is a tutorial project to show how to setup and use Kotlin in Android Project. I have recently started going to the gym, and I found out that it would be easier if I had application to assist me. I am calling this application 'Workout'.
Work on this application goes in steps, each step either integrates some new functionality or demonstrates usage of specific feature of Kotlin Language.
Each step has its own tag (name in the round brackets), and a corresponding video on Youtube.
- Creating new Project in Android Studio
- Initialising GIT
- Adding Kotlin plugins to build.gradle
- Converting Java Code into Kotlin Code
- Adding Kotlin Android Extensions Plugin
apply plugin: 'kotlin-android-extensions' - Using auto-generated properties for views in View Class (Activity)
import kotlinx.android.synthetic.main.home.*
- Code Cleanup
- Adding Anko library to the project
compile "org.jetbrains.anko:anko:$versions.anko" - Examples of one line functions (Kotlin introduces specific syntax)
fun isThisAJoke() = "NO Its not a Joke"
- Adding Anko Design library to the project
compile "org.jetbrains.anko:anko-design:$versions.anko" - Examples of Anko usage:
snackbar
doAsync
uiThread
startActivity<HomeActivity>()
- Implementation of Splash Screen to enhance User Experience
- Based on: https://www.bignerdranch.com/blog/splash-screens-the-right-way/
- Integration of dagger into Android Kotlin Project.
- Creation and usage of 'Extension Function'
fun AppCompatActivity.component() : ApplicationComponent = (application as Application).component - Creation and usage of 'Extension Property'
val AppCompatActivity.component: ApplicationComponent get() = (application as Application).component
- Using CountDownTimer for executing task after interval.
- Introduction to 'val' property
val name: String = "tomasz" - Introduction to 'var' property
@Inject lateinit var context: Context - Introduction to anonymous class in kotlin
val timer = object : CountDownTimer(...
- Lateinit var property
lateinit var onFinishFunction: () -> Unit) - Nullability in practice
private var onFinishFunction: (() -> Unit)? = null - Function/Lambda as a property
lateinit var onTickFunction: (Long) -> Unit - Function/Lambda as a method argument
fun onFinish(onFinishFunction: () -> Unit) { - Invocation of Function/Lambda
onFinishFunction.invoke()
onFinishFunction()
- Adding new Dagger Module
- Initializing component with two Modules
- Creating Singleton object in Module
- Val Property Lazy Initialization
private val ringtone by lazy {
RingtoneManager.getRingtone(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
} - Val Property with Getter
private val flags: Int
get() = PowerManager.SCREEN_BRIGHT_WAKE_LOCK or PowerManager.ACQUIRE_CAUSES_WAKEUP
- Method with Default Arguments
fun replaceFragment(fragment: Fragment, tag: String = fragment.javaClass.name, addToBackStack: Boolean = false) { - Constructor with Default Arguments
class ContentFragmentManager(private val activity: FragmentActivity, private val contentLayoutId: Int = R.id.contentFrame) { - Showing Fragment inside Activity
- Creating
ContentFragmentManagerto replace fragments inFrameLayout
- Creating BaseFragment
- Creating Abstract Property
protected abstract val layoutResourceId: Int - Creating Open Property
open protected val menuResourceId: Int? = null - Making 'Java Virtual method' final
final override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?)
- Creating Custom Property Setter
var delegate: Delegate? = null
set(value) {
field = value
notifyStateChanged()
} - Creating Data Model
- Creating Data Manager
- String Template with value
timerText.text = "$value" - String Template with expression
seriesNumberText.text = "${exerciseDataManager.exerciseData.seriesNumber}"
- Using Elvis Operator
fragmentManager.fragments.firstOrNull { it?.isVisible ?: false } - Using equality '==' sign correctly
if (currentFragment?.javaClass != fragment.javaClass) {
- Creating Dagger Module for specific Activity
- Creating Dagger Subcomponent
@Subcomponent(modules = arrayOf(ExerciseModule::class)) - Creating Custom Scope to be used by Subcomponent
annotation class ActivityScope
- Creating Alert Dialog using Anko Library
[Kotlin.org] (https://kotlinlang.org/)
[Kotlink.Anko] (https://github.com/Kotlin/anko/)
[Dagger] (https://github.com/google/dagger)
Tomasz Morcinek
tomasz.morcinek@gmail.com
tomasz.morcinek.dev@gmail.com
You are free to do whatever you want with it.