How to make a button open a new activity | How to change to a new activity in Android Studio

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what is going on everyone in this video what i'd like to do is show you guys how you can change to a new activity by clicking a button it's a lot easier than you might think so let's get started before i started recording i created a new project and all we have is the main activity that android studio creates when you start up a new project now the very first thing that i'd like to do is come over to our resources folder come down to values and strings and within here i'd like to define a few strings so we can differentiate between the two activities so right now we have hello world but i'd like this to say activity1 and then create another activity that says activity2 so we know that we actually changed to a new activity to do that you're just going to use the string tag we're going to give it a name of activity1 and then we'll just call it activity1 and we're going to do the same exact thing for activity2 but we're going to change it from activity1 to obviously activity2 so now that we have our two strings we can come back to our xml file click on this text view and then come all the way over to this text slot right here instead of having to say hello world we're going to reference our string resource and then click activity 1 because right now we have activity 1 loaded up hit enter and then this will change to activity 1 so let's make this a little bit more readable if we click it again we can come all the way down to common attributes and we're able to change a few things about it to start off i'd like to make it bold and then we can increase the font size from 14 sp all the way up to 36. so now it's a little bit more readable and then i'd also like to change the color from black to blue let's go with this blue right here and then the next thing that we're going to have to do is add in a button so we're able to swap over to activity 2. so we can click this little button here and drag one in we're going to give it a few constraints to the left and right and we'll contribute to the bottom and give it a constraint all the way up to our text view right here and maybe we'll click this guy bring him up a little bit cool and then we're going to come back over to our strings resource and then instead of having to say button we'll just have it say change activity so again we're going to use our string tag name it button and then the text is just going to be change activity you can name it whatever you want but this is what i'm going to go with now if we come back to our xml click our button go all the way over to text and then reference our string resource and then come all the way down to button and then this will change to change activity all right so it looks like we have our first activity all set up now what we have to do is create our second activity to do that come all the way over to the left hand panel and right now i have this values folder selected what you're going to want to do is make sure you have this java folder selected then we're going to come up to the top left hand corner click file new and then come all the way down to activity and then within here we want to create a new empty activity so click that and we're going to name it new activity and then we'll come all the way down and click finish all right so now we have this new activity and we're going to want to do the same exact thing that we did to our activity main.xml file we want to add this little text view so we can differentiate between the two activities to do that we're going to grab this text view drag it in add some constraints to the top the bottom [Music] left and right and then remember we already created a string within our strings resource folder this guy right here so we're going to come back to our new activity.xml file come over to text and then we're going to reference the string resource again and then we're going to click activity2 come all the way down to the attributes and we're going to change the font size to 36 the color to let's give it red and then we'll make it bold as well so now this is what our second activity looks like and this is what our first activity looks like so now all we have to do is add some code into our main activity.java file so that when the user presses our button it'll swap over to activity2 the new one that we just created all we have to do now is link up the button that we created in our xml file to do that we're going to reference the button class we're going to call it change activity button and we're going to set that equal to find view by id so this is going to find the view that we added into our xml file and we're going to r dot id and now i'm not sure what we had as an id for our button so if we click it come all the way over to our attributes panel the very first slot will give us the id of that view so in our case the id is button which is a default so i'm just going to keep it as that so we go r.id dot button which will reference the button that we added in our xml file and that statement and now what we have to do is create an on click listener for this button right here to do that we have to go change activity button dot set on click listener so what this does is it just listens for when the user presses the button and then within the parentheses here we're gonna go new view dot on click listener this very first option right here hit enter and it's going to fill in a bunch of code for you now within here this is the code that you want to be executed when the user actually presses the button so i'm going to write a function that will be executed when the user presses a button we're going to call it change activity and that so now we have to create this function right here we can come down here we'll make it private void because we don't want to return anything and then we'll just call it our change activity and then within here this is where we're going to actually add the code that swaps over to activity 2. the way we go about doing that is by using the intent class so we're going to reference that we're going to call it intent i want to set that equal to a new intent now within here we're going to reference this instance of the class and then we want to change over to new activity so we go about doing that is by typing in new activity dot class and this statement and then down here all we have to do is use this little function here it's called start activity so we're just going to go with the first one and then all we have to do is pass in our intent which will flop over to new activity and that should be all you need to swap over to the new activity that we created so i'm going to come up to the top right hand corner click this little play button and actually test to make sure that this actually works all right so the app finally loaded up and you can see we have our first activity right here if we come down and click the change activity button you'll see that it changes over to activity 2. and we can go back to the first activity by clicking the back button here and we can keep going back and forth alright so that's it for this video as always if you guys have any questions or if you're just having trouble getting something to work leave a comment down below and i'll be sure to help you out don't forget to like and subscribe thanks for watching and i'll see you guys in the next video
Info
Channel: Practical Coding
Views: 2,027
Rating: 5 out of 5
Keywords: Change to a New Activity by Clicking a Button, How to change to a new activity in Android Studio, how to open a new activity, change activity, switch activity android studio, change to new activity, how to open a new activity in android studio, android studio, app development, practical coding, android studio basics, into to android studio, Intents in android studio, how to, new activity, android studio activity, beginners tutorial
Id: wIbUdv9o4z0
Channel Id: undefined
Length: 6min 38sec (398 seconds)
Published: Tue Sep 15 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.