GridLayout crashes app - android

I have a GridLayout and I want to alter it with java.
I have the following code:
GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout1);
GridLayout.LayoutParams param =new GridLayout.LayoutParams();
param.rightMargin = 5;
param.topMargin = 5;
gridLayout.setLayoutParams(param);
Why does it crash?

The type of LayoutParams is determined by the parent of the View. Unless your R.id.gridLayout1 is inside another GridLayout, you should not be using GridLayout.LayoutParams.

You can do this :
GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout1);
PARENT_LAYOUT.LayoutParams layoutParams = ( PARENT_LAYOUT.LayoutParams) gridLayout.getLayoutParams();
layoutParams.rightMargin = 5;
layoutParams.topMargin = 5;
and replace PARENT_LAYOUT with the LinearLayout or whatever your are using as Root layout

Related

Set View height to 0dp and weight to 1 programatically

When creating GridView via xml I use the below code, but how could I achieve that programmatically during runtime?
android:layout_height="0dp"
android:layout_weight="1"
Thanks.
use this
GridView YOUR_GRID_VIEW =(GridView )findViewById(R.id.GridView )
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ 0,
/*weight*/ 1.0f
);
YOUR_GRID_VIEW.setLayoutParams(param);
LayoutParams.MATCH_PARENT is for width, 0 for Height and 1.0f is for weight
If you have layout_weight in xml, that means, that the parent layout of GridView is LinearLayout.
Having a reference to a GridView, that is already attached to view hierarchy, you can simply get LayoutParams of that view, cast it to LinearLayout.LayoutParams and perform necessary changes:
GridView gridView = ...;
LinearLayout.LayoutParams params = gridView.getLayoutParams();
params.weight = 1;
params.height = 0;
gridView.setLayoutParams(params);
Otherwise, if you are constructing GridView from scratch, you should create LinearLayout.LayoutParams on your own:
LinearLayout linearLayout = ...;
GridView gridView = new GridView(context);
LinearLayout.LayoutParams params =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
/* height */ 0,
/* weight */ 1);
linearLayout.addView(gridView, params);

GridLayout (not GridView) : Items are not correctly positioned in GridLayout when trying to stretch them evenly

I'm trying to add 12 buttons to a 3X4 GridLayout with fixed width and height programmally.
All these buttons are supposed to stretch evenly inside the grid, but it doesn't work well in some cases when I adjust the width of the GridLayout.
It seems that the width has to be set larger than a certain value so that all the buttons can be positioned correctly.
Here is my code:
gridLayout = new GridLayout(this);
gridLayout.setBackgroundColor(Color.RED);
//width c
gridLayout.setLayoutParams(new ViewGroup.LayoutParams(200, 300));
gridLayout.setRowCount(NUM_OF_ROW);
gridLayout.setColumnCount(NUM_OF_COL);
gridLayout.setUseDefaultMargins(false);
for(int i = 0;i < NUM_OF_ROW; ++i){
for(int j = 0;j < NUM_OF_COL; ++j){
Button btn = new Button(this);
gridLayout.addView(btn);
GridLayout.LayoutParams params = (GridLayout.LayoutParams) btn.getLayoutParams();
GridLayout.Spec rowSpec = GridLayout.spec(i,1,1);
GridLayout.Spec colSpec = GridLayout.spec(j,1,1);
params.rowSpec = rowSpec;
params.columnSpec = colSpec;
params.setGravity(Gravity.FILL);
btn.setLayoutParams(params);
}
}
relativeLayout = (RelativeLayout) findViewById(R.id.activity_main);
relativeLayout.addView(gridLayout);
Problem happens when width value set small enough.
Output on a Samsung tablet
Output I expected

How to make LinearLayout height to be the highest height of it's child component?

