Accelerometer Sensor Tutorial in Android Studio (Kotlin 2021)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so what is up guys in this video i'm going to be showing you how to use the accelerometer from your android phone and just to give you an example right now you can't see it but i'm moving my hand up and down so when i tilt the phone downwards this box goes up if i tilt it to the left it goes to the left if i tilt it to the right it goes to the right and if i lay my phone completely flat down it will register as it is flat so these are just a few of the things you can do with an accelerometer and it's actually my mate even who gave me the idea to make a video about this because of course you can use this to make some really crazy cool ui effects or maybe you want to make a game in kotlin and i think it's just really nice to know how to use this sensor on your android phone this should work on most modern android phones i'm not really sure how it works on the older ones but most phones should have an accelerometer and i'm not really sure how to test this on the emulator so i won't be able to explain that but if you have a real device that is the best way to test these things but anyways let's go ahead and get started with a new empty project and the first thing we're going to do inside here is go to our res folder click on values and click on our themes because i just want to change the default colors which are going to be this purple and we'll just change that to red and the other one also to red now we can go ahead and close this values folder and open the layout folder to go to our activity underscore main xml and click on the split view and inside here we're going to go and change this to a relative layout and we're going to give it a background color now we're just going to click on this black square and make it to a dark gray then the second thing to do is to go ahead and replace this app section and type in center in parent and set that to true then we want to give this an id and we're going to call it tv underscore square the width and height should be set to 200dp the background color is going to be set to red as well so we're just going to click add color black and manually change it to this right over here then we want to set the gravity to center and the text color to color white and finally the text size to 20sp of course this will vary on your phone but for my phone 20sp looks really good now we can go ahead and select everything and hold down ctrl alt plus l to rearrange everything and format it and that will take care of everything we need for our activity main xml next we can go ahead to our main activity file and get started immediately by typing a few late in advance so private late init var and the first one is going to be a sensor manager of type sensor manager and before we continue this we actually have to extend another class which is going to be the sensor event listener class this one down here or it's actually an interface and once you have that you're going to get this error and you're going to have to implement its members which is on sensor changed and on accuracy changed so just implement both of those they will appear under your oncreate method and we will deal with those later but let's continue with the late in advance so private late in it and we want to type in square which is going to be of type text view then in our oncreate file we're going to start by typing appcompat delegate and dot set default night modes app compat delegate and then mode knight no to make sure our phone remains in light mode then we need to locate our square and to do that we're just going to call find view by id and call it from our xml file tv underscore square and we have to create a method called setup sensor stuff after that we have to go and create this function so private function setup sensor stuff create a space and the first thing inside here to do is to create the sensor manager or actually initialize it so sensor manager equals get system service and that's going to be a sensor service and we need to cast that as a sensor manager then we need to specify the sensor we want to listen to so sensor manager get default sensor and we're going to type in sensor type and we want the accelerometer add the null safety operator and we're going to use the also keyword then inside here we are going to register a listener so we're going to type in sensor manager dot register listener and first we have to refer to this then we can pass in it which is going to be the sensor then we have to pass in a sensor manager with a delay and we are going to click on fastest because that will update us with the most recent details from our hardware and finally we need to set a max report latency so we're going to get our sensor manager again and we're just going to do the same thing as we did earlier and we can actually clean this up by going like this so it can be more readable and this is all we have to do to set up the sensor so now we have registered a listener now we actually have to retrieve the information and we can do that on sensor changed so let's get rid of the to do and the first thing we want to do is check if the event which is nullable and the sensor which is also nullable type is equal to sensor dot type accelerometer if it is this kind of sensor we want to retrieve its values so first we're going to start by creating a value called sides and this will just retrieve the values when we tilt the phone left and right so event dot values and i found that at the index of zero then we're going to go and type in value up down which will retrieve the values when we tilt our phone upwards or downwards and that is going to be equal to events dot values at the index of one and this is going to continuously retrieve these values so we don't have to worry about updating the ui or anything because it will automatically do it for us every time it retrieves a new value then we have to go ahead and call our square text view and we're just going to create an apply block to save some typing and the first one we want to refer to is rotation x and that's going to equal up down times three f then we're gonna go for rotation y which is going to equal sides times three f and of course you can play with these values to see how much you want the square to rotate when you rotate the phone i just thought 3f was the most appropriate option to make it look smooth and then we're going to go ahead and type in rotation and that is going to equal the negative of sides and the reason i made it negative is because when i originally created it if you just insert sides it will do the opposite of what you turn your phone to so if you do the negative of the opposite you'll just get it to follow your phone as you tilt it and i know that sounded confusing but another way to understand that is just to take away the negative and test it for yourself then we're going to go ahead and add a translation x which is going to equal the sides times minus 10 and translation y which is going to equal up down times 10. and then let's also implement some code that will allow us to change the color if the phone is perfectly flat so value caller is going to equal if up down to int is equal to zero and sides to ins is equal to zero then we will know that the phone is perfectly flat but if they are both zero we want to return the caller dot green else we will return the color.red then we can go down and refer to our square and set the background color to the color we just created and also we want to make sure we can see the values as we tilt the phone so we can register it and understand how it works so we're going to type in square dot text and that's going to equal up slash down and we need to interpolate so the first one we want is up down to int then we want to create a new line so we're going to create a break and type n and we're going to type in left slash right interpolate again and get these sides to int and this will take care of retrieving the values and showing them in the text view and since we won't be using on accuracy changed we're just going to return out of this by typing return and finally just to make sure nothing goes wrong when we close the app and it doesn't use unnecessary space or lead to unnecessary memory leaks we're going to go ahead and call on destroy and unregister our sensor manager so sensor manager dot unregister listener and the context is going to be this and yeah it was as simple as that all you have to keep in mind is to play around with these values over here and you can essentially do anything you want with this tilting feature but let's go ahead and click on run and perfect the app has loaded and now if we actually try to move our phone right now i'm moving my phone down and the more we move it down the more it goes up and if we put it on a flat surface it will stay green if we change it to the right you'll see that we'll move to the right and the same thing goes if we move it to the left but yeah that's actually all i wanted to share with you guys in this tutorial if you have any questions you're more than welcome to leave them in the comments section below but with that being said thanks for watching and i'll see you guys in the next video see you
Info
Channel: Indently
Views: 18,231
Rating: undefined out of 5
Keywords: android studio, code, coding, program, programming, tutorials, android, studio, java, kotlin, python, c#, how to, app, applications, developer, developement, learn coding, learn programming, free, coding in flow, androidevs, code palace, windows, computers, ios, technology
Id: xcsuDDQHrLo
Channel Id: undefined
Length: 9min 20sec (560 seconds)
Published: Wed Jan 13 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.