• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Center an item in a GridLayout programmatically

chacham

Lurker
Jan 26, 2016
7
0
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.

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);
doesn't seem to have any effect.

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:
OK, it looks like this does work:
Java:
GridLayout.LayoutParams title_layout = new GridLayout.LayoutParams();
title_layout.setGravity(Gravity.CENTER_VERTICAL);

When i first tried it, it was outside the loop, so both views were added with the same LayoutParams. For whatever reason, that results in putting both views on top of each other, centering in the second row, which seems to extend to the bottom of the layout. Creating a new LayoutParams for each iteration, however, does work (though the second one still centers between it and the bottom of the layout). Why does it work like that?
 
Upvote 0
Why dont you set the button location via the xml file that is used for styling the code? That way I think is easy to do.
Something like this
HTML:
    <ImageButton android:id="@+id/btnFindMe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@drawable/findme"></ImageButton>
 
Upvote 0
Thank you for taking a look.

I'd rather not use the xml layout file in this project because:

1) I find it easier to add many buttons and track them in an array than a bunch of xml.
2) The list may change, and it is easier to manage it with an array.
3) At this point, i just gotta learn how to do it. :)
 
Last edited:
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones