I am using a simple GridLayout
to which I'm adding buttons dynamically.
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tagGridLayout"
android:background="@color/white"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnCount="3"
>
</GridLayout>
And I am using this Java code to fill my grid which is all working fine except that the set Gravity option doesn't do anything. I've tried - changing layout_width to different types in the XML file, adding gravity to the GridLayout
etc. as mentioned in other solutions on this site. Another thing to note is that I am doing this in an Async task inside a Fragment. Basically I want to achieve what layout_gravity="fill_horizontal"
achieves in the XML.
tagButtons = new Button[trendingTagsCount];
for(int i=0;i<trendingTagsCount;i++)
{
tagButtons[i] = new Button(getActivity());
//tagButtons[i].setLayoutParams(new LayoutParams(Gravity.FILL_HORIZONTAL));
tagButtons[i].setText(getTagsList.get(i).tag);
tagButtons[i].setGravity(Gravity.FILL_HORIZONTAL);
tagButtonGrid.addView(tagButtons[i]);
}