1/ Step : Create A New Project and activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#75248C8C"
>
<TableLayout
android:layout_width="368dp"
android:layout_height="495dp"
android:background="@color/white"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_marginLeft="10sp"
android:layout_marginStart="10sp"
android:text="Enter Id"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />
<EditText
android:id="@+id/editId"
android:layout_width="201dp"
android:layout_height="48dp"
android:layout_column="1"
android:layout_marginLeft="50sp"
android:layout_marginStart="50sp"
android:hint="id"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_marginLeft="10sp"
android:layout_marginStart="10sp"
android:text="Enter Name"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />
<EditText
android:id="@+id/editName"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_column="1"
android:layout_marginLeft="50sp"
android:layout_marginStart="50sp"
android:hint="name"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />
</TableRow>
<TableRow android:layout_marginTop="60dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_marginLeft="10sp"
android:layout_marginStart="10sp"
android:text="Your Id"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />
<TextView
android:id="@+id/textViewShowId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_marginLeft="50sp"
android:layout_marginStart="50sp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />
</TableRow>
<TableRow android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_marginLeft="10sp"
android:layout_marginStart="10sp"
android:text="Your Name"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />
<TextView
android:id="@+id/textViewShowName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_marginLeft="50sp"
android:layout_marginStart="50sp"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:orientation="horizontal"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="@+id/save"
android:backgroundTint="#171715"
android:layout_margin="1dp"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:textSize="20dp"
android:text="Save" />
<Button
android:id="@+id/view"
android:layout_margin="1dp"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:backgroundTint="#171715"
android:textSize="20dp"
android:text="View" />
<Button
android:id="@+id/clear"
android:layout_margin="1dp"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:backgroundTint="#171715"
android:textSize="20dp"
android:text="Clear" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
2/Step: MainActivity
package com.alomkaisa.myapplication
import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.graphics.BitmapFactory
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.Button
import android.widget.CalendarView
import android.widget.CheckBox
import android.widget.CheckedTextView
import android.widget.EditText
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.ListView
import android.widget.ProgressBar
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.RatingBar
import android.widget.SearchView
import android.widget.SeekBar
import android.widget.Spinner
import android.widget.Switch
import android.widget.TextView
import android.widget.Toast
import android.widget.ToggleButton
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class MainActivity : AppCompatActivity() {
private val sharedPrefFile = "kotlinsharedpreference"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val inputId = findViewById<EditText>(R.id.editId)
val inputName = findViewById<EditText>(R.id.editName)
val outputId = findViewById<TextView>(R.id.textViewShowId)
val outputName = findViewById<TextView>(R.id.textViewShowName)
val btnSave = findViewById<Button>(R.id.save)
val btnView = findViewById<Button>(R.id.view)
val btnClear = findViewById<Button>(R.id.clear)
val sharedPreferences: SharedPreferences = this.getSharedPreferences(sharedPrefFile,Context.MODE_PRIVATE)
btnSave.setOnClickListener(View.OnClickListener {
val id:Int = Integer.parseInt(inputId.text.toString())
val name:String = inputName.text.toString()
val editor:SharedPreferences.Editor = sharedPreferences.edit()
editor.putInt("id_key",id)
editor.putString("name_key",name)
editor.apply()
editor.commit()
})
btnView.setOnClickListener {
val sharedIdValue = sharedPreferences.getInt("id_key",0)
val sharedNameValue = sharedPreferences.getString("name_key","defaultname")
if(sharedIdValue.equals(0) && sharedNameValue.equals("defaultname")){
outputName.setText("default name: ${sharedNameValue}").toString()
outputId.setText("default id: ${sharedIdValue.toString()}")
}else{
outputName.setText(sharedNameValue).toString()
outputId.setText(sharedIdValue.toString())
}
}
btnClear.setOnClickListener(View.OnClickListener {
val editor = sharedPreferences.edit()
editor.clear()
editor.apply()
outputName.setText("").toString()
outputId.setText("".toString())
})
}
}
Output :