Uploaded February 2013 | Updated November 2026, 3 hours ago
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
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


![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)





![Run Java from Jar: IntelliJ Terminal
How to run a Java program from IntelliJs built-in command line by using a Jar. Demos importing GSON library as an external Maven dependency to show power of a Jar file.
1. New project MyProjectName
Create package
Create MyMain class with main()
2. Setup GSON object
File Project Structure
Libraries (on left)
Click the green +
Select From Maven
Enter: com/google/code/gson:gson:2.8.2
3. Basic main()
public static void main(String[] args) {
// Use GSON:
Gson gson = new Gson();
for (String s : args) {
System.out.println(Args: + s);
}
}
RUN via IntelliJ:
3. Setup JAR
file project structure
artifacts (on left)
green + for new artifact
jar from modules with dependencies
pick the module to use [default likely OK]
choose the main class
4. Command line
java -jar out/artifacts/MyProjectName_jar/MyProjectName.jar Hello world! Run Java from Jar: IntelliJ Terminal](https://i.ytimg.com/vi/YwohEBqglY0/mqdefault.jpg)

