Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SetGravity for GridLayout not working

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]);
        }
like image 618
RaWa Avatar asked Jan 21 '13 09:01

RaWa


People also ask

How do I create a GridLayout in Java?

Such layouts may also be implemented by directly writing Java code or manually creating elements in an XML layout file, the latter of which is the topic of this chapter. A GridLayout is declared within an XML file using the <GridLayout> element tag. For example:

How do I design a GridLayout based user interface?

Visually designing a GridLayout based user interface is not, however, the only available method of working with the GridLayout class. Such layouts may also be implemented by directly writing Java code or manually creating elements in an XML layout file, the latter of which is the topic of this chapter.

How does GridLayout determine the position of an element?

If the element doesn’t specify where it needs to be present, then the GridLayout automatically assigns its position. The position of the component is based on its orientation, rowCount, and columnCount properties. 3. Space

What is the difference between GridView and GridLayout in Android?

GridView is a view, whereas GridLayout is a layout that can hold various views in it. Using GridView, you can display the items that are coming from the Adapter. GridLayout allows you to place many views and elements inside the layout and then style them. It allows you to set the number of rows and columns for your grid.


1 Answers

As Karan Mer said you set the Layout Gravity with GridLayout.LayoutParams.

But be careful, in Gridlayout you have to set the columnSpec / rowSpec of the Children (in your case the Button) before:

param.columnSpec = GridLayout.spec(0);
param.rowSpec = GridLayout.spec(0);

and only then

param.setGravity(Gravity.RIGHT);

if columnSpec / rowSpec is UNDEFINED when you do setGravity, gravity doesn't work...

Even if you do:

param.setGravity(Gravity.RIGHT);
param.columnSpec = GridLayout.spec(0);
param.rowSpec = GridLayout.spec(0);

Gravity doesn't work...

The right way is:

param.columnSpec = GridLayout.spec(0);
param.rowSpec = GridLayout.spec(0);
param.setGravity(Gravity.RIGHT);

I don't know if is a bug or a deliberate thing. (I'm using Android API 19)

like image 133
Zaza Avatar answered Oct 27 '22 16:10

Zaza