startActivityForResult Depreciated Solution - Android Studio - Compose

How to use ActivityResult | ActivityResultLauncher | rememberLauncherForActivityResult?

Since the startActivityForResult() used to launce the intents like GalleryCameraFile PickShare data, etc is deprecated so now there is a new way to do that ActivityResultLauncher class. In the Jetpack Compose we use rememberLauncherForActivityResult instead of ActivityResultLauncher.

>> Check for Java
>> Check for Kotlin
>> Check for Compose

Code:

build.gradle

Add the following library to the dependencies section of your app-level build.gradle file. This will be used to set the image picked from the Gallery to the Image.

implementation("io.coil-kt:coil-compose:2.6.0")

MainActivity.kt

package com.technifysoft.myapplication

import android.app.Activity
import android.net.Uri
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import coil.compose.rememberAsyncImagePainter
import com.technifysoft.myapplication.ui.theme.MyApplicationTheme

class MainActivityCompose : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            MainUI()
        }
    }
}

@Composable
fun MainUI() {
    val context = LocalContext.current

    // Holds the URI of the selected image
    var imageUri by remember { mutableStateOf<Uri?>(null) }

    // Launcher for picking image from gallery
    val launcher = rememberLauncherForActivityResult(
        contract = ActivityResultContracts.StartActivityForResult()
    ) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            imageUri = result.data?.data
        } else {
            Toast.makeText(context, "Cancelled", Toast.LENGTH_SHORT).show()
        }
    }

    // UI Layout
    Column(
        modifier = Modifier
            .fillMaxSize()
            .padding(24.dp),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        // Image (placeholder or picked)
        Image(
            modifier = Modifier.size(150.dp),
            painter = if (imageUri != null)
                rememberAsyncImagePainter(imageUri)
            else
                painterResource(id = R.drawable.ic_image_black),
            contentDescription = "Profile Image",
            contentScale = ContentScale.Crop
        )

        Spacer(modifier = Modifier.height(16.dp))

        // Button to pick image
        Button(onClick = {
            val intent = android.content.Intent(android.content.Intent.ACTION_PICK).apply {
                type = "image/*"
            }
            launcher.launch(intent)
        }) {
            Text("Pick Image")
        }
    }
}

/**
 * GreetingPreview is a composable function for previewing the MainUI in Android Studio.
 * It is annotated with @Preview to enable live preview.
 *
 */
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    MyApplicationTheme {
        MainUI()
    }
}

Screenshots:

startActivityForResult Depreciated Solution - Android Studio - Compose



Comments

Popular posts from this blog

Picture In Picture | Android Studio | Kotlin

Manage External Storage Permission - Android Studio - Kotlin

How to add AIDL folder | Android Studio