just last column in Grid layout gets stretch - android

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);

Related

Dynamic LinearLayout with same height as width

I'm trying to create a dynamic LinearLayout with the same height as width. With XML I can achieve this by setting the the height as 0px i.e. android:layout_height="0dp". But I'm not able to achieve the same thing while creating the layout from Java code.
LinearLayout parentLayout = layoutInflater.inflate();
LinearLayout childLayout = new LinearLayout(context);
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.height = 0;
parentLayout.addView(childLayout, layoutParams);
I want the childLayout to be of the same height as width but the height appears to be set to WRAP_CONTENT instead, what am I missing?
layoutParams.weight = layoutParams.height

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);

I programmatically created a TextView in TableLayout, it automatically sets the height and width

I programmatically created a TextView in TableLayout, it automatically sets the height and width.
Can I change the height and width of the TextView manually inside the TableLayout?
Use
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.height = HEIGHT;
params.width = WIDTH;
textView.setLayoutParams(params);
same for other parameters like margin, padding etc
Use
LayoutParams lParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
lParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
You can set the height and width programmatically using the following code.
TableRow.LayoutParams param = new TableRow.LayoutParams();
paramColumn1.height = TableRow.LayoutParams.WRAP_CONTENT;
paramColumn1.width = TableRow.LayoutParams.WRAP_CONTENT;
textView1.setLayoutParams(param);
You can also set other properties using this way.

Have an ImageView with a default height, change height when an image is put into it

So I am making an app that holds multiple user-chosen pictures. I have default ImageViews with pre-set heights of 300dp. I want this height to change to wrap_content once an image has been placed into the ImageView. The only way I know to do this is to remove the image from the layout and then re-add it with a new LayoutParams, but this messes up the order of the other views in my layout. Can I change the height without removing it?
Essentially:
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
final float scale = getResources().getDisplayMetrics().density;
LinearLayout.LayoutParams mTestImgParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
(int) Math.ceil(scale * 300)
);
final ImageView createdView = new ImageView(this);
mainLayout.addView(createdView, mTestImgParams);
//onLongClick listener, get picture, set the picture into the imageview, etc.
I somehow want to change the
(int) Math.ceil(scale*300)
to
LinearLayout.LayoutParams.WRAP_CONTENT
without removing and re-adding the ImageView, and only after the image has been placed. Help please.
You could try to get the current layout params and change it.
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) createdView.getLayoutParams();
params.height = LayoutParams.WRAP_CONTENT;
createdView.setLayoutParams(params);

How to programmatically set the width of the LinearLayout?

Is the source of the slider and the lesson link:
http://www.oodlestechnologies.com/blogs/Facebook-Style-Slide-Menu-In-Android
In this case, the width of the file "left_menu.xml" determined TextView (android:layout_width="260dp")
How do I set the width to "LinearLayout" file "left_menu.xml" depending on the device screen? For example, I want the width of the "LinearLayout" was always 1/3 of the screen device? Or any way to set the width of the TextView 1/3 of the width of the device screen.
To set the LinearLayout or TextView width to 1/3 of the device screen:
first of all get the device screen width:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
try {
display.getRealSize(size);
} catch (NoSuchMethodError err) {
display.getSize(size);
}
int width = size.x;
int height = size.y;
now just create a LayoutParams and set it to the Text:
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int)(width/3),
LinearLayout.LayoutParams.WRAP_CONTENT); // or set height to any fixed value you want
your_layout.setLayoutParams(lp);
// OR
your_textView.setLayoutParams(lp);
LinearLayout linear = (LinearLayout) findViewById(R.id.ll);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linear.setLayoutParams(params);
As you can see, you can set Integer values for the LinearLayout.LayoutParams() constructor, like this:
LinearLayout.LayoutParams cellParams = new LinearLayout.LayoutParams(0, 100);
The costructor wants pixels and not dp(Density Pixels), here's a formula to convert PXs from DPs:
(int) (<numberOfDPs> * getContext().getResources().getDisplayMetrics().density + 0.5f)
LinearLayout layout = (LinearLayout)findViewById(R.id.ll);
LayoutParams params = (LayoutParams) layout.getLayoutParams();
params.height = 100;
params.width = 100;
The above answers are correct. I'll just suggest future readers to take a look at their xml layout file and be sure that the LinearLayout element is not using weight attribute. If so, consider updating weight in your LayoutParams object.
Another good practice is to retrieve the layout params object from the view you're trying to adjust, instead of creating one from scratch and having to set all the values.
LinearLayout YOUR_LinearLayout =(LinearLayout)findViewById(R.id.YOUR_LinearLayout)
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
/*width*/ ViewGroup.LayoutParams.MATCH_PARENT,
/*height*/ 100,
/*weight*/ 1.0f
);
YOUR_LinearLayout.setLayoutParams(param);
The easy way to do that, set value of layoutParams, and please notice this size is not in DP.
view.layoutParams.width = 400;
view.layoutParams.height = 150;
view.requestLayout();

Categories

Resources