Simple SharedPreference Functions – Kotlin

Android provides many ways of storing data of an application. One of these ways is called Shared Preferences. Shared Preferences allow you to save and retrieve data in the form of key, value pair.

Here I share a simple custom SharedPreferences.kt class that can be used for every Android application if needed.

import android.content.Context
import android.content.SharedPreferences
import com.securepreferences.SecurePreferences

/**
 * Created by Sayandh-Kolincodes on 10/05/2018.
 */

class SharedPreference(val context: Context) {
    private val PREFS_NAME = "your_app_preference_name"

    private val sharedPref: SharedPreferences =
        context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)

    fun save(KEY_NAME: String, text: String) {

        val editor: SharedPreferences.Editor = sharedPref.edit()

        editor.putString(KEY_NAME, text)

        editor.commit()
    }

    fun save(KEY_NAME: String, value: Int) {
        val editor: SharedPreferences.Editor = sharedPref.edit()

        editor.putInt(KEY_NAME, value)

        editor.commit()
    }

    fun save(KEY_NAME: String, status: Boolean) {

        val editor: SharedPreferences.Editor = sharedPref.edit()

        editor.putBoolean(KEY_NAME, status)

        editor.commit()
    }

    fun getValueString(KEY_NAME: String): String? {

        return sharedPref.getString(KEY_NAME, null)


    }

    fun getValueInt(KEY_NAME: String): Int {

        return sharedPref.getInt(KEY_NAME, 0)
    }

    fun getValueBoolien(KEY_NAME: String, defaultValue: Boolean): Boolean {

        return sharedPref.getBoolean(KEY_NAME, defaultValue)

    }

    fun clearSharedPreference() {

        val editor: SharedPreferences.Editor = sharedPref.edit()

        //sharedPref = PreferenceManager.getDefaultSharedPreferences(context);

        editor.clear()
        editor.commit()
    }

    fun removeValue(KEY_NAME: String) {

        val editor: SharedPreferences.Editor = sharedPref.edit()

        editor.remove(KEY_NAME)
        editor.commit()
    }

}

You can find the Github repo for this class here

Edittext Text ChangeListener Kotlin

Action whenever the text is changed in the EditText View.

Application to implement a listener
TextWatcher object, for EditText to trigger an action on text change.

Create a new Project in Kotlin

Message can display outside of our application normal UI

1 Open Android Studio.
2 Go to File => New => New Project. Write application name . Then, check Include Kotlin Support and click next button.
3Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4 Then, select Empty Activity => click next => click finish.
5 You will get a newly created project successfully if you have followed steps properly.

Use the below code to Implement a Edittext with Kotlin

MainActivity.kt

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

        editTextSample.addTextChangedListener(object : TextWatcher {

            override fun afterTextChanged(s: Editable) {}

            override fun beforeTextChanged(s: CharSequence, start: Int,
                                           count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence, start: Int,
                                       before: Int, count: Int) {
                tvSample.setText("Text is : "+s)
            }
        })
    }
}


main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
      tools:context=".MainActivity">

    <LinearLayout
            android:layout_marginTop="20dp"
            android:orientation="vertical"
            android:padding="10sp"
            android:gravity="center_horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            android:layout_height="wrap_content" />

        <EditText
                android:id="@+id/editTextSample"
                android:textSize="20sp"
                android:layout_marginTop="50dp"
                android:hint="Enter Text ..."
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>



        <TextView
                android:id="@+id/tvSample"
                android:layout_marginTop="20dp"
                android:textColor="@color/colorPrimary"
                android:textStyle="bold"
                android:textSize="16dp"
                android:layout_width="wrap_content"
                  android:layout_height="wrap_content"/>

    </LinearLayout>

</android.support.constraint.ConstraintLayout>