🔒 Closed HOW TO CREATE AN ANDROID APP WITH ANDROID STUDIO ? Part-2

Status
Not open for further replies.

winz001

Eternal Poster

Step 6: Write the Button's 'onClick' Method

1.Select the MainActivity.java tab along the top of the work environment.

2. Add the following lines of code at the end of the onCreate method:

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.onClickListener() {
Override
public void onClick(View v) {
goToSecondActivity();
}
});
3. Add the following method to the bottom of the MainActivity class:
private void goToSecondActivity() {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}

4. Click the + next to import at the third line of MainActivity.java to expand the import statements.

5. Add the following to the end of the import statements if they are not already there:

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

Step 7: Test the Application

Click the green play symbol from the toolbar at the top of the Android Studio window.

When the 'Choose Device' dialog apperas (this may take a few moments), select the 'Lauch emulator' option.

Click OK.

When the emulator opens (this too could take awhile), the app will automatically launch the app upon the virtual phone being unlocked.

Make sure that all of your text displays correctly and that the button takes you to the next page.

Step 8: Up, Up, and Away!

Congrats! You've now completed your first Android application with some basic functionality.

Your finished app should have a page greeting the user and a button that takes the user to a second page.

_________________________
 
Status
Not open for further replies.

About this Thread

  • 2
    Replies
  • 745
    Views
  • 3
    Participants
Last reply from:
Ace Portgas

Trending Topics

Online now

Members online
820
Guests online
4,196
Total visitors
5,016

Forum statistics

Threads
2,313,806
Posts
29,175,483
Members
1,183,909
Latest member
Zens_101
Back
Top