I have textview for which i want to use weight as well as width, but i want to pass width as 0dp, if i set width in layout params as 0, it does not render on screen.
How can i set width as 0dp dynamically through code
I even tried giving width as LayoutParams.Wrap_Content, but that also hasn't worked
LinearLayout.LayoutParams tv_params_level = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, 0.1f);
If you set the width of the TextView via xml to 0dp you must set then its weight to 1 or other value greater than 0. To set the width programatically:
LinearLayout.LayoutParams tv_params_level = new LinearLayout.LayoutParams(0,
LayoutParams.WRAP_CONTENT, 1.0);
Then set this layout parameteres to your TextView:
tv.setLayoutParams(tv_params_level);
Related
I'm changing the width of a grid view based on its column width and number of columns (which works):
gridView.setLayoutParams(new LinearLayout.LayoutParams(gridManipulation.getColumnWidth() * (int)Math.sqrt(str.length) + 10, LayoutParams.FILL_PARENT));
gridView.setGravity(Gravity.CENTER);
I've tried re-centering it in the parent view but its not working, does anyone know why, its still centred as if it still has the width parameters before my dynamic change.
Set the Gravity as LayoutParams property then set that LayoutParams to your GridView as follows...
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(gridManipulation.getColumnWidth() * (int)Math.sqrt(str.length) + 10, LayoutParams.FILL_PARENT);
params.gravity = Gravity.CENTER;
gridView.setLayoutParams(params);
you cant center it if its bigger than its parent, which is never bigger than the screen, unless you set it manually, which will cause the system to render data outside the screen and slow you way down.....
is it expanding verticaly or horizontally or both? my recomendation would be to put it in a scrollview.
I've been stuck on this for quite some time now. The problem is that I have a linearlayout consisting of multiple textViews and an imageView. However, the imageview has a smaller width than the textviews. I tried everything i could find, but I couldn't find it. I am able to set the image to the left or right by using gravity, but when i use fill, it just centers and keeps the small width. The only thing i can think of is that the image is downloaded in a different thread. Can the width only be set after the image is downloaded?
// Download photo
this.post_body.setPhoto(url);
// Add imageview to linear layout
this.ll.addView((View) this.post_body.photo);
LinearLayout.LayoutParams layoutParams;
layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.FILL_HORIZONTAL;
this.post_body.photo.setLayoutParams(layoutParams);
Just try to set width=0 and set weight 1 for all the textview and buttons, imageviews whatever you want.
Just try like this here 1f is weight.
layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
I'm extending a LinearLayout to make a compound widget. The widget is set to layout_width="MATCH_PARENT".
mLabelTextView = new TextView(context);
mLabelTextView.setText("Testing");
LayoutParams labelParams = new
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0.0f);
labelParams.gravity = Gravity.CENTER_VERTICAL;
addView(mLabelTextView, labelParams);
mContentView = new Checkbox(content);
contentParams = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f);
contentParams.gravity = Gravity.CENTER_VERTICAL | Gravity.Right;
addView(mContentView, contentParams);
My problem is that the Gravity.Right property is taking effect on mContentView, but Gravity.CENTER_VERTICAL is. I know that mContentView is getting stretched to fill the remaining space. I've already tried just getting gravity=right by itself and that didn't work.
I have a similar code path in which I inflate a view that already has right gravity set in XML and that is working. So I must be missing something somewhere.
If your parent LinearLayout has horizontal orientation (which you don't specify, but I'm guessing), then it will ignore all horizontal layout gravity bits. Likewise, if it has vertical orientation, then it would have ignored the CENTER_VERTICAL setting.
One trick is to wrap mContentView in a vertically oriented LinearLayout, give that wrapper a layout weight of 1 (so it fills the remaining space) and then the wrapper will honor the RIGHT gravity.
A better alternative might be to extend RelativeLayout instead of LinearLayout. That gives you 2-D control over gravity (unlike LinearLayout).
I need to set the height of an imageview as matchparent programatically.if it is a fixed height i know how to set.
but how can i set it as matchparent?
EDIT:
actually height of the parent layout is dynamic.so i need to make the height of the imageview as the height of parent.
imageView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
imageView.getLayoutParams().height= ViewGroup.LayoutParams.MATCH_PARENT;
imageView.setLayoutParams
(new ViewGroup.MarginLayoutParams
(width, ViewGroup.LayoutParams.MATCH_PARENT));
The Type of layout params depends on the parent view group. If you put the wrong one it will cause exception.
For Kotlin Users
val params = mImageView?.layoutParams as FrameLayout.LayoutParams
params.width = FrameLayout.LayoutParams.MATCH_PARENT
params.height = FrameLayout.LayoutParams.MATCH_PARENT
mImageView?.layoutParams = params
Here I used FrameLayout.LayoutParams since my views( ImageView) parent is FrameLayout
I had same issue. Resolved by firstly setting :
imageView.setMinHeight(0);
imageView.setMinimumHeight(0);
And then :
imageView.getLayoutParams().height= ViewGroup.LayoutParams.MATCH_PARENT;
setMinHeight is defined by ImageView, while setMinimumHeight is defined by View. According to the docs, the greater of the two values is used, so both must be set.
You can try this incase you would like to match parent. The dimensions arrangement is width and height inorder
web = new WebView(this);
web.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
You can use the MATCH_PARENT constant or its numeric value -1.
initiate LayoutParams .
assign the parent's width and height and pass it to setLayoutParams method of the imageview
I have a LinearLayout class in which I have:
TexView | ImageView | EditText | ImageView.
I have the last ImageView all the way to the right side of the LinearLayout its wrapped in. The EditText runs very long and in some case pushes the last ImageView out of view (or so it seems to push it out of view).
I want to to have the EditText set to a percentage of the total width. I tried using weight with LinearLayout parameters but it seems to cause the view to get all out of wack. For example here it is for the EditText:
LinearLayout.LayoutParams lpEt = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
.50f);
All the other views follow suit but have lesser weights (.2, .2, .1) and they all add up to 1.0. But the LinearLayout row is never spaced correctly.
Should I find out the width of the parent (which is a ListView) and then set the width of the EditText explicitly based on the parent's width or is there a better way?
Im not sure what your linear layout orientation is but im guessing its horizontal. if so try this.
LayoutParams textViewParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(textViewParams);
LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
0,LayoutParams.WRAP_CONTENT,1f);
editText.setLayoutParams(editTextParams);
LayoutParams imageViewParams = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(imageViewParams);
This will make the edit text to fill the remaining space between the textview and imageview. If you need something else, just modify the code a bit. When using weights, set the weight or height as 0 for which you are setting weight for.