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, url,
    { response ->
        Log.e(TAG, "success")
    },
    {
        Log.e(TAG, "error")
    })
queue.add(stringRequest)

Play sound on Android

Resource files shall be placed under app\src\main\res\raw

import android.media.AudioManager
import android.media.SoundPool
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
    private var mCSoundOne: Int = 0
    private lateinit var mSoundPool: SoundPool
    private var mLoaded = false

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

        mSoundPool = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Log.d("SoundPoolTest", "Building Soundpool")
            SoundPool.Builder().setMaxStreams(1).build()
        } else {
            SoundPool(1, AudioManager.STREAM_MUSIC, 1)
        }
        mSoundPool.setOnLoadCompleteListener(SoundPool.OnLoadCompleteListener { soundPool, sampleId, status ->
            Log.d("SoundPoolTest", "Load Completed")
            mLoaded = true
        })
        Log.d("SoundPoolTest", "Loading wav")
        mCSoundOne = mSoundPool.load(applicationContext, R.raw.eins, 1)

    }

    fun play_one(view: View?) {
        Log.d("SoundPoolTest", "Button Clicked")
        if (mLoaded) {
            Log.d("SoundPoolTest", "Playing wav")
            mSoundPool.play(mCSoundOne, 1.0f, 1.0f, 0, 0, 1.0f)
        }
    }
}

Log4j2 PatternLayout Cheatsheet

In Log4j2 for Kotlin I’ve showed you how to configure a basic logger with log4j2.

If you use a PatternLayout like this

appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %m%n

you can customize the appearance of the output.

eg.

appender.console.layout.pattern = %d{yyyy-mm-dd-HH:mm:ss.SSS} %-4p %C{1}.%M [%t] - %m%n%ex

Parameter

Parameter Meaning
%m The log message
%n line break
%d timestamp
%p priority
%t thread
%C class

Timestamps

 

%d{yyyy-mm-dd-HH:mm:ss.SSS}
2021-50-27-10:50:32.701
Parameter Meaning
yyyy year in four digits
MM

MMM

month in two digits

Name of month three characters wide

dd day
HH hour
mm minutes
ss seconds
SSS milliseconds