Hello this is what has occurred to me.
And in this material we are going to put the coats inside our main activity class in order to ask the
user to give us the permission to draw widgets on the screen on the user's phone screen.
So first of all let's delete this unclick listener are pretty and float in here because we do not needed
any more and here so we just have the bottom billion floating and we initialize it here inside our uncreate
method.
Now we need to go inside our main activity class and puts the code or the ID of the drawing over other
apps permission.
So each permission should have an ID.
Right.
Lets put the ID here.
So I'm going to paste this line of code here so it is private static.
So it is a class actually a class variable and it is fine.
All it is constant it cannot be changed later and it is of type integer.
The name is Id draw over other apps permission and we initially assign the value number one here.
It doesn't really matter what you assign to this variable but it must be unique.
Ok For example I can assign the value in 2000 or files and it doesn't really matter.
But this value that I assign to this variable must be a unique value.
Now we need to go inside our uncreate method.
So now I'm going to paste an if statement in this and create method here.
So this is my IF statement and you can see that I'm saying if build that version that STK int is greater
than or equal to build that version that codes that m.
So as you may already know from Android marshmallow and later the must ask runtime permissions before
Emrick much marshmallow.
Are these kinds of permissions are already already granted.
Or for example the user may have given permission to the application by installing the app on his or
her device.
But from Android marshmallow and later we must ask this permission directly.
The user must know what we are going to do with the app with the device.
So if you are saying if built that version that STK is greater than or equal to build that version code
that M M stands for and with marshmallow OK and later and we have an AND statement if not or bant here
it is not not settings that can do the overlays context this or main activity that this lets It's a
good program practice to use our context like this one main activity that is in order in order to refer
to a specific context.
Here you are saying that if the settings application of the user's device has not given our application
the permission to draw our widget over other apps.
So the settings application must give us the permission to seethings application is going to be opened
and it's going to give us the permission.
So if this permission is not given by the settings application to our application we're going what is
it we are going to go inside the curly braces of this statement and we are going to first create an
Internet assignment operator new intent settings that action managed overlay permission.
We want to open the settings application in order to ask this permission action manage orally permission.
So it's going to open that specific part of the city's application in order to give us their permission
to manage to float our widget over on the screen come up and we are using this word here.
So you need to pass a value of that as the second argument you are right or parse.
We need to specify the package name of our application that is going to ask this permission.
This package name is actually unique for every application.
So we put two coats here and inside the coats we must use this value here is the exact value package
the capital piece.
As you can see it stores all of these letters of this package and it is actually the lower cost.
So we put a call in here exactly the same.
And we put a plus in order to concatenate this value with this one get package name.
So by using this gift package name if we can access the package name our application Okay.
And then the say start activity for resolved.
But here I don't like this start activity for resolve here because this drawing over other apps permission
it's not it's not like any other permissions in Android we have to open the settings application and
then the settings application is going to actually help us to give the APTA permission to float the
view on the screen so we can actually use a much more simpler solution instead of saying start start
activity for results or using these ideas that are or other permissions here we can't just simply say
start activity start activity and then we need to just pass the intent.
So we passed the intent here.
So now let me just delete this line of code here.
Let's also delete this constant that we created here for the permission.
OK.
It's actually necessary to create such constants.
If you are actually asking for runtime permission but this drawing over other apps permission is not
a usual permission.
OK it's not like any other permissions.
It's completely different.
So we can use a completely different solution.
Instead we use Start activity intent and we just passed the intent here.
Now let's also implement the float the view on the screen.
Inside our class.
So before the curly braces were made I think the class I'm gonna say private void.
And then if it is going to be floats the view on the screen a pair of prentices let's open and close
this Meffert by using these curly braces.
Let's say PTEN fluting that set on click listener knew and the hold down the shift key on your keyboard
and press and the key on your keyboard in order to see the autocomplete feature options and then press
on your keyboard.
And inside is ugly.
If we can now start the service we can say start service and we need to pass and enter.
So with a new Internet object and as the first argument we are going to parse the context main activity
that this comma and as the second argument we need to pass the service.
The service is floating.
View service that class semicolon.
So now let me create a new line so that we can see what's going on here.
And finally get the services started.
We don't need the main activity anymore so we just call finish in order to close it.
That's it.
Now let's run the app and then I am way to choose my emulator as you can see my emulator runs and Redgate
or UAP I-26 and press on.
OK so here visitings application is open.
It says display over other apps permission allowed display over other apps.
Now I'm going to actually allow this app to display our widget on the screen by just turning this one
on.
This we did this component.
Now I'm going to close this app OK.
I'm going to close it now.
I'm going to open my apps and I'm going to choose this application floating view.
So the app is now opened.
Let's click on floating button.
Now the wizard is shown to the user but there is a problem here when I click on this widget here.
It's not actually switched to the expanded state.
Right.
Or as you can see when I drag the switches over on the screen it's not small.
There's something wrong here.
So we need to go back to android studio and it's actually open to float any restart of a fight.
The problem is here inside this file inside this class.
And as you can see inside the switch statement we have this on the right.
Let's go inside.
Others move it.
We have this switch statement and we actually created these variables.
But we are not using them.
How can I actually understand that we are not using them because as he has even hovered over this for
example this one to start exposition it says de-value w the palms that X assigned to a start position
is never used.
This means that these variables for example start expose start white holes start to touch explore start
touch why these variables are not used here and when here for example for this emotional event that
action of we are actually referring to is valuable.
But we are accessing the default values that we said here.
Zero zero zero zero.
As you can see we are not accessing the actual values.
There is something wrong here.
So now I want you to pause the video and solve this problem on your own.
And if you cannot actually solve it you can actually play the video and then I'm going to show you how
we can solve this problem.
So now I'm going to show you that in the solution.
The solution is here instead of assigning actually default values to this variable.
Let's just say this is declared them.
Let's delete these default values but at the bottom here it's actually showing us an error right.
I just deleted these default values.
Now let's cut all of these variables here.
All of these four variables.
Let's scroll to the top and let's declare them inside our class.
Here let's face them here.
Now the problem is solved and we have no errors here inside our untouched untouched method here.
Right now let's run the app that me show you the emulator.
If I click on this button.
Now we have this widget that really did.
Now it is actually switched to the expanded state.
Right now let's test the unclick listener of these components for example this one lion line is tapped
the back leopard image.
The next we have this close button.
We have this open the application is opened and let's actually click on this close button.
Now it says expanded closed but it is pressed.
So if I click on this close spot in here now the service is stopped completely.
Let's open this up again.
View let's click on this button here.
Let's actually drag it as you can see it is now completely small.
Right.
If it open for example Google Chrome now we can see it this widget is actually completely smooth and
we can track it here on the screen.
So that's it.
Congratulations you've made it and I hope that you can create your own Android applications and creating
widgets like this font.
Or you can create a widget completely based on or on your own design and user experience and make sure
to upload your applications to Google Play and make me proud.
So thank you very much for watching.
And I will see you in the next application by.