Uploaded February 2014 | Updated November 2026, 3 hours ago
Tutorial on dynamically creating Android Buttons in a TableView at run-time using Java code vs XML. Covers setting text and images in buttons, and onClickListener() for button press. Full project source code available at github.com/drbfraser/video-tutorial-code/tree/master/DynamicButtons
If button sizes change when adding text, try setting the text size when you initially populate the buttons, before locking their size.
Java Source Code:
*************************************************************
/**
* Demo on creating dynamic buttons in a table and
* adding background icons.
*/
public class MainActivity extends Activity {
private static final int NUM_ROWS = 7;
private static final int NUM_COLS = 10;
Button buttons[][] = new Button[NUM_ROWS][NUM_COLS];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateButtons();
}
private void populateButtons() {
TableLayout table = (TableLayout) findViewById(R.id.tableForButtons);
for (int row = 0; row != NUM_ROWS; row++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,
1.0f));
table.addView(tableRow);
for (int col = 0; col != NUM_COLS; col++){
final int FINAL_COL = col;
final int FINAL_ROW = row;
Button button = new Button(this);
button.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT,
1.0f));
button.setText("" + col + "," + row);
// Make text not clip on small buttons
button.setPadding(0, 0, 0, 0);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gridButtonClicked(FINAL_COL, FINAL_ROW);
}
});
tableRow.addView(button);
buttons[row][col] = button;
}
}
}
private void gridButtonClicked(int col, int row) {
Toast.makeText(this, "Button clicked: " + col + "," + row,
Toast.LENGTH_SHORT).show();
Button button = buttons[row][col];
// Lock Button Sizes:
lockButtonSizes();
// Does not scale image.
// button.setBackgroundResource(R.drawable.action_lock_pink);
// Scale image to button: Only works in JellyBean!
// Image from Crystal Clear icon set, under LGPL
// commons.wikimedia.org/wiki/Crystal_Clear
int newWidth = button.getWidth();
int newHeight = button.getHeight();
Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.action_lock_pink);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, true);
Resources resource = getResources();
button.setBackground(new BitmapDrawable(resource, scaledBitmap));
// Change text on button:
button.setText("" + col);
}
private void lockButtonSizes() {
for (int row = 0; row != NUM_ROWS; row++) {
for (int col = 0; col != NUM_COLS; col++) {
Button button = buttons[row][col];
int width = button.getWidth();
button.setMinWidth(width);
button.setMaxWidth(width);
int height = button.getHeight();
button.setMinHeight(height);
button.setMaxHeight(height);
}
}
}
}
Tutorial on dynamically creating Android Buttons in a TableView at run-time using Java code vs XML. Covers setting text and images in buttons, and onClickListener() for button press. Full project source code available at github.com/drbfraser/video-tutorial-code/tree/master/DynamicButtons
If button sizes change when adding text, try setting the text size when you initially populate the buttons, before locking their size.
Java Source Code:
*************************************************************
/**
* Demo on creating dynamic buttons in a table and
* adding background icons.
*/
public class MainActivity extends Activity {
private static final int NUM_ROWS = 7;
private static final int NUM_COLS = 10;
Button buttons[][] = new Button[NUM_ROWS][NUM_COLS];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateButtons();
}
private void populateButtons() {
TableLayout table = (TableLayout) findViewById(R.id.tableForButtons);
for (int row = 0; row != NUM_ROWS; row++) {
TableRow tableRow = new TableRow(this);
tableRow.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT,
1.0f));
table.addView(tableRow);
for (int col = 0; col != NUM_COLS; col++){
final int FINAL_COL = col;
final int FINAL_ROW = row;
Button button = new Button(this);
button.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT,
1.0f));
button.setText("" + col + "," + row);
// Make text not clip on small buttons
button.setPadding(0, 0, 0, 0);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gridButtonClicked(FINAL_COL, FINAL_ROW);
}
});
tableRow.addView(button);
buttons[row][col] = button;
}
}
}
private void gridButtonClicked(int col, int row) {
Toast.makeText(this, "Button clicked: " + col + "," + row,
Toast.LENGTH_SHORT).show();
Button button = buttons[row][col];
// Lock Button Sizes:
lockButtonSizes();
// Does not scale image.
// button.setBackgroundResource(R.drawable.action_lock_pink);
// Scale image to button: Only works in JellyBean!
// Image from Crystal Clear icon set, under LGPL
// commons.wikimedia.org/wiki/Crystal_Clear
int newWidth = button.getWidth();
int newHeight = button.getHeight();
Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.action_lock_pink);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, true);
Resources resource = getResources();
button.setBackground(new BitmapDrawable(resource, scaledBitmap));
// Change text on button:
button.setText("" + col);
}
private void lockButtonSizes() {
for (int row = 0; row != NUM_ROWS; row++) {
for (int col = 0; col != NUM_COLS; col++) {
Button button = buttons[row][col];
int width = button.getWidth();
button.setMinWidth(width);
button.setMaxWidth(width);
int height = button.getHeight();
button.setMinHeight(height);
button.setMaxHeight(height);
}
}
}
}




![Pass Data To Activity: Android Programming
Demo of passing data to an Activity using Intents. Shows passing primitive values, and then working with an object. Discusses it effectively being pass-by-value, whereas pass-by-reference not possible with Intents.
Previous video: Switching activities ( https://www.youtube.com/watch?v=B1oaE0Odqes )
1. Passing Primitives
* Change makeIntent() to pass in a String and int [name, age]
- Encode using myIntent.putExtra(Key, Value)
* Change onCreate() to extract data from intent
- Extract using myIntent.getStringExtra(Key)
2. Passing Object State
* Pass in object by value; encode parts; onCreate() constructs new object
- Can only encode primitives into Extras, so encode objects state into extra.
(Intent can launch separate application, so cannot pass object reference outside of your memory space).
- Alternatives: Parcelable, Serializable, or Gson;
3. Pass in object by ref:
- 2nd activity creates a new object with identical state but distinct from 1st activitys.
- Need to have a reference to the same object, but cannot put address in an Extra
- Use Singleton, Application class Pass Data To Activity: Android Programming](https://i.ytimg.com/vi/9OYNH067Xa0/mqdefault.jpg)



![Switching Activities in Android Studio: Android Programming
Create and launch a second Android activity. Covers Intent, startActivity(), finish(), and the activity stack.
For tutorial on creating a button, see https://www.youtube.com/watch?v=aE5f1tV5nU4
Initial setup:
Have project with initial activity and button for Toast
Create new activity: SecondActivity
Show .xml, .java, and manifest.
Launch activity:
startActivity(new Intent( [Current Activitys this], [New Activitys Java].class));
Encapsulate launching
Intent i = SecondClass.makeIntent(this)
startActivity();
Activity Stack
- Loading an Activity pushes it on the Activity *Stack*
- Pressing back button pops stack:
destroys this activity, returns to previous activity.
- New Button runs activitys finish() method pops it in code.
Must use correctly to have good activity stack.
If you instead startActivity() back to the first one, you make a loop.
Can move on and kill current activity:
startActivity(...);
finish();
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? Switching Activities in Android Studio: Android Programming](https://i.ytimg.com/vi/B1oaE0Odqes/mqdefault.jpg)

