"requestCode" is used in order for the app to differentiate the request that is given. We need to create an if statement in the “onActivityResult” function in order to pass the data to the main activity.
Solid video even if though the API has changed a bit and it alters what we will do this video still covers concepts and thought process and it is becoming fun trying to figure out how to adjust the code to replicate the results.
2023 UPDATE: This is what the code should look like using non-depreciated methods: val getContent = registerForActivityResult(GetContent()) { uri: Uri? -> // Handle the returned Uri binding.ivPhoto.setImageURI(uri) } binding.btnTakePhoto.setOnClickListener { getContent.launch("image/*") } I am still learning, so there may be a better way to implement this. This is from my research and what worked for me! * make sure to import "androidx.activity.result.contract.ActivityResultContracts.GetContent" and "android.net.Uri" *
Nicely done, thank you very much. I have always loved the implicit intent system, but I was a little cloudy on how to work with it but not anymore. I feel like I also understand a larger concept here as well so many thanks!
Hi @PhilippLackner ! Thanks for this video. I have a problem, though. startActivityForResult has been deprecated in favor of registerForActivityResult. I'm finding it difficult to transition. Can you please update the video? Thank you!!!
First we call " registerForActivityResult()" which then returns an abstract class called "ActivityResultLauncher" which we needs to implement or override inside there are 3 abstract methods that we need to implement/override 1. getContract() //return ActivityResultContract 2. launch(input: I!, options: ActivityOptionsCompat?) // return Unit 3. unregister() //return @MainThread Unit
For Android 11, startActivityForResult is deprecated, looking at samples for ActivityResultRegistry but can't seem to get it working for this tutorial. Any help would be appreciated.
It was so good tutorial, I have a question, in case I need to do the same thing but instead uploading the image to Image View I would to set the activity background ?
hello Philipp great content, but when i use this code there is deprecation, can you show us how to do it with the update version instead of the deprecated, thank you for your help and support
registerForActivityResult gets it done. This is the whole code: val btnChoseImage: Button = findViewById(R.id.btnChooseImage) val ivImage: ImageView = findViewById(R.id.ivImage) val getImage = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri-> ivImage.setImageURI(uri) } btnChoseImage.setOnClickListener { getImage.launch("image/*") }
how can i go outside of any callback function in general in kotlin for example if the callback that i am in is returning null to one of the parameters Because in this context.. i am trying to implement onActivityResult() callback function which is part of an abstract class ActivityResultLauncher (Because this is the new way i guess) so the function now ask me what i want to do with the uri:Uri? object (i hope you know what i mean here) i want to check if it is null i want it to go out of the callback function i tried to say " if(uri==null) return " but did not work and i try "if(uri==null) return@onActivityResult" because this is recommended by the IDE.. is this the correct way to do?
Great content bro Have a question When you click the image in the image Activity how does the intent know it should finish/destroy the image Activity and pass the image clicked to the Main activity ?
@Philipp Lackner ohh Now I get it Thanks a lot for your videos really helpful to a lot of people going through their Android development journey !! Really grateful
Hello. If I try to implement that button.onClickListener in a function inside a viewmodel of a fragment, I get an error on startActivityForResult. The first parameter it needs is an activity and I don't know how to pass it...
how can i go outside of any callback function in general in kotlin for example if the callback that i am in is returning null to one of the parameters Because in this context.. i am trying to implement onActivityResult() callback function which is part of an abstract class ActivityResultLauncher (Because this is the new way i guess) so the function now ask me what i want to do with the uri:Uri? object (i hope you know what i mean here) i want to check if it is null i want it to go out of the callback function i tried to say " if(uri==null) return " but did not work and i try "if(uri==null) return@onActivityResult" because this is recommended by the IDE.. is this the correct way to do?
this worked for me class MainActivity : AppCompatActivity() { lateinit var btnTakePhoto:Button lateinit var ivPhoto:ImageView val choosePicure=Intent().apply { action=Intent.ACTION_GET_CONTENT type="image/*" } val resultContract=registerForActivityResult(ActivityResultContracts.StartActivityForResult()){ result: ActivityResult?-> if(result?.resultCode==Activity.RESULT_OK){ Log.v("xxx","it was ok") val uri=result.data?.data ivPhoto.setImageURI(uri) } else{ Log.v("xxx","it was not ok")} } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) btnTakePhoto=findViewById(R.id.btnTakePhoto) ivPhoto=findViewById(R.id.ivPhoto) btnTakePhoto.setOnClickListener { resultContract.launch(choosePicure) } } }
This code is not working in Bumblebee Android as ' startActivityForResult' is deprecated. Can you advise on an alternate code in place of the present code so that it runs in Bumblebee also?
you can use ActivityResultContracts, to know how to use check out the google guide which provides a similar example used in this video: developer.android.com/training/basics/intents/result#launch
startActivityForResult() is deprecated instead you should create an ActivityResultLauncher object using the function registerForActivityResult() with a lambda function that handles the results come from launched activity. Creating a resultLauncher : private val resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {result-> if(result.resultCode == Activity.RESULT_OK){ val data = result.data?.data binding.[YOUR IMAGE VIEW].setImageURI(data) } }
Alright yall, I came up with a solution (as of 2023): val image = findViewById(R.id.[your imageview name]) val content = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> if (result.resultCode == Activity.RESULT_OK) { val data = result.data?.data image.setImageURI(data) } } findViewById(R.id.[your button name]).setOnClickListener { val myIntent = Intent(Intent.ACTION_GET_CONTENT).also { it.type = "image/*" content.launch(it) } }
As the code for the intent is now deprecated I was able to do it like this: var ivImage = findViewById(R.id.ivImage) val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? -> ivImage.setImageURI(uri) } val btnGalery = findViewById(R.id.btnGalery) btnGalery.setOnClickListener { getContent.launch("image/*") }
"requestCode" is used in order for the app to differentiate the request that is given.
We need to create an if statement in the “onActivityResult” function in order to pass the data to the main activity.
Solid video even if though the API has changed a bit and it alters what we will do this video still covers concepts and thought process and it is becoming fun trying to figure out how to adjust the code to replicate the results.
2023 UPDATE:
This is what the code should look like using non-depreciated methods:
val getContent =
registerForActivityResult(GetContent()) { uri: Uri? ->
// Handle the returned Uri
binding.ivPhoto.setImageURI(uri)
}
binding.btnTakePhoto.setOnClickListener {
getContent.launch("image/*")
}
I am still learning, so there may be a better way to implement this. This is from my research and what worked for me!
* make sure to import "androidx.activity.result.contract.ActivityResultContracts.GetContent" and "android.net.Uri" *
You can update this segment or add a new segment that uses ActivityResultContracts
Man your content is always good. You deserve more subs 😊
Thanks ❤️
I'm a new sub 😎
I love your tutorials! There are some error caused by deprecated codes. Pls recreate this tutorial with latest code.
Nicely done, thank you very much.
I have always loved the implicit intent system, but I was a little cloudy on how to work with it but not anymore.
I feel like I also understand a larger concept here as well so many thanks!
hey I am getting this error can you please help
'startActivityForResult(Intent!, Int): Unit' is deprecated. Deprecated in Java getting this error
Hi @PhilippLackner ! Thanks for this video. I have a problem, though. startActivityForResult has been deprecated in favor of registerForActivityResult. I'm finding it difficult to transition. Can you please update the video? Thank you!!!
Thank you, your teaching style is amazing
You are a very good explainer.
Very good I want this , this is really very helpful for me
You are a great help, @Philipp.
First we call
" registerForActivityResult()" which then returns an abstract class called
"ActivityResultLauncher" which we needs to implement or override
inside there are 3 abstract methods that we need to implement/override
1. getContract() //return ActivityResultContract
2. launch(input: I!, options: ActivityOptionsCompat?) // return Unit
3. unregister() //return @MainThread Unit
For Android 11, startActivityForResult is deprecated, looking at samples for ActivityResultRegistry but can't seem to get it working for this tutorial.
Any help would be appreciated.
This seems to work:
package com.tutorials.implicitintent
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import com.tutorials.implicitintent.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
lateinit var bindingMain: ActivityMainBinding
var getContent = registerForActivityResult(
ActivityResultContracts.GetContent()) {
bindingMain.ivPhoto.setImageURI(it)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
bindingMain = ActivityMainBinding.inflate(layoutInflater)
setContentView(bindingMain.root)
bindingMain.btTakePhoto.setOnClickListener {
Intent(Intent.ACTION_GET_CONTENT).also { it ->
it.type = "image/*"
getContent.launch(it.type)
}
}
}
}
Thank You so much... This Worked for me😊
What worked?
It was so good tutorial, I have a question, in case I need to do the same thing but instead uploading the image to Image View I would to set the activity background ?
hello Philipp great content, but when i use this code there is deprecation, can you show us how to do it with the update version instead of the deprecated, thank you for your help and support
registerForActivityResult gets it done. This is the whole code:
val btnChoseImage: Button = findViewById(R.id.btnChooseImage)
val ivImage: ImageView = findViewById(R.id.ivImage)
val getImage = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri->
ivImage.setImageURI(uri)
}
btnChoseImage.setOnClickListener {
getImage.launch("image/*")
}
@@borispuhacin7509 Thank you!
@@borispuhacin7509 bro does not work for me
uri is actually a nullable Uri object
how can i go outside of any callback function in general in kotlin
for example if the callback that i am in is returning null to one of the parameters
Because in this context..
i am trying to implement
onActivityResult() callback function which is part of an abstract class ActivityResultLauncher
(Because this is the new way i guess)
so the function now ask me what i want to do with the uri:Uri? object (i hope you know what i mean here)
i want to check if it is null i want it to go out of the callback function
i tried to say " if(uri==null) return "
but did not work
and i try
"if(uri==null) return@onActivityResult"
because this is recommended by the IDE..
is this the correct way to do?
Thank you so much. Can you please change to the new function because the two fun are deprecated
the explanation is very good but you can record videos with face cam we connect much better
Could you make another Implicit Intent tutorial because the android studio does not recommend this method anymore.
Awesome, man 👍👍 that is so helpful information
Thanks a lot!
@5:29 Bro activityForResult() is deprecated what should i do?
Hi sir, you took an example of opening a pdf, can you tell me more about it, how to implement that via implicit intent for PDF reader.
awesome dude !
Thank you❤️
That was great!
Have a question , whenever I click the button the image doesn't show on the app. ANY HELP
@Lahty V. you found any solution?
In onActivityResult()
if(requestCode ==0){
....
}
@@vikasbhadoriya1466 yeah I found typo on that line already that was creating the issue
@@betaaccount7450 how far you come up with android development?
@@vikasbhadoriya1466 actually I completed this series and made an memes app with some libraries and API
Nice video , I have a question. Read image gallery Should we use permission ?
Yes, you need to put in manifest
Can you please create videos on Retrofit or some Api handling stuffs.. with get Post req
Tomorrow my next series starts about a clean architecture news app. We will use Retrofit with coroutines there
@@PhilippLackner ♥️
Great content bro
Have a question
When you click the image in the image Activity how does the intent know it should finish/destroy the image Activity and pass the image clicked to the Main activity ?
The gallery app is programmed that way. You can declare intent-filters and declare your app as an app that can open images for example
@Philipp Lackner ohh Now I get it Thanks a lot for your videos really helpful to a lot of people going through their Android development journey !! Really grateful
Hello Philipp, ivPhoto is not working for me. I checked again it look like it is missing from the import library or so. Can you suggest anything?
Thanks Sir
Awesome! 👍
Thanks! 👍
Love these
Hello. If I try to implement that button.onClickListener in a function inside a viewmodel of a fragment, I get an error on startActivityForResult. The first parameter it needs is an activity and I don't know how to pass it...
Ohh no, don't use anything in viewmodels that has something to do with views😅😅 do it in the fragment/activity
@@PhilippLackner so I write the function with intent there, thanks :D
Great content thanks
Very welcome
dosn't we need to provide permission for gallery access.
how can i go outside of any callback function in general in kotlin
for example if the callback that i am in is returning null to one of the parameters
Because in this context..
i am trying to implement
onActivityResult() callback function which is part of an abstract class ActivityResultLauncher
(Because this is the new way i guess)
so the function now ask me what i want to do with the uri:Uri? object (i hope you know what i mean here)
i want to check if it is null i want it to go out of the callback function
i tried to say " if(uri==null) return "
but did not work
and i try
"if(uri==null) return@onActivityResult"
because this is recommended by the IDE..
is this the correct way to do?
'startActivityForResult(Intent!, Int): Unit' is deprecated. Deprecated in Java getting this error
this worked for me
class MainActivity : AppCompatActivity() {
lateinit var btnTakePhoto:Button
lateinit var ivPhoto:ImageView
val choosePicure=Intent().apply {
action=Intent.ACTION_GET_CONTENT
type="image/*"
}
val resultContract=registerForActivityResult(ActivityResultContracts.StartActivityForResult()){ result: ActivityResult?->
if(result?.resultCode==Activity.RESULT_OK){
Log.v("xxx","it was ok")
val uri=result.data?.data
ivPhoto.setImageURI(uri)
}
else{ Log.v("xxx","it was not ok")}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnTakePhoto=findViewById(R.id.btnTakePhoto)
ivPhoto=findViewById(R.id.ivPhoto)
btnTakePhoto.setOnClickListener {
resultContract.launch(choosePicure)
}
}
}
I've just watched this video and checked all comments, but still didn't figure out what the problem is. plz help :)
Use registerForActivityResult
val btnChoseImage: Button = findViewById(R.id.btnChooseImage)
val ivImage: ImageView = findViewById(R.id.ivImage)
val getImage = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri->
ivImage.setImageURI(uri)
}
btnChoseImage.setOnClickListener {
getImage.launch("image/*")
}
This code is not working in Bumblebee Android as ' startActivityForResult' is deprecated.
Can you advise on an alternate code in place of the present code so that it runs in Bumblebee also?
you can use ActivityResultContracts, to know how to use check out the google guide which provides a similar example used in this video:
developer.android.com/training/basics/intents/result#launch
👍😁
Nice!!! :)
:) Thanks
startActivityForResult has been deprecated
startActivityForResult() is deprecated
instead you should create an ActivityResultLauncher object using the function registerForActivityResult()
with a lambda function that handles the results come from launched activity.
Creating a resultLauncher :
private val resultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {result->
if(result.resultCode == Activity.RESULT_OK){
val data = result.data?.data
binding.[YOUR IMAGE VIEW].setImageURI(data)
}
}
I personally just used startActivityIfNeeded(it, 0), but I am saving this to come back to to do the proper way. My way did work though.
Alright yall, I came up with a solution (as of 2023):
val image = findViewById(R.id.[your imageview name])
val content = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val data = result.data?.data
image.setImageURI(data)
}
}
findViewById(R.id.[your button name]).setOnClickListener {
val myIntent = Intent(Intent.ACTION_GET_CONTENT).also {
it.type = "image/*"
content.launch(it)
}
}
thanks man.
As the code for the intent is now deprecated I was able to do it like this:
var ivImage = findViewById(R.id.ivImage)
val getContent = registerForActivityResult(ActivityResultContracts.GetContent()) { uri: Uri? ->
ivImage.setImageURI(uri)
}
val btnGalery = findViewById(R.id.btnGalery)
btnGalery.setOnClickListener {
getContent.launch("image/*")
}
You can use binding to access element by ID. For example : binding.ivImage.setImageURI(uri)