Micro blog about Answer to the Ultimate Question of Life, the Universe, and Everything.
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home
  3. Android

Select images from local storage

Details
Written by: Stanko Milosev
Category: Android
Published: 17 February 2024
Last Updated: 17 February 2024
Hits: 442
My example how to select images from local storage:
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();
    }
}

Binding

Details
Written by: Stanko Milosev
Category: Android
Published: 10 August 2023
Last Updated: 10 August 2023
Hits: 616
  • kotlin
There are two types of binding in Android: View binding and Data Binding.

View binding example:

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.

Get data from Activity

Details
Written by: Stanko Milosev
Category: Android
Published: 09 August 2023
Last Updated: 09 August 2023
Hits: 555
  • kotlin
One example how to receive data from Activity:
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
		}
	}

Simple MVVM example in Kotlin

Details
Written by: Stanko Milosev
Category: Android
Published: 06 August 2023
Last Updated: 06 August 2023
Hits: 560
  • kotlin
First start Empty Views Activity:

In \app\build.gradle.kts I have added:

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.
  1. Mock context
  2. MockWebServer example
  3. Example of making HTTP Post and Get request from Android Kotlin to Web Api .NET Core
  4. Creating never ending application v2

Page 2 of 11

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10