Uploaded February 2014 | Updated November 2026, 3 hours ago
Demonstrate passing a value to an activity when it is created, and passing a value back (returning) when it finishes. Complete source code available at github.com/drbfraser/video-tutorial-code/tree/master/ExtrasDemo
Passing To an Activity:
-----------------------
Pass data to an activity from MainActivity:
Intent intent = new Intent(MainActivity.this, DisplayActivity.class);
intent.putExtra("my parameter name", 42);
startActivity(intent);
Getting value in second activity's onCreate():
Intent intent = getIntent();
int myValue = intent.getIntExtra("my parameter name", 0);
// 0 is the default value if the parameter not set.
Returning a value:
------------------
Change calling code (1st activity) to use startActivityForResult():
Intent intent = new Intent(MainActivity.this, DisplayActivity.class);
intent.putExtra("my parameter name", 42);
startActivityForResult(intent, 12345);
// 12345 is a code to myself to identify which call is returning.
Set data in the called activity (2nd activity):
Intent returnData = new Intent();
returnData.putExtra("my answer", 24);
setResult(Activity.RESULT_OK, returnData);
// use Activity.RESULT_CANCELED to indicate failure.
finish();
Catch the return value in 1st activity's onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 12345:
if (resultCode == Activity.RESULT_OK) {
int answer = data.getIntExtra("my answer", 0);
// 0 is default value if none was specified
// Do something with the answer...
break;
}
}
Demonstrate passing a value to an activity when it is created, and passing a value back (returning) when it finishes. Complete source code available at github.com/drbfraser/video-tutorial-code/tree/master/ExtrasDemo
Passing To an Activity:
-----------------------
Pass data to an activity from MainActivity:
Intent intent = new Intent(MainActivity.this, DisplayActivity.class);
intent.putExtra("my parameter name", 42);
startActivity(intent);
Getting value in second activity's onCreate():
Intent intent = getIntent();
int myValue = intent.getIntExtra("my parameter name", 0);
// 0 is the default value if the parameter not set.
Returning a value:
------------------
Change calling code (1st activity) to use startActivityForResult():
Intent intent = new Intent(MainActivity.this, DisplayActivity.class);
intent.putExtra("my parameter name", 42);
startActivityForResult(intent, 12345);
// 12345 is a code to myself to identify which call is returning.
Set data in the called activity (2nd activity):
Intent returnData = new Intent();
returnData.putExtra("my answer", 24);
setResult(Activity.RESULT_OK, returnData);
// use Activity.RESULT_CANCELED to indicate failure.
finish();
Catch the return value in 1st activity's onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 12345:
if (resultCode == Activity.RESULT_OK) {
int answer = data.getIntExtra("my answer", 0);
// 0 is default value if none was specified
// Do something with the answer...
break;
}
}



![Button Tutorial: Android Programming
Demonstration on how to create a button in an Android application. Walks through creating the button and writing the Java code to make it interactive.
Steps and Java code included below in this description.
Steps
1. Create the button on the UI (xml).
2. Get a reference to the button (java)
3. Register a listener (java) [code below]:
myButton.setOnClickListener(...)
Our listener is an anonymous class implementing:
View.OnClickListener
We implement its only function: onClick()
(this is where we put our code).
Aside:
Toast: a message that pops up on the screen.
Java Code:
public class MainActivity extends Activity {
private final String TAG = DemoButtonApp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupMessageButton();
}
private void setupMessageButton() {
// 1. Get a reference to the button.
Button messageButton = (Button) findViewById(R.id.btnDisplayMessage);
// 2. Set the click listener to run my code.
View.OnClickListener myListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, You clicked the button!);
Toast.makeText(
MainActivity.this,
You clicked it!,
Toast.LENGTH_LONG
).show();
}
};
messageButton.setOnClickListener(myListener);
}
}
Demo created using the Android Development Tools, build v21.0.0-519525 Button Tutorial: Android Programming](https://i.ytimg.com/vi/Uen8B9p05tI/mqdefault.jpg)


![Post changes to AppServer: Android Programming
Demonstration on posting data to a application server using Android. Server is a sample Google AppEngine web server (http://demoprojectserver1234.appspot.com/). Full code below. Previous tutorial: http://www.youtube.com/watch?v=kJY4SJ3IgGk
package ca.demo.projectserverviewer;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
* Simple activity to demonstrate sending data to the app-server.
*/
public class SimplePostActivity extends Activity {
private static final String TAG = PostActivity;
public static final String SERVER_URL = http://demoprojectserver1234.appspot.com/;
public static final String POST_FILE = post.cgi;
public static final String POST_URL = SERVER_URL + POST_FILE;
private static final String POST_OPTION_APPID = appid;
private static final String POST_OPTION_ITEMID = itemid;
private static final String POST_OPTION_DATA = data;
private static final String MY_APPID = 3fa19507-c746-4dfa-8ded-ec60ef4d30d9;
private static final String MY_ITEMID = 112233;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_post);
}
public void onClick_PostData(View v) {
TextView textView = (TextView) findViewById(R.id.txtDataEntry);
String data = textView.getText().toString();
postDataToServer(MY_APPID, MY_ITEMID, data);
// String uniqueItemId = UUID.randomUUID().toString();
// postDataToServer(MY_APPID, uniqueItemId, data);
}
// Post Changes to Server
// Source: http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient
private void postDataToServer(String appId, String itemId, String data) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(POST_URL);
try {
// Setup data:
// TODO:- Change the [[ and ]] to the less-than and greater-than (YouTube limit)
List[[NameValuePair]] nameValuePairs = new ArrayList[[NameValuePair]]();
nameValuePairs.add(new BasicNameValuePair(POST_OPTION_APPID, appId));
nameValuePairs.add(new BasicNameValuePair(POST_OPTION_ITEMID, itemId));
nameValuePairs.add(new BasicNameValuePair(POST_OPTION_DATA, data));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post
HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode();
Log.i(TAG, Post request finished with a status of: + status);
Toast.makeText(this, Post completed w/ status: +status, Toast.LENGTH_LONG).show();
} catch (ClientProtocolException e) {
Log.e(TAG, Unable to post (protocol): , e);
} catch (IOException e) {
Log.e(TAG, Unable to post (IOException): , e);
}
}
} Post changes to AppServer: Android Programming](https://i.ytimg.com/vi/VunA-CJZ10U/mqdefault.jpg)



