In this tutorial we're going to go ahead and we are going to add another functionality.
Actually it is going to be the final functionality of this application not the final but our application
is almost finished.
So we want to allow the user to share this image with other applications that are going to be that can
interact with other people such as email G-mail telegram app for example or whatsapp or any other applications.
So first we need a button.
So first of all make sure to open the activity under minute example here inside this pallet section
just click on and select this button and just drag it here into our layout and below these six bars
here.
Just put it here so you can see that these constraints are already created for the spot and automatically.
So I want this.
Let's actually select this spot and we need to have a top constraint here.
So if we hover over the top actually edge of the spot and you can see this little circle that is pulsating
right.
I just need to create a constraint to the bottom edge of this actually see each part here of these Baluchi.
But here right.
Just create this constraint here to this little circle and then just release it.
Let's increase the margin top of this spot and so just that it's an expansion inside this property section
for that margin tightest they've been here.
So this is our partner right now.
Let's select the spot and let's provide an item for this spot in between share and then person and to
your keyboard.
And for the text attribute just type in here.
Share your image your image.
Exclamation mark and then personated on your keyboard.
So that's it.
This is our button.
So let's go to our main activity class here let's scroll to the top of the file and first we need to
actually declare our object are valuable.
So private button private button PTEN and share semicolon Let's go to the uncreate effect I here let's
initialize our pity and share buttons so between and share assignment operator let's cast this object
to a button object find view ID.
Our thoughts ideas thoughts.
Pythian shared semicolon.
Now let's pro-white a listener for this one.
So after this line of code that sets the listener for this beauty and save the picture I can see Pythian
share that sets of Kleek listener.
And then here I just need to pass the context the main activity that is because this class main activity
is actually implementing this view that non-clinical is an interface.
You can see that we have no errors here.
So let's find the uncle whomever this is going to be executed and about this.
So we already checked for this condition that if the ID is for example PTEN take a picture if the ID
is BT and say picture and now we need to check for another ID so we can see it adding curly brace of
this LCF statement here that checks whether the ID is actually the BT and save the picture right after
the athing curly brace of this statement I can't create another LCS they've been so LCF actually view
that gets ID is equal to or that ID that Pythian share.
This means that if the user is actually interacting with that BT and shell button inside our user interface
and if the user is pressing this button then its going to execute.
Is this LCP statement here.
So lets go inside these LCF statement and lets see what we are going to do next.
RJ I already won.
So now first let me tell you that sharing an image with other applications can be error prone so we
must put our coats inside a try catch block.
OK.
So let's actually just step in here try and then here after the curly brace of destroyed like just typing
head catch inside the apprentice's just you know to specify the exception so just typing here exception
just open and close this test by using the curly braces and just they've been hit eat that print stacks
race.
So let's go inside is Charlie Black.
First of all we need to create a variable of type file.
You need to have the image file.
So I can just they've been filed and the name of this variable is going to be my picture my picture
file.
Right.
Assignment operator and now I'm going to the first with is safe file class that safelight.
So you know that this save file method is going to return an object of a fight.
Let me show you this method.
Inside the safe class you can see this method here right.
This is actually a static method that the return type of this if it is filed.
So this means this method must return a value of that object so let's go back to main If the class.
So say fine that's a fine here we need to pass two arguments the first one is the activity it's just
they can hear main activity that this and when it is specified is such an argument which is going to
be that bitmap so bitmap semicolon.
Right now this variable might be fine.
File Holts that image the actual image that is saved in Access not storage.
Now I can say so here let me tell you that the reason that we put on this try catch blocks here is that
we wanted to use this method save a file method.
OK.
Because the safe find if it is actually error prone if you must put this code here so that when we want
to share this image with other applications we have no errors there.
So after this line of code we need to create a value but of that you are inside the android that net
package.
Ok not Java dot net but this one.
And that's not you or I.
And the name of this variable is going to be my you or I.
Assignment operator so I mean if you have this you are here in order to access our saved a picture file.
So I can just say you are I thought fit on file from file.
And now we just need to pass my picture file here semi-colon.
That's it because we want to share our image with other applications.
We need to create a new intent called sheer intent.
Let's just say be here intent.
The name of this intent is going to be shared.
Intent assignment operator new intent.
And inside the Apprentice it's just they've been here intent that action sent intent that action sent.
Let's actually find it here.
And so this is our intent that we want to open the action center it's going to allow us this intent.
And I was to choose the applications that we share our image with other users such as whatsapp email
Gmail or any other applications that you have installed on your device.
And now if you need a specific data type this is the type of the intent.
So it's a step in here.
Sure intent that set type to just put two coats here this type is very case sensitive it must be lower
case.
So just typing here text slash plane semi-colon OK the type must be text slash playing because we want
to share this actually find with other applications for the time step in here.
TEXT slash play a plain text right now even to specify the text.
So in other words we want to specify the subject of the image so I can say Shirlington that put extra
here just typing here put extra.
So we need to specify the type the type is going to be subject right intense that extra subject intent.
Let me just type in intent that extra subject.
Ok comma and now we need to specify the actual text.
So just put two coats and just type in here.
This picture is sent.
This picture is sent from color app that I created myself that I created myself for example right.
Semicolon.
OK.
You can actually use this application in order to send images to your friends.
And this message is going to be shown to the users.
Ok this picture is sent from the color app that I created myself.
Now I can say.
So we need to pass.
We need to actually send the 5 the picture file with you.
Right.
So I can say sharing Sherrington.
That puts extra intent that extra steam stream actually to intent that extra stream.
Is this fun.
Karma and that we can pass that you are a my you are right.
Semicolon if you don't put this line of code here.
The app is not going to be able to actually send the picture file the picture with the intent.
The other use to add X actually the to other users.
So it is very important and it's that threat stream need to pass this here as a threat stream.
And in my you or I here in other words it is actually a map right.
Key value x threat stream is the key and the you is the value which has my picture file.
Here in line number one hundred and forty you can see this my picture file.
So when you say X threat stream this means that we want to actually send a file.
OK.
With this intent and that file is going to be my you are right that holds this might be terrified.
In other words not actually holds but has a reference to this my picture file.
So now I can say start activity intent that create chooses so you can actually open an intent for us
in order to choose the applications that we want to actually share the image with.
First we need to pass we can see that use to create chooser here.
Not now we need to pass the intent that we created here.
So the intent is going to be sharing karma and then we need to pass the message.
So I just put two coats here and here I can say let's share your image your picture actually your picture.
The other apps with others here let me create some new lines so that you can see these values better.
So let's share your picture with others.
So these are the codes that we need in order to share in order to allow the app to share our image a
picture of it.
Other applications.
So now it's time to run the application but under the emulator it's not going to work properly.
If you want to actually see it work properly make sure to test your app on your actual device device.
So for now I just want to show you how it works on the emulator.
Let me show you the emulator now.
Our Are is running on the simulator.
Let's take a picture with this one.
And then here let's click on share your image.
When I click on your image it connects you to say cell select conversation.
Well once you start in your conversation you'll see that here.
So the only app that is on the emulator that is actually available in order to share our image with
is the message.
Let's click on your message and open them message up here for us.
So if you want to actually see other applications make sure to test this app on your actual device where
you have lots of other messaging applications for example such as a whatsapp or any other messaging
applications that you can share your image with others.
So that's it.
But we need to add the final functionality to this application and that that is the DS subparts I want
this search bar here to display this form.
Actually look at this one.
I want this one to be read and I want the form of this sequel to be clean and I want the form of the
search bar to be actually blue.
I'm going to add that functionality in the next story or if you have any questions about the concepts
that they taught you in this lesson please post the discussion in the course and then I do my best to
answer all of your questions.