Add a Second Activity using Intent in Android Studio

Reading Time: 2 minutes

When you want to add a second activity in Android Studio, perhaps best way is to do it by using Intent class. Once when you made your first android app as single activity apk, next natural thing is that you want to expand your android application. Good practice is to start your new many activity application as blank activity, and to make other activities as Google AdMob Ads Activity. On that way, first activity would have more space to put some control buttons to it, while later activities would have ads inserted. So late start our blank activity first:

Initial-Blank-Activity-for-Activities

Then you can get rid of the email icon at the bottom of your activity

Remove-email-icon

Just go to app→res→layout→activity_main.xml and delete four lines of code as indicated in figure bellow.

Remove-email-icon02

Also, go to app→java→cirvirlab.activities→MainActivity.java and delete lines of code as indicated in figure bellow.

Remove-email-icon03

Next thing to do is to right click on MainActivity.java app→java→cirvirlab.activities→MainActivity.java and add new activity

Add-google-Ad-Activity-as-second-Activity

Advise is to choose Google AdMob Ads Activity so that we have some Google Ads on our second (and later) activities.

Add google Ad Activity as second Activity

Add-google-Ad-Activity-as-second-Activity03

This action will make necessary changes app→manifests→AndroidManifest.xml
Arrange you new activity like any other Google AdMob Ads Activity. Then put one button on your main activity so that you can navigate to second activity. Declare onClik event in text view of content_main.xml

<Button

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”SECOND ACTIVITY”

android:id=”@+id/button”

android:onClick=”openSecond”

android:layout_alignParentTop=”true”

android:layout_centerHorizontal=”true”

android:layout_marginTop=”131dp” />

</RelativeLayout>

Also, put following code in MainActivity.java file:

public void openSecond(View view)
{

Button button = (Button)view;

startActivity(new Intent(getApplicationContext(),Activity2.class));

}
This slice of code shows true power of using Intent class. Second activity appears when you press button.

Screenshot_2015-11-24-15-19-42

External links:

Add a Second Activity by using Intent in Android Studio on developer.android.com

Tagged with: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*