Kotlinx Datetime

Dependency Add the following to your dependencies implementation(org.jetbrains.kotlinx:kotlinx-datetime:0.2.0) Usage import kotlinx.datetime.* val today = Clock.System.todayAt(TimeZone.currentSystemDefault()) If you target Android devices running below API 26, you need to use Android Gradle plugin 4.0 or newer and enable core library desugaring. https://github.com/Kotlin/kotlinx-datetime

How to create a mutable list in Kotlin?

Kotlin has its own keyword to create list which are mutable As a child I learned that our solar system has nine planets val planets = mutableListOf(“Mercury”, “Venus”, “Earth”, “Mars”, “Jupiter”, “Saturn”, “Uranus”, “Neptune”, “Pluto”) println(planets) In 2006 Pluto was reclassified as a dwarf planet. So let’s remove Pluto planets.remove(“Pluto”) println(planets)

Network Requests with Volley

Consider Volley a 2.0 version of Android Asynchronous Http Client. A major advantage of Volley over ASyncTask is that you can do multiple requests simultaneously without the overhead of thread management. Gradle Add implementation ‘com.android.volley:volley:1.2.1’ to your module gradle dependencies. Basic Anatomy of a Volley Request val queue = Volley.newRequestQueue(context) val stringRequest = StringRequest( Request.Method.GET,…

Passing data between fragments using SafeArgs

App Gradle plugins { id ‘androidx.navigation.safeargs.kotlin Project Gradle dependencies { classpath “androidx.navigation:navigation-safe-args-gradle-plugin:2.3.5″ nav_graph.xml <fragment> <action android:id=”@+id/action_SecondFragment_to_FirstFragment” app:destination=”@id/FirstFragment” /> <argument android:name=”myArg” app:argType=”integer” /> FirstFragment import com.example.codelabsfirstappwithkotlin.databinding.FragmentFirstBinding val action = FirstFragmentDirections.actionFirstFragmentToSecondFragment(currentCount) findNavController().navigate(action) SecondFragment import androidx.navigation.fragment.navArgs val args: SecondFragmentArgs by navArgs() val count = args.myArg

Play sound on Android

Motivation When writing app you sometimes want to play some sounds e.g. for notifications. In Android this can be accomplished by using the SoundPool class. Resource files Resource files e.g. your wav shall be placed under app\src\main\res\raw Code snippet This is a working example for playing short sound files. If you want to make a…

Log4j2 for Kotlin

Motivation Logging is a common good practice in software engineering. It enables you to monitor applications in production to gather information about crashes and other malfunctions for further analysis. It is the “little brother” of debugging and often a precursor for setting up test cases which can lead to reproducing the bugs on the developers…

Kotlin Scripts with Gradle

If you are using Gradle and you want to run a Kotlin script you have to add the following to your build.gradle file dependencies { implementation “org.jetbrains.kotlin:kotlin-stdlib” implementation ‘org.jetbrains.kotlin:kotlin-script-runtime:1.4.32’ }