Skip to content

Instantly share code, notes, and snippets.

@StanKocken
Created August 6, 2013 09:14
Show Gist options
  • Save StanKocken/6162997 to your computer and use it in GitHub Desktop.
Save StanKocken/6162997 to your computer and use it in GitHub Desktop.
A gridlayout override to have the same width for all cells (expect for the first one because in my case it's an header)
/*
* Copyright (C) 2013 Stan Kocken (http://www.stankocken.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.skocken.gist;
import android.content.Context;
import android.support.v7.widget.GridLayout;
import android.util.AttributeSet;
import android.view.View;
/**
* @author stan.kocken@gmail.com
*
*/
public class GridLayoutUniform extends GridLayout {
/** Used locally to tag Logs */
@SuppressWarnings("unused")
private static final String TAG = GridLayoutUniform.class.getSimpleName();
public GridLayoutUniform(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public GridLayoutUniform(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GridLayoutUniform(Context context) {
super(context);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
for (int i = 1; i < getChildCount(); i++) {
View child = getChildAt(i);
// to have all field with the same width (expect for the first one, it's an header)
GridLayout.LayoutParams params = (GridLayout.LayoutParams) child.getLayoutParams();
int newWidth = ((r - l) / getColumnCount()) - params.rightMargin - params.leftMargin;
if (newWidth != params.width) {
params.width = newWidth;
child.setLayoutParams(params);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment