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.
_________________________