Uploaded February 2013 | Updated November 2026, 3 hours ago
Updated version of video using Android Studios found here: youtu.be/ToOX2OTCYAk
Guide/demonstration to switching between activities in an Android program.
Steps listed below in the description.
Link to previous demo for creating a button:
youtube.com/watch?v=Uen8B9p05tI
Steps:
==========
1. Create the layout
- Create a new layout file under res/layout
- Named: second.xml
- Make it show something new (text field, icon...)
2. Create new Java class
- Create a new Java class: SecondScreen
- Override the onCreate():
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
3. Add activity to manifest
- Application tab, add new "Application Nodes"
- Add, "Create a new element at the TOP LEVEL", Activity
- Set name: Select java class from above.
- Set Label (if desired)
4. Java code to switch activities
---------------------------
startActivity(new Intent( [Current Activity's 'this'], [New Activity's Java].class));
Ex:
startActivity(new Intent(InClassDemoApp1.this, SecondScreen.class));
- Suggestion: Put launch code inside button callback:
Button daClicker = (Button) findViewById(R.id.button1);
daClicker.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SecondScreen.class));
}
});
5. Activity Stack
=====================
- Loading an Activity are pushed on the Activity *Stack*
- Activity's finish() method pops it
Returns to previous activity on stack.
- Must use correctly!
- Can move on and kill current activity:
startActivity(...);
finish();
6. Troubleshooting:
---------------------------
- Demonstrate what happens with the activity being in the manifest.
* View LogCat via DDMS:
...android.content.ActivityNotFoundException: Unable to find explicit activity class ...; have you declared this activity in your AndroidManifest.xml?
Updated version of video using Android Studios found here: youtu.be/ToOX2OTCYAk
Guide/demonstration to switching between activities in an Android program.
Steps listed below in the description.
Link to previous demo for creating a button:
youtube.com/watch?v=Uen8B9p05tI
Steps:
==========
1. Create the layout
- Create a new layout file under res/layout
- Named: second.xml
- Make it show something new (text field, icon...)
2. Create new Java class
- Create a new Java class: SecondScreen
- Override the onCreate():
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
3. Add activity to manifest
- Application tab, add new "Application Nodes"
- Add, "Create a new element at the TOP LEVEL", Activity
- Set name: Select java class from above.
- Set Label (if desired)
4. Java code to switch activities
---------------------------
startActivity(new Intent( [Current Activity's 'this'], [New Activity's Java].class));
Ex:
startActivity(new Intent(InClassDemoApp1.this, SecondScreen.class));
- Suggestion: Put launch code inside button callback:
Button daClicker = (Button) findViewById(R.id.button1);
daClicker.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SecondScreen.class));
}
});
5. Activity Stack
=====================
- Loading an Activity are pushed on the Activity *Stack*
- Activity's finish() method pops it
Returns to previous activity on stack.
- Must use correctly!
- Can move on and kill current activity:
startActivity(...);
finish();
6. Troubleshooting:
---------------------------
- Demonstrate what happens with the activity being in the manifest.
* View LogCat via DDMS:
...android.content.ActivityNotFoundException: Unable to find explicit activity class ...; have you declared this activity in your AndroidManifest.xml?








