chacham
Lurker
I am adding ImageButtons and TextViews to a GridLayout programmatically, and want to center the TextViews in relation to its ImageButton. The ImageButtons are all the same size. The GridLayout is specified in the xml file with: android:columnCount="2"
list is an array of a class holding the required information.
This is my first project not copied from a book, and i want to learn how to do this.
doesn't seem to have any effect.
Trying to copy the sizing of the ImageButton, got me to try:
Yet, setting title itself, or to the layout, puts both TextViews on top of each other, under both ImageButtons (though in the 2nd column):
How to i center the TextView?
list is an array of a class holding the required information.
Java:
int button_dip = Math.round((float) 150 * getApplicationContext().getResources().getDisplayMetrics().density);
for(int i = 0; i < list.length; i++)
{
ImageButton button = new ImageButton(this);
button.setImageResource(list[i].image_off);
TextView title = new TextView(this);
title.setText(list[i].title);
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 50);
layout.addView(button, button_dip, button_dip););
layout.addView(title);
}
This is my first project not copied from a book, and i want to learn how to do this.
Java:
title.setGravity(Gravity.CENTER_VERTICAL);
Trying to copy the sizing of the ImageButton, got me to try:
Java:
GridLayout.LayoutParams title_layout = new GridLayout.LayoutParams();
title_layout.setGravity(Gravity.CENTER_VERTICAL);
Yet, setting title itself, or to the layout, puts both TextViews on top of each other, under both ImageButtons (though in the 2nd column):
Java:
layout.addView(title, title_layout);
How to i center the TextView?
Last edited: