Alignment not right when adding view dynamically to GridLayout
Asked Answered
G

2

7

When added buttons by XML - All good

When adding buttons through XML

  <GridLayout
    android:id="@+id/social_gl_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerHorizontal="true"
    android:alignmentMode="alignBounds"
    android:columnCount="2"
    android:padding="8dp">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="45dp"
        android:layout_columnWeight="1">
        <Button
            android:layout_width="fill_parent"
            android:layout_height="45dp"
            android:background="@android:color/holo_blue_light"
            android:text="Hi"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="45dp"
        android:layout_columnWeight="1">
        <Button
            android:layout_width="fill_parent"
            android:layout_height="45dp"
            android:background="@android:color/holo_green_light"
            android:text="Whatsapp"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="45dp"
        android:layout_columnWeight="1">
        <Button
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:background="@android:color/holo_green_light"
            android:text="This is facebook"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="45dp"
        android:layout_columnWeight="1">
        <Button
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:background="@android:color/holo_blue_light"
            android:text="On"
            />
    </LinearLayout>

</GridLayout>

When added Buttons dynamically(By code)- Alignment missing, buttons not occupying complete width of a column

GridLayout gl = (GridLayout) findViewById(R.id.social_gl_content);
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    for (int i = 0 ; i < strs.length ; i++) {
        View v = inflater.inflate(R.layout.grid_item, null);
        Button b = (Button) v.findViewById(R.id.button);
        b.setText(strs[i]);
        if ( i % 2 ==0) {
            b.setBackgroundColor(Color.BLACK);
        }else{
            b.setBackgroundColor(Color.BLUE);
        }
        gl.addView(v);
    }

enter image description here

Gait answered 20/11, 2015 at 7:41 Comment(2)
take a look here.. #10016843Haigh
try this LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); b.setLayoutParams(parms);Caprification
K
5

Instead of passing null:

View v = inflater.inflate(R.layout.grid_item, null);

pass the parentView so that v has the correct layout params.

View v = inflater.inflate(R.layout.grid_item, gl, false);

Kelsi answered 23/11, 2015 at 3:15 Comment(1)
I duplicated the original problem, then I tried this solution and ended up with a blank screen.Keratogenous
C
1

Try this :

LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
b.setLayoutParams(parms);
Caprification answered 20/11, 2015 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.