kotlin code snippets

Native Video Ad Sample xml file
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.ads.nativead.NativeAdView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/tv_video_tile_native_ad_attribution_small"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#F19938"
            android:text="Ad"
            android:textColor="#FFFFFF"
            android:textSize="12sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <com.google.android.gms.ads.nativead.MediaView
                android:id="@+id/native_ad_media"
                android:layout_width="match_parent"
                android:layout_margin="16dp"
                android:layout_height="200dp">
            </com.google.android.gms.ads.nativead.MediaView>

            <LinearLayout
                android:layout_width="match_parent"
                android:orientation="horizontal"
                android:paddingLeft="16dp"
                android:paddingRight="16dp"
                android:layout_height="60dp">
                <ImageView
                    android:id="@+id/native_ad_icon"
                    android:layout_width="60dp"
                    android:layout_height="60dp"></ImageView>

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <TextView
                        android:id="@+id/native_ad_headline"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:ellipsize="end"
                        android:lines="1"
                        android:maxLines="1"
                        android:textColor="#000000"
                        android:textSize="16sp"
                        tools:text="Headline" />

                    <TextView
                        android:id="@+id/native_ad_body"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:ellipsize="end"
                        android:lines="1"
                        android:maxLines="1"
                        android:textColor="#828282"
                        android:textSize="14sp"
                        tools:text="body" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>

    </FrameLayout>
</com.google.android.gms.ads.nativead.NativeAdView>
Use Ternary Operator in Kotlin
val v = 9

val result = if (v > 10) "Greater than 10" else "less than 10"

println(result)
Methods to convert a String to Long in Kotlin
// define a string
val strVal: String = "123"

// convert to long using toLong()
val result: Long = strVal.toLong()

// print the result 
println(result)
String to Int conversion in Kotlin
fun main() {
    // define a String
    val myStr: String = "100"
    
    // convert to int
    val num: Int = myStr.toInt()
    
    // print the result
    println(num)
}
Check if item exists in an Array Kotlin
fun checkItem(arr: Array<String>, item: String): Boolean {
    if (arr.indexOf(item) != -1) {
        return true
    } else {
        return false
    }
}

fun main() {
    // create an array
    val students = arrayOf<String>("Jacob", "Roy", "Yamini", "Neha")
    
    // Check if Roy is in the array
    val check_roy: Boolean = checkItem(students, "Roy")
    println("Roy exists: " + check_roy)
    
    // Check if Sohan is in the array
    val check_sohan: Boolean = checkItem(students, "Sohan")
    println("Sohan exists: " + check_sohan)
}
Get the index of an item in an Array Kotlin
fun main() {
    val arr = arrayOf<String>("Rusty", "Amit", "Martin", "Luther", "Joy")
    
    val index = arr.indexOf("Luther")
    
    println(index)
}
Mutable List in Kotlin
val lst = mutableListOf<Int>(10, 20, 30, 40, 50)
Remove an item from Array in Kotlin
fun removeElement(arr: Array<String>, itemIndex: Int): Array<String> {
    val arrList = arr.toMutableList()
    
    arrList.removeAt(itemIndex)
    
    return arrList.toTypedArray()
}

fun main() {
	var fruits = arrayOf<String>("Apple", "Banana", "Orange", "Papaya")
    
    // Remove second element from array
    fruits = removeElement(fruits, 2)
    
    println(fruits.contentToString())
}
Add new items to Array in Kotlin
fun main() {
	var names = arrayOf<String>("James", "Walter", "Roy", "Salve")
	
    val namesList = names.toMutableList()
    namesList.add("Rick")
    namesList.add("Carol")
    names = namesList.toTypedArray()
    
    println(names.contentToString())
}
Create an empty Array in Kotlin
// using arrayOf() function
val arr = arrayOf<String>()

// or
val arr: Array<String> = arrayOf()
Get Length of an Array in Kotlin
val employees = arrayOf("John", "Jacob", "Martha", "Neel")
    
val result = employees.size

println( result )
Create an Array in Kotlin
fun main() {
    // array that contains any type of value - implicit
	val arr = arrayOf(10, 20, 30, 40, 50, "Jack")
    
    println(arr[1]) // -> 20
    println(arr[5]) // -> "Jack"
    
    // array that contains integer type values only - explicit
    val nums = arrayOf<Int>(3, 2, 9, 4)
    println(nums[2]) // -> 9
}