Found 417 Articles for Kotlin

Throw Custom Exception in Kotlin

Soumak De
Updated on 27-Oct-2021 07:56:24

869 Views

Exception is an important aspect of any programming language. It prevents our code from generating incorrect output at runtime.The concept of exception in Kotlin is very much same as it is in Java. All the exceptions in Kotlin are the descendants of Throwable class. In Kotlin, developers do have the privilege to create their own custom Exception.Custom Exceptions are a part of unchecked exceptions which means they will be thrown at the runtime.Before getting into custom exceptions in Kotlin, let's take a look checked and unchecked exceptions.Checked ExceptionsChecked exceptions are those which are checked at the compile time. In the ... Read More

How to iterate over a Hashmap in Kotlin?

Soumak De
Updated on 27-Oct-2021 06:22:01

2K+ Views

A Map is a collection where data is stored as a key-value pair and the corresponding key has to be unique. A HashMap is a collection class based upon MutableMap interface and it does that by implementing MutableMap interface of HashTable. Kotlin provides four types of constructor to define and manipulate HashMap.HashMap() – It is the default constructor which helps us create an empty HashMap.HashMap(initialCapacity: Int, loadFactor: Float = 0f) – It helps us create a HashMap using initial capacity; if it is not provided, then it will be ignored and it will act as default HashMap().HashMap(initialCapacity: Int) – It ... Read More

How to change a TextView's style at runtime in Android using Kotlin?

Azhar
Updated on 30-Nov-2020 05:17:49

2K+ Views

This example demonstrates how to change a TextView's style at runtime in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code in res/values/styles.xml           bold|italic       #FFFFFF               normal       #C0C0C0     Step 4 − Add the following code in res/values/colors.xml    @android:color/holo_green_light    @android:color/holo_red_dark Step ... Read More

How to send a notification from a service in Android using Kotlin?

Azhar
Updated on 30-Nov-2020 05:13:41

2K+ Views

This example demonstrates how to send a notification from a service in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.             Step 3 − Add the following code to src/MainActivity.ktimport android.content.Intent import android.os.Bundle import android.view.View import android.widget.EditText import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat class MainActivity : AppCompatActivity() {    lateinit var editText: EditText    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)       setContentView(R.layout.activity_main)   ... Read More

How does one use glide to download an image into a bitmap using Kotlin?

Azhar
Updated on 30-Nov-2020 05:08:08

615 Views

This example demonstrates how to use glide to download an image into a bitmap using KotlinStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.ktimport android.graphics.Bitmap import android.graphics.drawable.Drawable import android.os.Bundle import android.widget.ImageView import androidx.annotation.Nullable import androidx.appcompat.app.AppCompatActivity import com.bumptech.glide.Glide import com.bumptech.glide.request.target.CustomTarget import com.bumptech.glide.request.transition.Transition; class MainActivity : AppCompatActivity() {    lateinit var imageView: ImageView    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)     ... Read More

How to disable GridView scrolling in Android using Kotlin?

Azhar
Updated on 30-Nov-2020 04:49:17

258 Views

This example demonstrates how to disable GridView scrolling in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.ktimport android.content.Context import android.os.Bundle import android.view.MotionEvent import android.view.View import android.view.ViewGroup import android.widget.* import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    private lateinit var gridView: GridView       var imageIDs = arrayOf(          R.drawable.ronaldo,          R.drawable.andre, ... Read More

How to use Android Picasso library to download images using Kotlin?

Azhar
Updated on 30-Nov-2020 04:41:35

433 Views

This example demonstrates how to use the Android Picasso library to download images using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Add the following dependency to the build gradle (Module: app)implementation 'com.squareup.picasso:picasso:2.4.0'Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.ktimport android.os.Bundle import android.widget.Button import android.widget.ImageView import androidx.appcompat.app.AppCompatActivity import com.squareup.picasso.Picasso class MainActivity : AppCompatActivity() {    lateinit var imageView: ImageView    lateinit var btnDownload: Button    override fun onCreate(savedInstanceState: ... Read More

How to create a swipe refresh layout in Android using Kotlin?

Azhar
Updated on 30-Nov-2020 04:33:51

3K+ Views

This example demonstrates how to create a swipe refresh layout in Android using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.             Step 3 − Add the following code to src/MainActivity.ktimport androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.os.Handler import android.widget.TextView import androidx.swiperefreshlayout.widget.SwipeRefreshLayout class MainActivity : AppCompatActivity() {    lateinit var swipeRefreshLayout: SwipeRefreshLayout    lateinit var textView: TextView    var number: Int = 0    override fun onCreate(savedInstanceState: Bundle?) {   ... Read More

How to close all Android activities at once using Kotlin?

Azhar
Updated on 30-Nov-2020 04:28:50

790 Views

This example demonstrates how to close all Android activities at once using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.             Step 3 − Add the following code to src/MainActivity.ktpackage com.example.q28 import android.content.Intent import android.os.Bundle import android.widget.Button import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() {    lateinit var button: Button    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)       setContentView(R.layout.activity_main)       title = ... Read More

How to get the Android Emulator's IP address using Kotlin?

Azhar
Updated on 30-Nov-2020 04:20:53

260 Views

This example demonstrates how to get the Android Emulator's IP address using Kotlin.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.             Step 3 − Add the following code to src/MainActivity.ktimport android.net.wifi.WifiManager import android.os.Bundle import android.widget.Button import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import android.text.format.Formatter class MainActivity : AppCompatActivity() {    lateinit var button: Button    lateinit var textView: TextView    override fun onCreate(savedInstanceState: Bundle?) {       super.onCreate(savedInstanceState)     ... Read More

Advertisements