Getting Started in Android Development Part 12 - Starting an new Activity with an Intent
Home Tutorials Hints & Tips About

Android Apps and Training

Share on Facebook Share on Twitter

Getting Started in Android Development

Part 12 - Starting a new Activity with an Intent

Starting a new Activity from another Activity requires a few steps. As in our video example, you first need to create a new Java class and layout for the new activity. In Android Studio simply right click over the package name in the navigation pane and select “New” -> “Java Class”.  

Creating a new Java Class in Android Studio


To create a layout, right click over the “layout” folder under the “res” folder and select “New” -> “Layout resource file”.

Creating a new Android Layout File in Android Studio

The contents of the layout file are not covered in this tutorial, but make sure that the java file has setContentView associated with the layout file within the onCreate method. A list of override methods can be called by selecting ctrl+o in Android Studio. The new java file should look similar the one below. The layout for our example is called activity_listview.xml and will be covered in more detail in the next tutorial.

The next task is to reference the new java class in the Android Manifest file. Essentially, the existing XML code within the <activity> tags can be copied and pasted and the relevant changes made. The name needs to be changed to the java class name. Our example is shown below where the new java class is called “ListviewActivity.java”. Remember to include the “dot” preceding the class name.

android:name=".ListviewActivity"

The “action” tag within the intent filter needs to be changed to a unique string that will be used when we wish the start the new activity. The string and be any unique string. Using the package name means it will be globally unique after the app is published. However, the package name does not have to be used. Our example is shown below:

<action android:name="com.nbstutorials.tutorialsset1.LISTVIEW" />

Since this Activity is not loaded on start-up, the intent filter category needs to be changed from LAUCHER to DEFAULT. More about intents and intent filters can be found here.

After this tutorial, your AndroidManifest.xml file should look similar to the one below.

The final task is to turn your attention to adding java code to the Activity from where you want to call the new Activity. This is done by creating an Intent with the action name string from the Intent filter and the starting the activity with the intent’s variable name.  Our example code is below:

Intent intent1 = new Intent ("com.nbstutorials.tutorialsset1.LISTVIEW");

startActivity(intent1);

In our example, these two lines of code are placed in the “launch button” case statement in the MainActivity.java file.

The process in now complete and the new Activity will be started when the launch button is clicked. After this tutorial your MainActivity java file should look similar to the one below.


Download Download tutorial set 1


© 2015 North Border Software All rights reserved
Privacy and Cookies Terms of Use Disclaimer Copyright
North Border Software Logo

North Border Software