I add two Checkboxes dynamically to a Linearlayout. Then those Linearlayouts are added one after another in a Relativelayout. The weights of the checkboxes are set so that each take 50% of the Linearlayout width. Now, if their heights do not match, the bottom of the checkbox with bigger height disappears. How to solve this? Here's a screenshot:
And the code:
LinearLayout ll;
LinearLayout.LayoutParams lp;
CheckBox ch;
int id = 1200, i, j;
for (i = 0, j = 0; i < selections.size() - 1; i += 2, j += 2) {
ll = new LinearLayout(NotificationSettings.this);
lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ch = new CheckBox(NotificationSettings.this);
lp.weight = 1.0f;
ch.setLayoutParams(lp);
ch.setText(selections.get(i));
ch.setChecked(isSelected);
ch.setTextColor(color);
ch.setId(j);
ll.addView(ch);
ch = new CheckBox(NotificationSettings.this);
ch.setLayoutParams(lp);
ch.setText(selections.get(i + 1));
ch.setChecked(isSelected);
ch.setTextColor(color);
ch.setId(j + 1);
ll.addView(ch);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
if (id == 1200)
p.addRule(RelativeLayout.BELOW, addBelow);
else
p.addRule(RelativeLayout.BELOW, id);
ll.setLayoutParams(p);
ll.setId(++id);
rl.addView(ll);
}
Edit:
When both checkboxes have multiple lines:
Can you make sure that the Linear Layout's height below it is not too large that it covers the above Linear Layout?
Or try changing your Relative Layout Params' height to WRAP_CONTENT
Change
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
To
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

Unwanted spacing between android viewgroups added programmatically

I'm adding a RelativeLayout in a FrameLayout ( added in xml ) and I attach a LinearLayout in this RelativeLayout.
The problem is I get an unwanted left/right spacing/padding between the RelativeLayout and its child LinearLayout as you can see here from the uiautomator dump.
The weird thing is that spacing is always 11 pixels from each side tested on emulator N5, N4, N7 images and Moto G and N4 devices.
Thank you
The code is :
rlBottomBarContainer = new RelativeLayout(this);
rlBottomBarContainer.setId(R.id.bottomBarContainer);
rlBottomBarContainer.setClipToPadding(false);
rlBottomBarContainer.setClipChildren(false);
rlBottomBarContainer.setBackground(getResources().getDrawable(R.drawable.bara));
int topMargin = whiteTowerContainerHeight /2 + tower_height/2 + 100;
int containerHeight = whiteTowerContainerHeight - topMargin;
FrameLayout.LayoutParams containerParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
containerHeight);
containerParams.topMargin = topMargin;
rlBottomBarContainer.setLayoutParams(containerParams);
frameLayoutRoot.addView(rlBottomBarContainer); // parentLayout
llBottomBarContainer = new LinearLayout(this);
llBottomBarContainer.setId(R.id.upper_bottombar_ID);
llBottomBarContainer.setClipChildren(false);
llBottomBarContainer.setClipToPadding(false);
llBottomBarContainer.setBackgroundColor(Color.parseColor("#00FFFFFF"));
llBottomBarContainer.setOrientation(LinearLayout.HORIZONTAL);
RelativeLayout.LayoutParams bottomBarParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
getBottomBarTextSize());
llBottomBarContainer.setLayoutParams(bottomBarParams);
rlBottomBarContainer.addView(llBottomBarContainer);
As you can see from the code the LinearLayout is added with match_parent for the width. RelativeLayout width is from 0 to 720px but LinearLayout is from 11 to 709pixels.
First of all I do not understand why you use a FrameLayout and you do not use directly the RelativeLayout as your parent layout.
The error I see on your code is that you set a FrameLayout.LayoutParams to your RelativeLayout and a RelativeLayout.LayoutParams to your LinearLayout.
Probably this is why you get some extra spacing.
I would suggest that you do not create your layouts programmatically, but rather do it on xml and then if you need the layout objects on java you can get them with the findViewById(int id) method

just last column in Grid layout gets stretch

I want to stretch all columns in my grid layout equally , How can I do that ?
I am using this params for every cell
LayoutParams param2 =new LayoutParams();
param2.height = LayoutParams.WRAP_CONTENT;
param2.width = ViewGroup.LayoutParams.WRAP_CONTENT;
param2.setGravity(Gravity.CENTER);
param2.columnSpec = GridLayout.spec(i,FILL);
param2.rowSpec = GridLayout.spec(rowCount,FILL);
param2.setGravity(Gravity.FILL);
Build Your Own GridLayout using nested LinearLayouts so you can customize it like you want, Or simply set the weight for each cell you add but you will need set you minimum sdk to 21
Setting width of child dynamically
GridLayout.LayoutParams params = (GridLayout.LayoutParams) child.getLayoutParams();
params.width = (parent.getWidth()/parent.getColumnCount()) -params.rightMargin - params.leftMargin;
child.setLayoutParams(params);

Categories

Resources