supportActionBar?.hide()
load()
val db = Room.databaseBuilder(
applicationContext,
AppDatabase::class.java, "$TABLE_NAME"
).allowMainThreadQueries().build()
val userDao = db.userDao()
binding.apply {
// roomga image qoshish uchun tanlab olish
imageviewlogin.setOnClickListener {
openFileChooser()
}
btnlogin.setOnClickListener {
progressbarlogin.visibility = View.VISIBLE
if (edittextfirstname.text.isEmpty() || edittextlastname.text.isEmpty()){
edittextfirstname.setError("Error")
edittextlastname.setError("Error")
progressbarlogin.visibility = View.GONE
}else {
val user = User(0, edittextfirstname.text.toString(),edittextlastname.text.toString(),bitmapconverttoBytArray(uriconverttoBitmap(imageuri!!)))
userDao.insertUsers(user)
progressbarlogin.visibility = View.GONE
save("${edittextlastname.text} ${edittextfirstname.text}")
startActivity(Intent(this@MainActivity,MainActivity2::class.java))
}
}
}
}
// tanlanadigan faylni chaqrish
fun openFileChooser() {
getContent.launch("image/*")
}
// imagenin qayerdan tanlash keraklligini ko'rsatadigan fun
val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
binding.imageviewlogin.setImageURI(uri)
imageuri = uri
}
fun uriconverttoBitmap(uri: Uri): Bitmap {
val bitmap = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
ImageDecoder.decodeBitmap(ImageDecoder.createSource(this@MainActivity.contentResolver, uri))
} else {
MediaStore.Images.Media.getBitmap(this@MainActivity.contentResolver, uri)
}
return bitmap
}
fun bitmapconverttoBytArray(bitmap: Bitmap): ByteArray {
val outputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
return outputStream.toByteArray()
}
fun byteArrayconverttoBitmap(byteArray: ByteArray): Bitmap {
return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
}
fun save(text:String){
val editor = getSharedPreferences("mrj", MODE_PRIVATE).edit() as SharedPreferences.Editor
editor.putString("dev",text)
editor.commit()
}
fun load() {
val sharedPreferences = getSharedPreferences("mrj", MODE_PRIVATE)
savedtext = sharedPreferences.getString("dev", null)
if (savedtext != null) {
val intent = Intent(this@MainActivity, MainActivity2::class.java)
intent.putExtra("username", savedtext)
startActivity(intent)
}
}
}
package com.example.enlishforkidsbeginner
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.enlishforkidsbeginner.databinding.ActivityMain2Binding
import com.example.enlishforkidsbeginner.databinding.ActivityMainBinding
class MainActivity2 : AppCompatActivity() {
lateinit var binding: ActivityMain2Binding
override fun onCreate(savedInstanceState: Bundle?) {
binding = ActivityMain2Binding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
setContentView(binding.root)
intent = intent
var username: String? = intent.getStringExtra("username")
binding.textviewusername.text = username.toString()
binding.apply {
linearlayaoutlearning.setOnClickListener {
startActivity(Intent(this@MainActivity2,MainActivity3::class.java))
Dostları ilə paylaş: |