1
00:00:00,890 --> 00:00:03,250
Hello this is what has occurred to me.

2
00:00:03,590 --> 00:00:10,970
And in this material we are going to put the coats inside our main activity class in order to ask the

3
00:00:10,970 --> 00:00:18,860
user to give us the permission to draw widgets on the screen on the user's phone screen.

4
00:00:18,860 --> 00:00:24,280
So first of all let's delete this unclick listener are pretty and float in here because we do not needed

5
00:00:24,330 --> 00:00:31,850
any more and here so we just have the bottom billion floating and we initialize it here inside our uncreate

6
00:00:31,850 --> 00:00:32,670
method.

7
00:00:32,690 --> 00:00:42,830
Now we need to go inside our main activity class and puts the code or the ID of the drawing over other

8
00:00:42,830 --> 00:00:44,290
apps permission.

9
00:00:44,780 --> 00:00:46,430
So each permission should have an ID.

10
00:00:46,430 --> 00:00:47,130
Right.

11
00:00:47,210 --> 00:00:50,410
Lets put the ID here.

12
00:00:50,620 --> 00:00:55,680
So I'm going to paste this line of code here so it is private static.

13
00:00:56,200 --> 00:01:02,330
So it is a class actually a class variable and it is fine.

14
00:01:02,330 --> 00:01:07,230
All it is constant it cannot be changed later and it is of type integer.

15
00:01:07,330 --> 00:01:13,640
The name is Id draw over other apps permission and we initially assign the value number one here.

16
00:01:13,690 --> 00:01:19,370
It doesn't really matter what you assign to this variable but it must be unique.

17
00:01:19,520 --> 00:01:26,070
Ok For example I can assign the value in 2000 or files and it doesn't really matter.

18
00:01:26,120 --> 00:01:31,100
But this value that I assign to this variable must be a unique value.

19
00:01:31,110 --> 00:01:35,000
Now we need to go inside our uncreate method.

20
00:01:36,500 --> 00:01:41,510
So now I'm going to paste an if statement in this and create method here.

21
00:01:42,290 --> 00:01:51,500
So this is my IF statement and you can see that I'm saying if build that version that STK int is greater

22
00:01:51,530 --> 00:01:57,230
than or equal to build that version that codes that m.

23
00:01:57,260 --> 00:02:06,270
So as you may already know from Android marshmallow and later the must ask runtime permissions before

24
00:02:06,280 --> 00:02:07,800
Emrick much marshmallow.

25
00:02:07,960 --> 00:02:11,230
Are these kinds of permissions are already already granted.

26
00:02:11,240 --> 00:02:21,230
Or for example the user may have given permission to the application by installing the app on his or

27
00:02:21,230 --> 00:02:22,170
her device.

28
00:02:22,340 --> 00:02:28,620
But from Android marshmallow and later we must ask this permission directly.

29
00:02:30,750 --> 00:02:35,130
The user must know what we are going to do with the app with the device.

30
00:02:35,460 --> 00:02:42,180
So if you are saying if built that version that STK is greater than or equal to build that version code

31
00:02:42,270 --> 00:02:51,860
that M M stands for and with marshmallow OK and later and we have an AND statement if not or bant here

32
00:02:51,860 --> 00:03:01,170
it is not not settings that can do the overlays context this or main activity that this lets It's a

33
00:03:01,170 --> 00:03:09,670
good program practice to use our context like this one main activity that is in order in order to refer

34
00:03:09,670 --> 00:03:10,970
to a specific context.

35
00:03:12,220 --> 00:03:21,950
Here you are saying that if the settings application of the user's device has not given our application

36
00:03:22,190 --> 00:03:27,660
the permission to draw our widget over other apps.

37
00:03:27,890 --> 00:03:33,490
So the settings application must give us the permission to seethings application is going to be opened

38
00:03:33,950 --> 00:03:35,390
and it's going to give us the permission.

39
00:03:35,420 --> 00:03:42,680
So if this permission is not given by the settings application to our application we're going what is

40
00:03:42,890 --> 00:03:47,210
it we are going to go inside the curly braces of this statement and we are going to first create an

41
00:03:47,210 --> 00:03:55,490
Internet assignment operator new intent settings that action managed overlay permission.

42
00:03:55,490 --> 00:04:02,560
We want to open the settings application in order to ask this permission action manage orally permission.

43
00:04:02,570 --> 00:04:08,270
So it's going to open that specific part of the city's application in order to give us their permission

44
00:04:08,300 --> 00:04:16,490
to manage to float our widget over on the screen come up and we are using this word here.

45
00:04:16,500 --> 00:04:20,830
So you need to pass a value of that as the second argument you are right or parse.

46
00:04:21,100 --> 00:04:28,200
We need to specify the package name of our application that is going to ask this permission.

47
00:04:28,270 --> 00:04:32,490
This package name is actually unique for every application.

48
00:04:32,590 --> 00:04:40,090
So we put two coats here and inside the coats we must use this value here is the exact value package

49
00:04:40,450 --> 00:04:42,180
the capital piece.

50
00:04:42,370 --> 00:04:47,600
As you can see it stores all of these letters of this package and it is actually the lower cost.

