How to Open a New Activity With a Button in Android Studio [Kotlin 2020]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to new mini tutorial on how to create a new activity and how to navigate from one activity to a different activity so if you have a button and one activity and if you click on that it will take you to a new activity as you can see it took us to activity b and also i have included a back button in the action bar where it will take us to the previous or the parent activity so as you can see it took us to the previous activity which is activity a so if this is what you are looking for don't worry i've got your back and let's get start on coding alright guys so once you have created your new project you will end up having with two files the first one is the activity main.xml this is where you will create your layout and mainactivity.kotlin this is where you put all of your logic so this is your first activity and now you need to create your second activity so that you can navigate between them so go to your applications right click on that click on new and then go to activity and then click on empty activity and in here name your activity which is second activity and click on finish alright so as you can see now we have two more files for the second activity so let's close these for a moment and now you need to go to your first activity and this is where you will create your button so split the screen so that you write your code here and all you need to do change this to button and now you need to change this text to second activity and do not forget to provide an id so that we can access it in mainactivity.com and the id would be second activity button all right so once you have done that you need to go to mainactivity.collin so that we set onclicklistener on this button and you need to set on click listener for the button and uncreate methods so second activity button and then set on click listener so in onclicklistener you need to redirect the activity from main activity to second activity so we need to create a nintendo object so val intend and that would equal to intent so in the intent constructor it will take two arguments the first one is the package context so this and the second argument is the second activity class so second activity and then we have class.java so once you have the object ready you need to start a new activity and then pass the intent object to it and this will take us from this activity to the second activity so let's run our app to see the result all right so as you can see we are in the first activity and we have the button so if we click on this button it should take us to the second activity and as you can see it took us to the second activity but we cannot go back to the previous activity because we have not enabled the back button in the action bar and we can also change this action bar title to the second activity so let's do that alright so you need to go to the second activity because this is where we will put the back button in the action bar and the oncreate method so you need to create an action bar object so valve action bar and then call on support action bar and so once you have done that you need to check if the action button is not null and you can do that in two ways it's either you have an if statement action bar and then this will not equal to not and then you can go to actionbar.title and then set the title to secondactivity and also you can do it in a different way rather than writing an if statement you can call the action bar and then put double exclamation before calling the title and then you can set this to second activity and i believe this is much cleaner so we will keep that alright so once you have done that you need to enable the back button so have the object ready and then call on set display home as up enabled and then pass through to it so that you have the back button so if you run our app to see the result and let's go to the second activity as you can see we have second activity and we have the back button so if you click on that it will not take us to the previous activity because we need to identify the patent of this activity so we need to go to projects and then apps and then go to manifest and then open android manifest and in here you can see your activities and we are looking for the second activity so right before the closing tag you can call patent activity name and the pattern for the second activity is the previous activity which is the main activity so click on that and that should do the job so if you run our app one more time so if you click on this button it will take us to the second activity and if you click on this back button it will take us to the previous activity and you can also change this action bar title to first activity and you can just copy this from second activity and go to the main activity and in main activity you can paste it here and change this to first activity and if you run your app one more time as you can see we have first activity and then we can navigate to the second activity and then go back to the first activity alright guys we have done it if this is your first time here and you want to learn on how to build mobile applications web development and programming tutorials in general please consider subscribing and click on the bell notification so that you do not miss on anything and see you guys in the next video and happy coding and see you guys in the next video and happy coding
Info
Channel: CodeWithMazn
Views: 22,366
Rating: 4.8898306 out of 5
Keywords: how to open a new activity, android studio how to make a button open a new activity, how to open a new activity from button click, intent, activity, android, how to make a button open a new activity in android studio, open new activity with button android, open new activity on button click android studio, kotlin open new activity, open new activity kotlin, kotlin intent activity, android button onclick open new layout, android change activity, switch activity android studio, Kotlin, Java
Id: 4bEZ2PlIo8g
Channel Id: undefined
Length: 5min 47sec (347 seconds)
Published: Sat Aug 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.