![Download XML from a Web Server: Android Programming
Tutorial showing how to download XML data from a sample Google AppEngine web server (http://demoprojectserver1234.appspot.com/) in an Android project. Full Android code below.
// imports omitted due to YouTube space restriction!
// Some code based on content in Android Application Development in 24 hours by Darcey and Conder (2nd ed).
/**
* Demonstrate querying a basic application server for information.
*
*/
public class MainActivity extends Activity {
private static final String TAG = ProjectServerDemo;
public static final String APP_ID = 3fa19507-c746-4dfa-8ded-ec60ef4d30d9;
public static final String SERVER_URL = http://demoprojectserver1234.appspot.com/;
public static final String QUERY_FILE = xmlquery.cgi;
public static final String QUERY_OPTIONS = ?appid= + APP_ID;
public static final String QUERY_URL = SERVER_URL + QUERY_FILE + QUERY_OPTIONS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick_ClearDisplay(View v) {
TextView textView = (TextView) findViewById(R.id.displayData);
textView.setText();
}
public void onClick_QueryServer(View v) {
Log.i(TAG, Query server...);
AsyncDownloader downloader = new AsyncDownloader();
downloader.execute();
}
private void handleNewRecord(String itemId, String data) {
TextView textView = (TextView) findViewById(R.id.displayData);
String message =
textView.getText().toString() + n +
itemId + : + data;
textView.setText(message);
}
// Inner class for doing background download.
// REVISIT:- Change the [[ to a less than; ]] to a greater than.
// (YouTube wont let you use those characters...)
private class AsyncDownloader extends AsyncTask[[Object, String, Integer]] {
@Override
protected Integer doInBackground(Object... arg0) {
XmlPullParser receivedData = tryDownloadingXmlData();
int recordsFound = tryParsingXmlData(receivedData);
return recordsFound;
}
private XmlPullParser tryDownloadingXmlData() {
try {
Log.i(TAG, Now downloading...);
URL xmlUrl = new URL(QUERY_URL);
XmlPullParser receivedData = XmlPullParserFactory.newInstance().newPullParser();
receivedData.setInput(xmlUrl.openStream(), null);
return receivedData;
} catch (XmlPullParserException e) {
Log.e(TAG, XmlPullParserExecption, e);
} catch (IOException e) {
Log.e(TAG, XmlPullParserExecption, e);
}
return null;
}
private int tryParsingXmlData(XmlPullParser receivedData) {
if (receivedData != null) {
try {
return processReceivedData(receivedData);
} catch (XmlPullParserException e) {
Log.e(TAG, Pull Parser failure, e);
} catch (IOException e) {
Log.e(TAG, IO Exception parsing XML, e);
}
}
return 0;
}
private int processReceivedData(XmlPullParser xmlData) throws XmlPullParserException, IOException {
int recordsFound = 0;
// Find values in the XML records
String appId = ; // Attributes
String itemId = ;
String timeStamp = ;
String data = ; // Text
int eventType = -1;
while (eventType != XmlResourceParser.END_DOCUMENT) {
String tagName = xmlData.getName();
switch (eventType) {
case XmlResourceParser.START_TAG:
// Start of a record, so pull values encoded as attributes.
if (tagName.equals(record)) {
appId = xmlData.getAttributeValue(null, appid);
itemId = xmlData.getAttributeValue(null, itemid);
timeStamp = xmlData.getAttributeValue(null, timestamp);
data = ;
}
break;
// Grab data text (very simple processing)
// NOTE: This could be full XML data to process.
case XmlResourceParser.TEXT:
data += xmlData.getText();
break;
case XmlPullParser.END_TAG:
if (tagName.equals(record)) {
recordsFound++;
publishProgress(appId, itemId, data, timeStamp);
}
break;
}
eventType = xmlData.next();
}
// Handle no data available: Publish an empty event.
if (recordsFound 0) {
publishProgress();
}
Log.i(TAG, Finished processing +recordsFound+ records.);
return recordsFound;
}
@Override
protected void onProgressUpdate(String... values) {
if (values.length 0) {
Log.i(TAG, No data downloaded);
}
if (values.length 4) {
String appId = values[0];
String itemId = values[1];
String data = values[2];
String timeStamp = values[3];
// Log it
Log.i(TAG, AppID: + appId + , Timestamp: + timeStamp);
Log.i(TAG, ItemID: + itemId + , Data: + data);
// Pass it to the application
handleNewRecord(itemId, data);
}
super.onProgressUpdate(values);
}
}
} Download XML from a Web Server: Android Programming](https://i.ytimg.com/vi/kJY4SJ3IgGk/mqdefault.jpg)

