This article covers basic Android utility functions using Kotlin programming language

Following Android Kotlin Utility functions are covered in this article:

  • Check if internet connection is available Kotlin Android code example.
  • Hide soft keyboard Kotlin Android code example.
  • Put data in shared preferences Kotlin Android code example.
  • Get data from shared preference in Android using Kotlin code.
  • Clear shared preferences.
  • Display Notification in Android using Kotlin code.
  • Show alert dialog box in Android using Kotlin code.
package com.developine.kotlinandroidutils
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.app.AlertDialog
import android.app.NotificationManager
import android.content.Context
import android.view.WindowManager
import android.net.ConnectivityManager
import android.provider.Settings
import android.support.v4.app.NotificationCompat
import android.telephony.TelephonyManager
import android.content.DialogInterface
import android.os.Build

 

class MainActivity : AppCompatActivity() {

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
return super.onCreateOptionsMenu(menu)
}

override fun onDestroy() {
super.onDestroy()

}

override fun onPause() {
super.onPause()
}

override fun onResume() {
super.onResume()

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

}

 HIDE SOFT KEYBOARD ANDROID KOTLIN CODE

fun hideSoftKeyboard() {
getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN )
}

CHECK IF NETWORK CONNECTION AVAILABLE ANDROID KOTLIN CODE

fun isNetworkConnected(): Boolean {

 

/* you will also need to add this permission in Manifest.

<uses-permission android:name=”android.permission.ACCESS_NETWORK_STATE” /> */

val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

val activeNetworkInfo = connectivityManager.activeNetworkInfo return activeNetworkInfo != null &&
activeNetworkInfo.isConnected return true

}

SAVE DATA IN SHARED PREFRENCES ANDROID KOTLIN CODE

fun putPrefrences(key: String, value: String) {

 

val sharedPref = this?.getPreferences(Context.MODE_PRIVATE) ?: return

with(sharedPref.edit())
{
putString(key, value) commit() }
}

// READ DATA FROM SHARED PREFRENCES ANDROID KOTLIN CODE

fun getPrefrences(key: String): String {
val sharedPref = this?.getPreferences(Context.MODE_PRIVATE) ?: return
val defaultValue = “no record found”
val highScore = sharedPref.getString(key, defaultValue) return highScore }

// CLEAR APPLICATION SHARED PREFRENCES ANDROID KOTLIN CODE

fun clearPrefrences() {
val sharedPref = this?.getPreferences(Context.MODE_PRIVATE) ?: return
sharedPref.edit().clear().commit()

}

CREATE NOTIFICATION ANDROID KOTLIN CODE

fun displayNotification() {

 

val mBuilder = NotificationCompat.Builder(this, “”) .setSmallIcon(R.drawable.notification_icon) .setContentTitle(“Notification title”) .setContentText(“Notification Content”) .setPriority(NotificationCompat.PRIORITY_DEFAULT)

val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

mNotificationManager.notify(1, mBuilder.build())

}

GET DEVICE UNIQUE ID ANDROID KOTLIN CODE

fun getDeviceUniqueID() {

 

var androidID = Settings.Secure.getString(this.contentResolver, Settings.Secure.ANDROID_ID)

}

 

// GET DEVICE IMEI NUMBER ANDROID KOTLIN CODE

 fun getDeviceIMEI() {

 

/* * You also need to add below permission in Manifest file. 

<uses-permission android:name=”android.permission.READ_PHONE_STATE”/> * */

val telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager telephonyManager.deviceId

}

SHOW ALERT DIALOG ANDROID KOTLIN CODE

  • fun showAlertDialog() {

    val builder: AlertDialog.Builder

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)

    { builder = AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert) }

    else {

    builder = AlertDialog.Builder(this)

    }

    builder.setTitle(“Delete entry”) .setMessage(“Are you sure you want to delete this entry?”)

    .setPositiveButton(android.R.string.yes, DialogInterface.OnClickListener { dialog, which -> // continue with delete }) .setNegativeButton(android.R.string.no, DialogInterface.OnClickListener {

    dialog, which -> // do nothing }) .

    setIcon(android.R.drawable.ic_dialog_alert) .show()

    }

    }

Recommended Reading:

Contact Us