1. Are you ready for the Galaxy S20? Here is everything we know so far!

Center an item in a GridLayout programmatically

Discussion in 'Android Development' started by chacham, Feb 3, 2016.

  1. chacham

    chacham Lurker
    Thread Starter

    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.

    Code (Java):
    1.  
    2. int button_dip = Math.round((float) 150 * getApplicationContext().getResources().getDisplayMetrics().density);
    3.  
    4. for(int i = 0; i < list.length; i++)
    5.         {
    6.             ImageButton button = new ImageButton(this);
    7.             button.setImageResource(list[i].image_off);
    8.  
    9.             TextView title = new TextView(this);
    10.             title.setText(list[i].title);
    11.             title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 50);
    12.  
    13.             layout.addView(button, button_dip, button_dip););
    14.             layout.addView(title);
    15.         }
    This is my first project not copied from a book, and i want to learn how to do this.

    Code (Java):
    1. title.setGravity(Gravity.CENTER_VERTICAL);
    doesn't seem to have any effect.

    Trying to copy the sizing of the ImageButton, got me to try:
    Code (Java):
    1. GridLayout.LayoutParams title_layout = new GridLayout.LayoutParams();
    2. 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):
    Code (Java):
    1. layout.addView(title, title_layout);
    How to i center the TextView?
     


    #1 chacham, Feb 3, 2016
    Last edited: Feb 3, 2016

    1. Download the Forums for Android™ app!


      Download

       
  2. chacham

    chacham Lurker
    Thread Starter

    OK, it looks like this does work:
    Code (Java):
    1. GridLayout.LayoutParams title_layout = new GridLayout.LayoutParams();
    2. 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?
     
  3. chacham

    chacham Lurker
    Thread Starter

    Hmm... I guess LayoutParams isn't just used for information while placement, but actually becomes the permanent layout object.
     
  4. HeavensSentSword

    HeavensSentSword Well-Known Member

    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:
    1.     <ImageButton android:id="@+id/btnFindMe"
    2.        android:layout_width="wrap_content"
    3.        android:layout_height="wrap_content"
    4.        android:layout_centerInParent="true"
    5.        android:background="@drawable/findme"></ImageButton>
     
  5. chacham

    chacham Lurker
    Thread Starter

    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. :)
     
    #5 chacham, Feb 6, 2016
    Last edited: Feb 8, 2016
Loading...

Share This Page

Loading...