- Details
- Written by: Stanko Milosev
- Category: Android
- Hits: 192
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) } private val galleryLauncher = this.registerForActivityResult(ActivityResultContracts.GetMultipleContents()) { images -> for (image in images) { println(image.path) } } private fun openGallery() { galleryLauncher.launch("image/*") } fun onButtonClick(view: View) { openGallery(); } }
- Details
- Written by: Stanko Milosev
- Category: Android
- Hits: 334
binding.name.text = viewModel.name binding.button.setOnClickListener { viewModel.userClicked() }Data Binding example
<Button android:id="@+id/btnMyButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save Settings" android:onClick="@{() -> viewModel.onButtonClick()}" />According to Android for Developers both can be used:
Comparison with data binding View binding and data binding both generate binding classes that you can use to reference views directly. However, view binding is intended to handle simpler use cases and provides the following benefits over data binding: Faster compilation: view binding requires no annotation processing, so compile times are faster. Ease of use: view binding doesn't require specially tagged XML layout files, so it's faster to adopt in your apps. Once you enable view binding in a module, it applies to all of that module's layouts automatically. On the other hand, view binding has the following limitations compared to data binding: View binding doesn't support layout variables or layout expressions, so it can't be used to declare dynamic UI content straight from XML layout files. View binding doesn't support two-way data binding. Because of these considerations, in some cases it's best to use both view binding and data binding in a project. You can use data binding in layouts that require advanced features and use view binding in layouts that don't.
- Details
- Written by: Stanko Milosev
- Category: Android
- Hits: 317
val btnSettings: Button = findViewById<View>(R.id.btnSettings) as Button btnSettings.setOnClickListener { val intent = Intent(this, SettingsActivity::class.java) //startActivity(intent) settingsActivityResultLauncher.launch(intent) } private val settingsActivityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> if (result.resultCode == Activity.RESULT_OK) { val data: Intent? = result.data val editTextFileNameValue = data?.getStringExtra("editTextFileName") fileName = editTextFileNameValue val editTextFolderNameValue = data?.getStringExtra("editTextFolderName") folderName = editTextFolderNameValue } }
- Details
- Written by: Stanko Milosev
- Category: Android
- Hits: 321
buildFeatures { dataBinding = true }Sync gradle files. Add Kotlin class "ButtonViewModel":
package com.milosev.mymvvmexample import androidx.lifecycle.ViewModel class ButtonViewModel : ViewModel() { fun onButtonClick() { println("Hello, Kotlin!") } }In activity_main.xml add layout and data so it looks like:
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> <data> <variable name="viewModel" type="com.milosev.mymvvmexample.ButtonViewModel" /> </data> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="@{() -> viewModel.onButtonClick()}" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> </layout>MainActivity.kt:
package com.milosev.mymvvmexample import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import androidx.databinding.DataBindingUtil import androidx.lifecycle.ViewModelProvider import com.milosev.mymvvmexample.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main) val viewModel = ViewModelProvider(this)[ButtonViewModel::class.java] binding.viewModel = viewModel binding.lifecycleOwner = this } }Example download from here.