Uploaded February 2013 | Updated November 2026, 3 hours ago
Demonstration/guide on how to use custom plain-old Java objects inside an Activity.
Java code and outline are in this description (below).
Ideas:
---------------------------------------------
- Separate the UI from the Business Logic (BL)
- BL plain java
- UI code (Android) calls BL objects.
Steps:
---------------------------------------------
1. Design and implement BL (Java classes).
2. Instantiate BL object in UI code.
3. Call BL object from UI code (client)
enum:
---------------------------------------------
A set of possible values.
Example:
public enum Emotion {TIRED, HAPPY, SAD};
Example:
---------------------------------------------
PetRock: Generate get/set
public class PetRock {
public enum Emotion {TIRED, HAPPY, SAD};
private Emotion currentEmotion = Emotion.TIRED;
public Emotion getCurrentEmotion() {
return currentEmotion;
}
public void setCurrentEmotion(Emotion currentEmotion) {
this.currentEmotion = currentEmotion;
}
@Override
public String toString() {
return "I'm feeling " + currentEmotion.toString() + ".";
}
}
UI:
- Label for status
- 2 Buttons for Happy/Sad to change state and update UI
Function to setup each button, then refactor to one function.
public class MainActivity extends Activity {
private PetRock rocky = new PetRock();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupEmotionButton(R.id.btnHappy, Emotion.HAPPY);
setupEmotionButton(R.id.btnSad, Emotion.SAD);
updateUI();
}
private void setupEmotionButton(int buttonId, final Emotion newEmotion) {
Button button = (Button) findViewById(buttonId);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
rocky.setCurrentEmotion(newEmotion);
updateUI();
}
});
}
private void updateUI() {
TextView textView = (TextView) findViewById(R.id.txtRockyFeeling);
String feeling = rocky.toString();
textView.setText(feeling);
}
}
Demonstration/guide on how to use custom plain-old Java objects inside an Activity.
Java code and outline are in this description (below).
Ideas:
---------------------------------------------
- Separate the UI from the Business Logic (BL)
- BL plain java
- UI code (Android) calls BL objects.
Steps:
---------------------------------------------
1. Design and implement BL (Java classes).
2. Instantiate BL object in UI code.
3. Call BL object from UI code (client)
enum:
---------------------------------------------
A set of possible values.
Example:
public enum Emotion {TIRED, HAPPY, SAD};
Example:
---------------------------------------------
PetRock: Generate get/set
public class PetRock {
public enum Emotion {TIRED, HAPPY, SAD};
private Emotion currentEmotion = Emotion.TIRED;
public Emotion getCurrentEmotion() {
return currentEmotion;
}
public void setCurrentEmotion(Emotion currentEmotion) {
this.currentEmotion = currentEmotion;
}
@Override
public String toString() {
return "I'm feeling " + currentEmotion.toString() + ".";
}
}
UI:
- Label for status
- 2 Buttons for Happy/Sad to change state and update UI
Function to setup each button, then refactor to one function.
public class MainActivity extends Activity {
private PetRock rocky = new PetRock();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupEmotionButton(R.id.btnHappy, Emotion.HAPPY);
setupEmotionButton(R.id.btnSad, Emotion.SAD);
updateUI();
}
private void setupEmotionButton(int buttonId, final Emotion newEmotion) {
Button button = (Button) findViewById(buttonId);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
rocky.setCurrentEmotion(newEmotion);
updateUI();
}
});
}
private void updateUI() {
TextView textView = (TextView) findViewById(R.id.txtRockyFeeling);
String feeling = rocky.toString();
textView.setText(feeling);
}
}








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

