0

I try to create a custom LinearLayout which will act like a grid. I succeed to do so, and to have the aspect I want but I still have a problem. When I have only one rows. I want to change the layout_height attribute of my custom layout from wrap_content to march_parent to be able to center the only LinearLayout which will be created.

So for now I my code was acting after the onFinishInflate method but I have a problem here, I can't get a LayoutParams because the value are null and when I try to set my own new params, it's not working :

setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

Here is my code :

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.ArrayList;

public class GameGridLayout extends LinearLayout {
    private Context context;
    private int currentRow = -1, maxRowItems = 3;
    private ArrayList<LinearLayout> layouts = new ArrayList<>();

    public GameGridLayout(Context context) {
        super(context);
        this.context = context;
    }

    public GameGridLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public GameGridLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    private ArrayList<View> getViews() {
        int nbChilds = getChildCount();
        ArrayList<View> views = new ArrayList<>();
        for (int i = 0; i < nbChilds; i++) {
            views.add(getChildAt(i));
        }
        return views;
    }

    private void reorganizeViews(Context context) {
        ArrayList<View> views = getViews();
        if(views.size() == 4) maxRowItems = 2;

        for (int i = 0; i < views.size(); i++) {
            if(i % maxRowItems == 0) {
                LinearLayout currentLayout = new LinearLayout(context);
                layouts.add(currentLayout);
                currentRow++;
            }
            View currentChild = views.get(i);
            if(currentChild != null) {
                removeView(currentChild);
                layouts.get(currentRow).addView(currentChild);
            }
        }

        for (int i = 0; i < layouts.size(); i++) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            layouts.get(i).setLayoutParams(params);
            layouts.get(i).setOrientation(HORIZONTAL);
            layouts.get(i).setWeightSum(3);
            layouts.get(i).setGravity(Gravity.CENTER);

            addView(layouts.get(i));
        }

        setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        reorganizeViews(context);
    }
}

If you know who to do, I saw that if I use the inflate method, I could maybe have the result I want, but I don't know how to do, for example :

inflate(context, ??, this);

I don't have any xml to inflate, so...

zed13
  • 357
  • 4
  • 21

1 Answers1

0

Calling setLayoutParams in onFinishInflate won't work actually, because the LayoutParams of your GameGridLayout will be overwritten when it is added to its parent later.

I suggest you call setLayoutParams of the GameGridLayout after it is added to its parent.

shhp
  • 3,653
  • 2
  • 18
  • 23
  • Yeah, I have finished by doing this, just create a simple method which setLayoutParams and in my onCreate I called that method. It has the advantage to be easy ^_^. Thanks – zed13 Mar 15 '16 at 14:54