Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

import androidx.appcompat.app.

AppCompatActivity
import android.os.Bundle
import android.content.Context
import android.content.SharedPreferences
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val id = findViewById<EditText>(R.id.editId)
val name = findViewById<EditText>(R.id.editName)
val resultId = findViewById<TextView>(R.id.textViewShowId)
val resultName = findViewById<TextView>(R.id.textViewShowName)

val Save = findViewById<Button>(R.id.save)


val View = findViewById<Button>(R.id.view)
val Clear = findViewById<Button>(R.id.clear)

val sharedPreferences: SharedPreferences =


this.getSharedPreferences("LOL",Context.MODE_PRIVATE)
Save.setOnClickListener(View.OnClickListener {
val id:Int = Integer.parseInt(Id.text.toString())
val name:String = Name.text.toString()
val editor = sharedPreferences.edit()
editor.putInt("id",id)
editor.putString("name",name)
editor.apply()
editor.commit()
})
View.setOnClickListener {
val sharedIdValue = sharedPreferences.getInt("id", 0)
val sharedNameValue = sharedPreferences.getString("name", "No Data")

ResultName.setText(sharedNameValue).toString()
ResultId.setText(sharedIdValue.toString())

}
Clear.setOnClickListener(View.OnClickListener {
val editor = sharedPreferences.edit()
[11/27, 9:26 PM] Noman Ali: editor.clear()
editor.apply()
ResultName.setText("").toString()
ResultId.setText("".toString())
})
}
}
[11/27, 9:27 PM] Noman Ali: package com.example.assignment3

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.button_id)
val text = findViewById<EditText>(R.id.text_id)

button.setOnClickListener {
val Message = send_text.text.toString()
val intent = Intent(applicationContext, LOL::class.java)
intent.putExtra("WelcomeMessage", Message)
startActivity(intent)
}
}
}

You might also like