51
00:04:47,770 --> 00:04:51,590
So we put a call in here exactly the same.

52
00:04:51,910 --> 00:04:56,730
And we put a plus in order to concatenate this value with this one get package name.

53
00:04:56,740 --> 00:05:04,290
So by using this gift package name if we can access the package name our application Okay.

54
00:05:04,690 --> 00:05:08,130
And then the say start activity for resolved.

55
00:05:08,320 --> 00:05:16,150
But here I don't like this start activity for resolve here because this drawing over other apps permission

56
00:05:16,180 --> 00:05:23,350
it's not it's not like any other permissions in Android we have to open the settings application and

57
00:05:23,350 --> 00:05:30,490
then the settings application is going to actually help us to give the APTA permission to float the

58
00:05:30,490 --> 00:05:38,060
view on the screen so we can actually use a much more simpler solution instead of saying start start

59
00:05:38,080 --> 00:05:44,740
activity for results or using these ideas that are or other permissions here we can't just simply say

60
00:05:44,980 --> 00:05:52,090
start activity start activity and then we need to just pass the intent.

61
00:05:52,090 --> 00:05:54,760
So we passed the intent here.

62
00:05:55,570 --> 00:05:58,620
So now let me just delete this line of code here.

63
00:05:58,930 --> 00:06:03,910
Let's also delete this constant that we created here for the permission.

64
00:06:04,140 --> 00:06:04,750
OK.

65
00:06:05,050 --> 00:06:08,680
It's actually necessary to create such constants.

66
00:06:08,680 --> 00:06:15,070
If you are actually asking for runtime permission but this drawing over other apps permission is not

67
00:06:15,070 --> 00:06:16,420
a usual permission.

68
00:06:16,610 --> 00:06:18,830
OK it's not like any other permissions.

69
00:06:18,910 --> 00:06:20,530
It's completely different.

70
00:06:20,650 --> 00:06:23,350
So we can use a completely different solution.

71
00:06:23,650 --> 00:06:29,020
Instead we use Start activity intent and we just passed the intent here.

72
00:06:29,020 --> 00:06:32,770
Now let's also implement the float the view on the screen.

73
00:06:32,800 --> 00:06:34,270
Inside our class.

74
00:06:34,270 --> 00:06:39,580
So before the curly braces were made I think the class I'm gonna say private void.

75
00:06:40,780 --> 00:06:45,780
And then if it is going to be floats the view on the screen a pair of prentices let's open and close

76
00:06:45,780 --> 00:06:47,900
this Meffert by using these curly braces.

77
00:06:47,920 --> 00:06:56,580
Let's say PTEN fluting that set on click listener knew and the hold down the shift key on your keyboard

78
00:06:56,610 --> 00:07:02,100
and press and the key on your keyboard in order to see the autocomplete feature options and then press

79
00:07:02,100 --> 00:07:03,390
on your keyboard.

80
00:07:03,700 --> 00:07:04,670
And inside is ugly.

81
00:07:04,690 --> 00:07:09,920
If we can now start the service we can say start service and we need to pass and enter.

82
00:07:09,930 --> 00:07:16,350
So with a new Internet object and as the first argument we are going to parse the context main activity

83
00:07:16,360 --> 00:07:21,650
that this comma and as the second argument we need to pass the service.

84
00:07:21,720 --> 00:07:23,280
The service is floating.

85
00:07:23,280 --> 00:07:27,260
View service that class semicolon.

86
00:07:27,260 --> 00:07:30,200
So now let me create a new line so that we can see what's going on here.

87
00:07:30,200 --> 00:07:32,440
And finally get the services started.

88
00:07:32,480 --> 00:07:38,360
We don't need the main activity anymore so we just call finish in order to close it.

89
00:07:38,450 --> 00:07:39,110
That's it.

90
00:07:39,110 --> 00:07:45,930
Now let's run the app and then I am way to choose my emulator as you can see my emulator runs and Redgate

91
00:07:45,980 --> 00:07:48,170
or UAP I-26 and press on.

92
00:07:48,170 --> 00:07:51,510
OK so here visitings application is open.

93
00:07:51,530 --> 00:07:56,480
It says display over other apps permission allowed display over other apps.

94
00:07:56,480 --> 00:08:02,570
Now I'm going to actually allow this app to display our widget on the screen by just turning this one

95
00:08:02,630 --> 00:08:02,980
on.

96
00:08:03,100 --> 00:08:05,090
This we did this component.

97
00:08:05,140 --> 00:08:09,010
Now I'm going to close this app OK.

98
00:08:09,170 --> 00:08:11,360
I'm going to close it now.

99
00:08:11,380 --> 00:08:15,310
I'm going to open my apps and I'm going to choose this application floating view.

100
00:08:15,400 --> 00:08:16,710
So the app is now opened.

101
00:08:16,900 --> 00:08:18,580
Let's click on floating button.

102
00:08:18,580 --> 00:08:25,550
Now the wizard is shown to the user but there is a problem here when I click on this widget here.

103
00:08:25,600 --> 00:08:29,280
It's not actually switched to the expanded state.

