How to Make a Button Open a New Activity - Android Studio Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hey guys. In this tutorial, we are going to learn how to open a new activity when clicking a button For this, we start a new Android Studio project We keep the name as it is and click "Next" We keep the "Minimum SDK" as well And we choose "Empty Activity" Here we keep the name as well and click "Finish" Now of course, we need a second activity. So we right-click on our package New->Activity->Empty Activity We call the name "Activity2" And click "Finish" Now, let's prepare the layout for our MainActivity We change this ConstraintLayout to a RelativeLayout And let's change this TextView to "Activity 1" We can also delete these constrains here And we put this text in the middle Let's copy this layout for our second activity Go to Activity2 Paste it here And we change the text to "Activity 2" So we always see which activity we are in currently Back to our MainActivity Because now of course, we also have to add a button Which brings us to our Activity2 So we add a Button. wrap_content width and height We set the text to: "open activity 2" And it also needs an ID. So we write id +id And we set the ID to simply "button" And let's put this button under this TextView here Okay, nice. Back to our MainActivity.java file First, we need to prepare a variable for our button. So we write: private Button button; And down here in our onCreate() method we are going to initialize this button We say: button = findViewById(R.id.button) Which is our XML id for this button Semicolon.. and of course we also have to cast it to a Button here Nice. Next we set an onClickListener() for this button We write button.setOnClickListener(new View.OnClickListener()) And in this OnClick() method we are going to call openActivity2(); And we create this method down here We write: public void openActivity2() And in here we are going to open our second activity And this is very simple process. We simply create an Intent with Intent intent = new Intent() We pass the context, which is "this" And we pass the class we want to open Which is Activity2.class And in the next line we write: startActivity() and we pass the intent we just created And that's basically the whole process This part here will open the second activity. Let's test it Okay, as you can see here we are in Activity 1 currently Now we click our button And we changed to Activity2 When we click the back button, we get back to Activity 1 And if you click again, we go back to Activity 2 I hope this was helpful. Take care
Info
Channel: Coding in Flow
Views: 907,651
Rating: 4.8771701 out of 5
Keywords: 2017, android, android apps, android apps programming, android development, android development for beginners, android studio, android studio tutorial, android tutorial, apps, apps programming, beginners, coding, development, how to make android apps, how to make apps, learn to code, learn to program, software development, tutorial, 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, fragment
Id: bgIUdb-7Rqo
Channel Id: undefined
Length: 3min 22sec (202 seconds)
Published: Wed Oct 11 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.