Android App Development in Python With Kivy

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back in today's video we're going to learn how to develop android apps in python so let's get right into it all right now in order to build an android app in python we're going to use the kivy framework or the kv module so what we're going to do first is we're going to open up a command line cmd for example on windows and we're going to say pip install and then ki like that k-i-v-y this is the framework or the library we're going to use in order to develop platform independent apps now these apps can compile for android they can compile for ios they run on windows they run on linux they run on mac so they're platform independent applications and in this video we're going to focus on the android development uh so once you have installed that you can go into the code and say import kivy like that and you can also go ahead and say something like import qiv dot ui x dot and then the individual components that we're going to use uh we're going to skip that part for now we're going to do this later on um and the first thing we want to do is we want to require a specific kv version so we're going to say kivy dot require and we choose the version that this kivy app is going to use now for this video i'm going to use 1.9.0 and the reason for that is that for some reason 2.0.0 so 2.0 2.0.0 this works on my computer so if i require this version it runs on uh windows here where i'm recording but it does not run on my phone this is a samsung galaxy s7 it's not the the newest phone so maybe on your phone it runs with 2.0 as well in my case it runs with that version it does not run with that version i don't know why so you have to play around with that if you want to find out for yourself we're going to look at a compilation process later on but the important thing is you need to require a kv version and of course you can look into the documentations to see okay what's the difference between the versions are there any features that i need that are only available in a specific version and then you can decide based on that but we're going to use this one for now and what we need to do is we need to create a class so let's say we have a class and we're going to call the app that we're going to use here uh neural random for example because the end the end goal is to just have an app that generates random numbers we're just going to make a very simple app because in this video we're going to focus on how to make an android app uh in general and if this video performs well and if you guys are interested in that i'm going to make more videos on specific projects so for today's video we're going to have something very simple uh just some label with a heading so for example neural random and then some label with a random number a button that generates random numbers and that's basically it uh and for this we're going to have a class we're going to call this neural random now and we're going to uh paste app in here but in order to paste app in here we need to first import something we need to say import kivy dot or actually was it from kivy app i think it was from kivy app right yes from kivy.amp we need to import app with a capital a and our class is going to inherit from that app like that so now neural random is an app in and of itself and this app has a function which is called built and it takes a self parameter and this function is what we actually use to return the ui and for now we're just going to say return and we need a label so we're going to say from kivy.uix dot label import label like that with a capital l and then we're just going to return a label object and we're going to say set the text to neural random for example okay uh so that is the class that is now the app this is actually done the only thing that we now need to do is we need to create an instance of that so we need to say um neural random equals neural random there you go and then we need to run that object so we need to say neural random dot run like that uh and that is actually it that's all the magic we now have this app and we can run this app on our computer we don't need to compile it for android we can just run it here and you can see this is what the app looks like at the moment it's not very fancy it's a very basic label but this can also run on android now we're not going to compile it yet so i first want to create the application here on windows on uh in pycharm for the computer and then when we like the app we're going to export it to android so we're going to first finish this app and then we're going to build it for android all right so we're now going to take a look at how we can outsource the design so we don't want to have this build function where we just return some labels and some layouts and some buttons we want to have a design file a kivy file that we then use to define the graphical user interface and we then want to load this file and return it in the build function in order to do that we need to go to the directory that we're working in and we're going to right click here say new file and we're going to call this neural random dot kb now it's very important that this name cannot be chosen arbitrarily so you cannot just choose anything here you need to choose the class name because qivi is automatically going to associate the class name and the file name so if the class is called neural random you want to call this file neural random.kv if the class is called hello world you're going to call the file helloworld.kv now one thing that's also important is that you want to keep the file in lowercase because i had the problem that when i called the file capital n and capital r neural random it did not work on android it still worked on my computer but it did not work on android so if you want to make sure this works you're going to call it uh in lowercase the class name so in this case neural random.kv now inside of that kv file we're going to specify the layout and we're going to start with a simple box layout so this is a very very simple layout and we're going to use these angle brackets here so we're going to say angle brackets box layout and we're going to follow that up with a colon first of all we need to specify the orientation of the layout so we're going to say orientation is going to be vertical and then we can add the elements so then we can say um they go label colon and inside the label we're going to specify the individual attributes of the label so we're going to say text for example is going to be neural random in quotation marks so this is just a basic text and we can specify some attributes like font size and color so for example the font underscore size is going to be 64. and for the color this is a little bit tricky or not tricky but a little bit unusual we need to specify rgb values but normalized rgb values so usually you would say something like 255 46 71 for example now you have to normalize that so divide everything by 255. so for this we're going to use a color picker i have prepared this color picker here from google we can choose a color let's pick something orange and if i want to have this color here i just get the rgb values here down below so 235 1170 and in order to get the actual values i'm going to run a calculator and we're going to divide 235 by 255 this is 0.92 so we can go to pycharm say 0.92 for that uh then we can say 117 divided by 255 0.45 so 0.45 and the last one was zero so this is now the label next we can go and say another label is going to be [Music] text just uh a dash for nothing uh until we have a a random number in there and we're going to say font size is 64. we're going to leave the color because i think by default it's white and this is what i want to have here and last but not least i want to have a button and this button is going to have the text generate and the font size uh 32 for this one and we can also specify the size if we want to now i think for the box layout it's going to take the whole thing uh but if you want to specify a size you can do it like that 150 for example i'm not sure if that's even useful in the box layout here but we're just gonna do that and later on we're also going to add an event on press to call a function but for now we're just going to have the layout here uh so we have the layout designed or specified in this kv file and in order to now use that we're just going to return a box layout so we don't need to import anything we don't need to load this file we're just going to say return box layout like that and if i now run this uh of course we need to [Music] what was it uh uix.box layout import box layout right there you go so we import that we run that and we have a problem that i forget anything here let me just see oh yes vertical needs to be in quotation marks otherwise it doesn't work so let's rerun this here there you go so you can see a basic ui where we have this neural random here we have this label here and we have this button that we can click nothing happens uh but this is how you can design the layout so what basically happens here is uh we have this neural random app and we have this built and this build returns a box layout and then it looks into the same file so the file with the same name the kivy file with the same name as the class and it sees okay we have a box layout here and those are the things inside of that box layout and then basically translates that into kibi into python and it returns the layout that we were looking for all right so the final thing that's missing here in our code is the functionality to generate random numbers and to translate that generation into the ui so we want to generate random numbers and we want to see the random numbers in the ui and for that we're going to create another class and this is going to be the root class we're going to have our own version of the box layout so we're going to have the my route it's going to inherit from box layout and in here we're going to have the generate number function but we're also going to have a constructor and this constructor is going to uh call the super constructor so we're going to have this init method here we're going to call super we're going to pass my route we're going to pass self and we're going to call the init function of the parent class like that so basic constructor and here we're going to have the generate number function all right and what this generate this generate number function is going to do is it's going to manipulate one of the labels um and change the text to a random number and this is going to be a one-liner it's very simple uh but before we can do that we need to have some id for that label so we need to say okay self dot uh dot something so we need to specify not any label we cannot just say self.label because which label we need to specify a specific label and in order to be able to do that we need to have an id for the label that we want to talk to and we need to make some changes here so first of all we're going to change box layout up here to my route so we're going to change this to my route and inside of my route we're going to have a box layout so the top element is in angle brackets box layout now is no longer in angle brackets we're going to take all that indented here like that there you go and we now have this my route class that we have here which inherits from box layout it has a box layout inside the orientation is vertical we have a label here and we have this label here and we want this label to have an id so what we do is we specify up here that there is something called random label and it's going to be mapped to random labels so basically we're just saying that the name random label here is going to be named mapped to the name randomly uh label in the code this is important because uh i can say this label has the id random label we don't need any quotation marks here and i'm going to use random label here in this file and i'm also going to use random label in this file if i would use another id in this file i would have to change this uh to map for example random label here to something else in the code but we're going to use the same name uh so it's not a big deal um and we're going to leave it at that we're going to say random label mapping to random label the id is going to be random label and now we can just go ahead and say self dot random underscore label dot i think it was text yes text equals str so the string type casting off random oh we need to import random first import random there you go then randomlabel.txt equals string.random.randint from zero to a thousand for example uh and that's basically it that's all we need to do but of course we need to now not return a box layout but we need to return the my root object or class here constructor basically uh so i think this is it let's see if it works or if i forgot something but i think that should be it there you go and if i now press generate nothing happens why did nothing happen oh of course because i have to add the on press event here so i need to say on press and here we're going to just specify uh what was it called i think root dot generate generate number or what did i call the function here yeah generate number so i think that is how it works and now you can see it works if i press generate it generates a bunch of numbers and we can see them on the screen so the only step that is left here is to compile this to uh to android and to then run this on our phone so now we get to the final part that is a little bit complicated but it's also the most relevant and the most interesting part how do we take all this and compile this to an android application uh it's not as easy just as just pressing a button that says compiled android we need to put some effort into this and some bad news for everyone who only uses windows and has zero linux experience you will need linux or mac in order to do this however you don't need to have a separate linux distribution you can also use the subsystem for linux you can use a virtual machine but the tool that we're going to use here is called builddozer so builddozer like that build and then ozer this is the tool that we're going to use here and in order to know how to use this tool we're going to visit the documentation so builddozer dot read the docs io and if we click on installation here we can see the instructions basically all we need to do is written here you need to make sure that you have some linux system so you can have a linux installed on a separate laptop you can have uh it running in a virtual machine and you can also have the linux subsystem so if you have the linux subsystem you can just run it if you don't have the linux subsystem you can visit the microsoft store and you can just uh look for ubuntu or for debian or whatever and then you can install it you can follow the instructions if it's too complicated for you you might want to watch a tutorial just type in youtube how to install the windows subsystem for linux if you want to see a tutorial by me let me know in the comment section down below if enough people want to see it i'm going to make a video on it but for now i'm going to assume that you have some sort of linux system and what you want to do then is you want to install pip3 install build dozer so this is a pip tool that you wanna install pip three install build dozer you basically follow the instructions here and then if you're running this on linux you need to to execute these commands so first you update uh you you do a pseudo app update and then you install all of these packages here so cmake and open jdk zip unzip git python 3 pip auto conf and all that you just copy this command here you copy it and you paste it into a terminal and then you run that i'm not going to run that here because i already have it um and it might take some tile the whole compilation process is going to take some time um so you basically run that you run pip3 install scythe and virtual and if you just copy these lines and run them and then you can export this to the path so this is how you install that this is just uh following the instructions you use this pip command you use these two install commands or updated install commands then another pip command and then it's done um and you do some troubleshooting along the way of course if you have to i assume you're able to google through the error messages and once you have that we're going to say builddozer.init so what we're going to do is we're going to navigate to the directory that we're currently working in so in my case this is uh neural nine here and this is the directory where we have the main file and the neural random.kv file or whatever you call it and now we're going to just say let me just clear that here now we're going to say builddozer init like that build those are init and now we have the builddozer.spec file okay so now we're going to do is we're going to uh open that up you can use a terminal editor or you can just navigate to the direction you can open this in pycharm if you want i'm just going to open it in the terminal now so we're going to open the build dozer spec and here we have all sorts of specifications and you want to change the title of the application so for example neural random you want to change the package name you want to call this i don't know neural random app for example uh and you can change a bunch of other things here you can also include extensions and all that you can specify a version you can specify requirements whatever one thing that may be important we're going to do that afterwards is you can also specify let me just see where it is um you can specify the pre-splash file name and the icon file name so if you want your app on your phone to have an icon you specified here and kivy apps have a loading screen so when you open up the app you're going to see something called a pre-splash so this is just going to be an image when the app is loading you're going to see that image and by default it's the kibi logo and of course you can change that to your own logo we're going to do that later on not now so basically you have that specification file i'm going to leave that here and then once you have that once you have everything done in this directory you just run build dozer dash v android debug and this is how you actually compile it so you go to directory build dozer dash v android debug like that and then it starts running and this is going to take a very long time depending on your computer of course but for me it took like i don't know 10 minutes 20 minutes 30 minutes i'm not sure it took a very long time it's not something that you just wait for two minutes and it's done so it's going to take some while i'm going to skip that part get some coffee get some tea let it run and then when it's done we're going to proceed with the process here all right so once the process is finished you can see that the build was successful and that the android packaging is done and now you can go to pycharm and you can see this new bin directory here if you open it up you will see a file called neural random app whatever dot apk so the apk file is what you want to get onto your smartphone in order to install the application but before we talk about that i want to mention that i took all these files here the specification file the main python file the neural random.kv file and i put them into a separate directory here called my app before running the buildozer dash v android debug command and the reason for that is that if you run this in a directory that has sub directories with python files and image files and so on you are going to take them into the project so i have in the neural nine directory here i have uh a directory called prepared in a directory called videos with tons of python files in there and if i have my files here just up there in the neural nine directory it's going to take all the subfolders as part of the project and it's going to produce a bunch of errors this happened to me so i had to create a directory and put all these files inside it and then call the command from there so the command was actually called from neural nine slash my app and you wanna make sure that in that folder in that directory you don't have anything except for the kv files and the the python script and so on and all the resources that you need for that app uh and the second thing that i changed is in the neural random.kv file i added one additional value here to the color because i think that's the opacity if you don't add that for some reason it's going to show you uh the correct color in python or so on the computer but it's not going to work on my android phone at least so i had to add this one here in addition but the good thing is that if you run this command once and you wait for i don't know 10 20 minutes you can run this all over again in a short period of time so if you make a slight change in the code or if you make a slight change in the qivi file you can just rerun this command and it's not going to take so long it's take it takes like 30 seconds 20 seconds something like that uh you don't have to wait all over again to make some slight changes so what you want to do is you want to somehow get this file onto your android phone this apk file you can upload it to drive you can upload it to dropbox you can use an usb cable whatever you want to get it onto your phone and once you have it onto your phone uh or once you have it on your phone you can just open the apk file and click on install and once this is done you will have the app and you can click on it and i'm not sure if we're going to be able to see this in the camera now um but there is the app and if i click the button here it's probably not easy to see let me just reposition my microphone here if i click that button new numbers are generated now maybe i'm going to do a screen recording here and edit it into the video but as you can see the app works on my smartphone and um this is how you do it you basically create all this in kitty python then you compile it on linux or on mac if you want to then you take the apk file get it onto your smartphone install the app and then you can use it all right so last but not least we're going to add an icon and a pre-splash to our application and in order to do that we're going to open up the specification file and we're going to specify the file path to the icons now you can choose whatever icon and pre-splash you like i chose uh this as the icon here and this as the pre-splash it's the same thing but in a different resolution for the icon i have 256 times 256 and for the pre-splash i think it's 2000 times 2000 as you can see oh no you cannot see it up here above my camera there you go um i don't think that there's a limitation on the pre-splash i didn't have any issues with that but i did have issues with the icon so when the icon was too large it crashed my app or something so i think the icon has to be a certain maximum size i'm not sure how it works but with 256 times 256 it works uh and what we're going to do now is we're going to edit in the specification file you can do it in pycharm i'm going to do it in the terminal because it's just uh better syntax highlighting for this file so we're going to open the bulldozer dot specification file and in here we can go down to icon or pre-splash so you can just scroll or you can look for it i'm going to look for icon here there you go i can dot file name and here's the file path so we're going to remove uh we're going to remove the comment here icon.file name equals and i'm just going to remove all this here to i can png because it's in the same directory otherwise of course you specify the full file path uh and the pre-splash file name is gonna be all this removed and just pre-splash there you go so i'm gonna save that and we're gonna build that again and once it's done i'm going to copy to my phone again and then we're going to take a look at it all right it's done i downloaded it from the drive and i installed it on my phone and now you can see hopefully that the icon is the neural nine icon and when i press on it we're going to see the proper pre-splash before the app opens and there you go that's the same app again so it works maybe i'm going to do again a screen recording of my phone here so that you can see it better but that is basically how you program android apps in python all right guys so that's it for today's video i hope you enjoyed i hope you learned something if so let me know by hitting the like button leaving a comment in the comment section down below if you like these android tutorials if you want to see more android tutorials let me also know in the comment section down below if this video performs well i'm gonna do more android projects with python uh and besides that don't forget to subscribe and hit the notification bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 15,732
Rating: 4.953434 out of 5
Keywords: android, android development, android programming, python android development, python android programming, python android, python kivy, kivy tutorial, kivy android, kivy android tutorial, python compile android, android compile, kivy compile android
Id: 6gNpSuE01qE
Channel Id: undefined
Length: 26min 26sec (1586 seconds)
Published: Sat Jul 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.