Simple Kotlin App | Android Studio | 2024

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is going on guys welcome back to another Android application development video in Android Studio in this video we are going to learn how to create our first application with the kotlin till now we were developing our application using Java but now we will start developing our application with kotlin which is the official language for the Android it is not very different from java we are familiar with the Java now we will get familiar with the kotlin so let's start our new project so here I have updated Android Studio so my Android Studio is now latest that is giraffe version so let's click on new project so previously we used to select empty activity from now on we will select empty views activity because this empty activity is for Jetpack composable so we are not using that we are using views so we will click on empty views Activity Click on next so here you can give the application name so application name will be my kotlin app package name you can give anything and most importantly language is pre-selected that is kotlin so we are selecting Java in previous videos now we will select kotlin it is already selected minimum SDK you can choose anyone let's choose nougat and build configuration Let It Be recommended one so click on finish so as you can see our project is loaded and we have this mainactivity.kt previously we used to have main activity.java now it is KT extension that means we are using cotin so activity main.xml will be as it is click on split this is as it is and this will be main activity.kt here also we have oncreate function all the things are same just the syntax are different so let's run this application and see so as you can see our application is launched and we have this hello world over here now let's add some views so go to activity main.xml and we will Design so for this I will remove this text View let's remove it you can use any layout but I will use for now linear layout and orientation let's say vertical let's quickly design a simple app where we will take the edit text width will be match parent height will be wrap content hint will be enter a I'll give the ID to it foreign let's give some margin as well about 20 DB let's copy this and create one more enter B and it will be enter B so we have to edit text over here let's give margin to this linear layout as well so we have to edit text now we will add some buttons and result text view so for buttons I will use another linear layout width will be match parent height will be wrap content orientation will be horizontal and I will add four buttons so buttons will be wrap content wrap content text addition ID will be button add let's give some margin about 4 DB let's copy this button and paste it four times so we have these four buttons second button will be subtraction third button will be multiply and fourth button will be division so here button division ID will be button multiplication here it will be subtraction in this way we will calculate two numbers and we will show a result over here so after this we'll show text View width will be match parent height will be wrap content text will be for now let's say result text size will be 24 SP and gravity will be Center so as you can see over here we can see the result let's give margin so in this way we designed our simple application let's go to mainactivity.kt now so in mainactivity.kd previously in Java what we used to do we used to write here button button add and here what we used to do button add equals to find view by ID r dot ID dot ID so here in kotlin what we will do we have to first Define here with late in it where button add type will be button in this way we have defined it and here we can do same thing find view by d r dot ID dot button add so in this way we can do so let's copy this and paste it four times it will be button subtract button multiplication button division so in this way we can give ID for all those so it is the same thing what we were doing in Java let's initialize edit text also e t a edit text and ET B edit text and one text view result TV which will be text View now we have to implement on click listener to all of them so what we will do is inherit from on click listener so here what we will do we have already narrated this so comma it will be view dot on click listener let's import it we can do directly from here also like button add dot on click listener set on click listener but there are four button that's why we are doing here so that we can get the ID and Implement directly so let's Implement members that will be on click here we can use the switch case but there is no switch case in kotlin so we will use when p 0 that means parameter here we can say V when the V Dot ID when V Dot ID and then we will check so here view is nullable so we have to write question mark so that it will be null safe when we have ID if the ID is r dot ID Dot add then we'll do something if ID is subtraction then we'll do something if ID is multiplication then we'll do something per division so here we can write the body what will happen so to do the calculation what we will do first I will create two local variables that will be variable a equals to I will take the value from ETA that will ETA dot get text we have get text in Java but here we can directly write text Dot two string so it will get the value in a string if you hover over here you can see this is a string but we have to convert it to double so that we can calculate so 2 Double so we have the value of this edit text in the a that will be double value duplicate it make it B ETA will be ETA B so we'll get both the values now what we will do so we will also create one variable that will be result of type double or you can directly initialize it like 0 1 0 it will automatically take that it is double so let's go to add body here what we will do result equals to A plus b that's it let's go to subtraction result equal to a minus B for multiplication result equals to A multiply B foreign by B so in this way we have calculated the result and put it in result variable now what we have to do we have to show this result in this result text view so for that what we will do after this switch case that means when after this what we will do result text View dot in Java we have dot set text but here we can directly write text equals to result we can concatenate it like result is we can specify a dollar result so in this way we can see the result in result text View so if we summarize is what we have done is we have just initialized the variables all the views and we have given the ID but I have forgot to assign ID to these views let's do that also ETA equals to find view ID r dot ID dot ETA the same way e t b equals to B result TV equals to foreign I guess let's go to our view result ID will be result TV so here result TV so in this way we have assigned the ID as we do in Java and on click of any button we have assigned the values to A and B and then we have stored the value in result and in the result text here we have just displayed the result that's it let's run it and test it once so our application is running let's give the input as 10 and B as 5. let's click on plus first it is not working we click on minus multiply any of the button is not working why because we have not assigned on click listener so we have just implemented on click listener but we have not assigned it to any buttons so what we have to do is button add Dot set on click listener that will be this the same way assigned it to every button so in this way all the click listener will be assigned to this let's run it now if you enter 10 and 5 let's see if we click on plus result is 15 we have added we have subtraction we have multiplication and we have division all the answer are correct and our application is working fine so in this way we can create the kotlin project where the views will be same in kotlin some definition will be different some syntax will be different that's it otherwise it is very easy to learn we can learn it by making the project so now on we will be making projects in kotlin so that we will learn the kotlin gradually so that's all for this video If you enjoyed this video please hit the like button if you have any doubts and queries please comment below I will reply for you don't forget to hit the Subscribe button before going see you in the next video bye bye [Music]
Info
Channel: Easy Tuto
Views: 89,562
Rating: undefined out of 5
Keywords: easy tuto, android, development, beginners, android studio, kotlin, first app, first app with kotlin, kotlin app, how to make simple kotlin app, android girraffe, first kotlin app, android studio kotlin, learn kotlin
Id: XLt_moCoauw
Channel Id: undefined
Length: 12min 28sec (748 seconds)
Published: Sat Sep 09 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.