How do I set android:layout_columnWeight="1" programmatically to an element in an android support v7 Gridlayout

17,119

Solution 1

I found this method from GridLayout: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.0_r1/android/widget/GridLayout.java#GridLayout.spec%28int%2Cfloat%29

So maybe you could try something like this:

((GridLayout.LayoutParams) this.getLayoutParams()).columnSpec =
    GridLayout.spec(GridLayout.UNDEFINED, 1f);

Solution 2

-Here you are ! :>

Button button = new Button(this);
GridLayout.LayoutParams param= new GridLayout.LayoutParams(GridLayout.spec(
            GridLayout.UNDEFINED,GridLayout.FILL,1f),
            GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f));
param.height = 0;                                                                                                           
param.width = 0;
button.setLayoutParams(param);

Solution 3

For anyone using xamarin.android, these answers helped me, but I had to change the syntax a bit. So if anyone else is using xamarin.android here is the syntax:

GridLayout.LayoutParams param = new GridLayout.LayoutParams();
param.RowSpec = GridLayout.InvokeSpec(GridLayout.Undefined, 1f);
param.ColumnSpec = GridLayout.InvokeSpec(GridLayout.Undefined, 1f);
view.LayoutParameters = param;

Where view is the view contained in the GridLayout.

Solution 4

This functions of GridLayout.Spec will allow you to set the weight correctly:

public static Spec spec(int start, int size, Alignment alignment, float weight)
public static Spec spec(int start, Alignment alignment, float weight)
public static Spec spec(int start, int size, float weight) 
public static Spec spec(int start, float weight)

usage example:

((LayoutParams) gm.getViewFromGridCoords(i, j).getLayoutParams()).columnSpec = GridLayout.spec(j, span, CENTER, 1);
Share:
17,119
hans_dc
Author by

hans_dc

Updated on June 04, 2022

Comments

  • hans_dc
    hans_dc about 2 years

    I'm trying to build a GirdLayout programmatically with 2 columns and I want these columns to have equal width set at half the width of the screen.

    I figured out you can do this since API 21 or with the support v7 GirdLayout view. I see examples that uses android:layout_columnWeight="1" to do this. But I can not find how to do this programmatically.

    Can anyone help me with this matter?

    package com.tunify.v3.component;
    
    import java.util.ArrayList;
    
    import android.support.v7.widget.GridLayout;
    import android.util.DisplayMetrics;
    import android.util.TypedValue;
    import android.view.ViewGroup;
    
    import com.tunify.asme.data.ChangeableSelectionParameterValue;
    
    public class ChangeableSelectionParameters extends GridLayout{
    
    //private ArrayList<ChangeableSelectionParameterView> selectionParameterViews;
    private ArrayList<ChangeableSelectionParameterValue> selectionParameterValues;
    
    private static final int ITEM_PADDING_LEFT_DP = 0;
    private static final int ITEM_PADDING_RIGHT_DP = 0;
    private static final int ITEM_PADDING_TOP_DP = 0;
    private static final int ITEM_PADDING_BOTTOM_DP = 0;
    
    //private static final int MUSICCOLLECTION_DISPLAY_TEXT_SIZE_SP = 15;
    
    
    public ChangeableSelectionParameters(android.content.Context context, ArrayList<ChangeableSelectionParameterValue> selectionParameterValues) {
        super(context);
    
        this.selectionParameterValues = selectionParameterValues;
    
        initializeLayoutBasics(context);
        initializeComponents();
    }
    
    private void initializeLayoutBasics(android.content.Context context) {
        setOrientation(VERTICAL);
        setColumnCount(2);
        setRowCount((int)Math.ceil(selectionParameterValues.size()/2.0));
    
        int width = ViewGroup.LayoutParams.MATCH_PARENT;
        int height = ViewGroup.LayoutParams.WRAP_CONTENT;
        android.view.ViewGroup.LayoutParams layoutParams = new android.view.ViewGroup.LayoutParams(width, height);
        this.setLayoutParams(layoutParams);
    
        DisplayMetrics metrics = getResources().getDisplayMetrics();
    
        int paddingLeftPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ITEM_PADDING_LEFT_DP, metrics);
        int paddingRightPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ITEM_PADDING_RIGHT_DP, metrics);
        int paddingTopPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ITEM_PADDING_TOP_DP, metrics);
        int paddingBottomPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, ITEM_PADDING_BOTTOM_DP, metrics);
        setPadding(paddingLeftPx, paddingTopPx, paddingRightPx, paddingBottomPx);
    
    }
    
    private void initializeComponents() {
    
        for(ChangeableSelectionParameterValue param : this.selectionParameterValues){
            ChangeableSelectionParameterView v = new ChangeableSelectionParameterView(getContext(), param);
            //TODO set views layout column weight to 1
            this.addView(v);
        }
     }
    }
    

    SOLUTION:

     GridLayout.LayoutParams parem = new LayoutParams(GridLayout.spec(GridLayout.UNDEFINED, 1f),      GridLayout.spec(GridLayout.UNDEFINED, 1f));
     v.setLayoutParams(parem);
    
  • hans_dc
    hans_dc almost 9 years
    Your answer helped me, I got an ClassCastException when I tried your code, but changed it a bit to get the answer. I added the solution to my question!
  • Aiko West
    Aiko West about 6 years
    Does this work with a normal GridLayout or do I need to use android.support.v7.widget.GridLayout. Because I need to use my GridLayout in a ScrollView, but v7.widget is not allowed inside the ScrollView. But with my normal GridLayout, it is not working. Do I need to have set any other settings to the Layout ?