104
00:08:29,290 --> 00:08:29,870
Right.

105
00:08:30,070 --> 00:08:35,020
Or as you can see when I drag the switches over on the screen it's not small.

106
00:08:35,260 --> 00:08:37,140
There's something wrong here.

107
00:08:37,240 --> 00:08:43,240
So we need to go back to android studio and it's actually open to float any restart of a fight.

108
00:08:43,420 --> 00:08:47,830
The problem is here inside this file inside this class.

109
00:08:48,160 --> 00:08:52,450
And as you can see inside the switch statement we have this on the right.

110
00:08:52,450 --> 00:08:53,120
Let's go inside.

111
00:08:53,130 --> 00:08:53,740
Others move it.

112
00:08:53,740 --> 00:08:58,920
We have this switch statement and we actually created these variables.

113
00:08:59,140 --> 00:09:00,610
But we are not using them.

114
00:09:00,610 --> 00:09:06,700
How can I actually understand that we are not using them because as he has even hovered over this for

115
00:09:06,700 --> 00:09:13,820
example this one to start exposition it says de-value w the palms that X assigned to a start position

116
00:09:13,990 --> 00:09:15,580
is never used.

117
00:09:15,580 --> 00:09:22,400
This means that these variables for example start expose start white holes start to touch explore start

118
00:09:22,540 --> 00:09:30,100
touch why these variables are not used here and when here for example for this emotional event that

119
00:09:30,100 --> 00:09:34,350
action of we are actually referring to is valuable.

120
00:09:34,360 --> 00:09:37,900
But we are accessing the default values that we said here.

121
00:09:37,900 --> 00:09:39,750
Zero zero zero zero.

122
00:09:40,240 --> 00:09:42,730
As you can see we are not accessing the actual values.

123
00:09:42,730 --> 00:09:44,040
There is something wrong here.

124
00:09:44,260 --> 00:09:49,930
So now I want you to pause the video and solve this problem on your own.

125
00:09:50,260 --> 00:09:55,390
And if you cannot actually solve it you can actually play the video and then I'm going to show you how

126
00:09:55,390 --> 00:09:56,790
we can solve this problem.

127
00:09:57,190 --> 00:10:00,570
So now I'm going to show you that in the solution.

128
00:10:01,120 --> 00:10:07,790
The solution is here instead of assigning actually default values to this variable.

129
00:10:08,170 --> 00:10:10,810
Let's just say this is declared them.

130
00:10:10,920 --> 00:10:16,870
Let's delete these default values but at the bottom here it's actually showing us an error right.

131
00:10:16,900 --> 00:10:22,730
I just deleted these default values.

132
00:10:22,760 --> 00:10:27,170
Now let's cut all of these variables here.

133
00:10:27,800 --> 00:10:29,250
All of these four variables.

134
00:10:29,520 --> 00:10:33,140
Let's scroll to the top and let's declare them inside our class.

135
00:10:33,140 --> 00:10:35,730
Here let's face them here.

136
00:10:35,750 --> 00:10:42,080
Now the problem is solved and we have no errors here inside our untouched untouched method here.

137
00:10:42,080 --> 00:10:49,750
Right now let's run the app that me show you the emulator.

138
00:10:49,870 --> 00:10:51,080
If I click on this button.

139
00:10:51,370 --> 00:10:53,910
Now we have this widget that really did.

140
00:10:53,980 --> 00:10:57,600
Now it is actually switched to the expanded state.

141
00:10:57,670 --> 00:11:05,980
Right now let's test the unclick listener of these components for example this one lion line is tapped

142
00:11:06,500 --> 00:11:10,390
the back leopard image.

143
00:11:10,390 --> 00:11:13,460
The next we have this close button.

144
00:11:13,480 --> 00:11:20,600
We have this open the application is opened and let's actually click on this close button.

145
00:11:20,600 --> 00:11:24,710
Now it says expanded closed but it is pressed.

146
00:11:24,710 --> 00:11:29,500
So if I click on this close spot in here now the service is stopped completely.

147
00:11:29,870 --> 00:11:32,640
Let's open this up again.

148
00:11:32,850 --> 00:11:35,780
View let's click on this button here.

149
00:11:35,820 --> 00:11:39,400
Let's actually drag it as you can see it is now completely small.

150
00:11:39,780 --> 00:11:40,380
Right.

151
00:11:41,270 --> 00:11:48,560
If it open for example Google Chrome now we can see it this widget is actually completely smooth and

152
00:11:48,560 --> 00:11:53,130
we can track it here on the screen.

153
00:11:53,570 --> 00:11:54,740
So that's it.

154
00:11:54,740 --> 00:12:03,440
Congratulations you've made it and I hope that you can create your own Android applications and creating

155
00:12:03,560 --> 00:12:06,400
widgets like this font.

156
00:12:06,410 --> 00:12:14,900
Or you can create a widget completely based on or on your own design and user experience and make sure

157
00:12:15,140 --> 00:12:20,700
to upload your applications to Google Play and make me proud.

158
00:12:20,870 --> 00:12:22,640
So thank you very much for watching.

159
00:12:22,850 --> 00:12:27,200
And I will see you in the next application by.

