How can I set the value for the attribute layout_weight
for button in android dynamically from java code ?

- 2,515
- 6
- 31
- 45

- 63,550
- 98
- 229
- 344
-
It'll be very similar to this question/answer: http://stackoverflow.com/questions/4638832/how-to-programmatically-set-the-layout-align-parent-right-attribute-of-a-button-i/4639012#4639012 and this one: http://stackoverflow.com/questions/4637233/how-to-set-layout-span-through-code/4637266#4637266 – Rich Schuler Jan 09 '11 at 19:07
-
If you have already defined view in layout(xml) file, then creating new LayoutParams overwrites other params defined in xml file. So first you should use "getLayoutParams". See full answer http://stackoverflow.com/questions/4641072/how-to-set-layout-weight-attribute-dynamically-from-code/38011495#38011495 – Shirish Herwade Jun 24 '16 at 10:44
13 Answers
You can pass it in as part of the LinearLayout.LayoutParams
constructor:
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
1.0f
);
YOUR_VIEW.setLayoutParams(param);
The last parameter is the weight.

- 3,392
- 3
- 36
- 47

- 51,744
- 11
- 75
- 60
-
2It should be `param = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, (float) 1.0);` – Mithun Sreedharan Sep 01 '11 at 12:55
-
56
-
11
-
5
-
10
-
3I think, one of two parameters LayoutParams.MATCH_PARENT should be changed to 0. – CoolMind Dec 15 '15 at 10:24
-
@Erich Douglass: Thanks. Working properly for me. I have used same as you mentioned above. I am using this to divide screen in 2 parts horizontally. – BK19 Oct 07 '16 at 07:18
-
@Erich what if i want to set 52% of weight, i have tried 0.52f, but it's not working – TapanHP Dec 29 '16 at 11:47
-
If all you want to do is change the weight of an existing view use parameters: view.getLayoutParams().width, view.getLayoutParams().height,
– jwehrle Jun 04 '17 at 20:04
Use LinearLayout.LayoutParams
:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.weight = 1.0f;
Button button = new Button(this);
button.setLayoutParams(params);
EDIT: Ah, Erich's answer is easier!

- 133,643
- 45
- 263
- 274
-
28Erich's anser may be easier to write, but yours is easier to read. Since it says which attribute the 1.0f is used for. – Johan Apr 02 '12 at 13:28
-
3yours established that you can modify weight on the fly by calling 'view.getLayoutParams()' then modify the value. – RobGThai Apr 23 '13 at 07:58
-
1`FILL_PARENT` has been deprecated and `MATCH_PARENT` should be used instead. – Simon Forsberg Nov 01 '13 at 21:45
-
@SimonAndréForsberg Unfortunately many of us need to use `FILL_PARENT` still to support older versions of Android, and in the end, they both defined as the constant -1 for use in the layout. Since `MATCH_PARENT` can cause errors in older Android, in many cases it is easier to just continue to use `FILL_PARENT` – LocalPCGuy Feb 24 '14 at 21:20
-
2@LocalPCGuy Not true. As long as you're *targeting* SDK >= 8 (which you absolutely should be), `MATCH_PARENT` can be used instead of `FILL_PARENT` even on older devices. – Kevin Coppock Feb 24 '14 at 21:24
-
2@LocalPCGuy [No one uses API 7 or less anymore](http://developer.android.com/about/dashboards/index.html). If you're trying to make your app compatible with APIs 7 or less, you'd probably run into a whole lot of other problems before worrying about `FILL_PARENT` vs. `MATCH_PARENT`. – Simon Forsberg Feb 24 '14 at 21:26
-
Good point. I actually ran into it on an app that was originally designed to be compatible with those versions, and so had to maintain that compatibility. Guess I just never looked at exactly where the cutoff was. – LocalPCGuy Feb 24 '14 at 22:22
If you already define your view in your layout(xml) file, only want to change the weight programmatically, this way is better
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
mButton.getLayoutParams();
params.weight = 1.0f;
mButton.setLayoutParams(params);
new a LayoutParams overwrites other params defined in you xml file like margins, or you need to specify all of them in LayoutParams.
-
3With "params.weight = 1f;" you're already setting the weight on the LayoutParams object. There's no need to call "mButton.setLayoutParams(params);". – MrMaffen Mar 28 '15 at 20:32
-
2
-
1This is actually the most elegant answer and it directly changes a specific UI object. The best answer is good too and older than this post. So it deserves as the best answer. I upvoted this post though. – The Original Android Jul 28 '15 at 01:51
-
-
-
-
You can actually just call `mButton.requestLayout()` instead `setLayoutParams` since you're just changing a value. `setLayoutParams` calls `requestLayout` internally. – JohnnyLambada Aug 01 '18 at 16:42
If the constructor with width, height and weight is not working, try using the constructor with width and height. And then manually set the weight.
And if you want the width to be set according to the weight, set width as 0 in the constructor. Same applies for height. Below code works for me.
LinearLayout.LayoutParams childParam1 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT);
childParam1.weight = 0.3f;
child1.setLayoutParams(childParam1);
LinearLayout.LayoutParams childParam2 = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT);
childParam2.weight = 0.7f;
child2.setLayoutParams(childParam2);
parent.setWeightSum(1f);
parent.addView(child1);
parent.addView(child2);

- 2,760
- 1
- 18
- 33

- 351
- 4
- 4
-
Just what I needed! Every one is talking about setting weights here, no one displayed what happens when those weighed Children are added to the Parent View! I was actually looking for how to add weightSum on the ParentView! Thanks! – sud007 Mar 17 '16 at 07:12
If layoutparams
is already defined (in XML or dynamically), Here's a one liner:
((LinearLayout.LayoutParams) mView.getLayoutParams()).weight = 1;

- 22,411
- 20
- 73
- 96
If I someone looking for answer, use this:
LinearLayout.LayoutParams lay = (LinearLayout.LayoutParams) myLayout.getLayoutParams();
lay.weight = 0.5;
If you are initializing your layout from xml file, this will be much more convenient than providing new layout parameters for Linear Layout.

- 845
- 1
- 10
- 20
-
This should be the accepted answer as it doesn't modify any previously set variables like width/height/margin/padding etc. Everything is being kept and re-used while the weight is changed. – MrMaffen Mar 28 '15 at 20:34
-
2If the view to set `layout_weight` has already been laid out, you need to call `View.requestLayout()` to update. – Attacktive Sep 21 '15 at 06:03
Any of LinearLayout.LayoutParams
and TableLayout.LayoutParams
worked for me, for buttons the right one is TableRow.LayoutParams
. That is:
TableRow.LayoutParams buttonParams = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT, 1f);
About using MATCH_PARENT
or WRAP_CONTENT
be the same.

- 6,006
- 2
- 32
- 28

- 625
- 7
- 24
-
3`FILL_PARENT` has been deprecated and `MATCH_PARENT` should be used instead. – Simon Forsberg Nov 01 '13 at 21:44
-
-
You call the `LayoutParams` of the direct parent. I assume you had a crash attempting to use `LinearLayout.LayoutParams` without the parent `LinearLayout`. – Abandoned Cart Feb 24 '20 at 05:03
If you have already defined your view in layout(xml) file and only want to change the weight pro grammatically, then then creating new LayoutParams overwrites other params defined in you xml file.
So first you should use "getLayoutParams" and then setLayoutParams
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mButton.getLayoutParams();
params.weight = 4f;
mButton.setLayoutParams(params);

- 11,461
- 20
- 72
- 111
I have done my task by adding this line of code!
LinearLayout.LayoutParams mylayout = (LinearLayout.LayoutParams)
myview.getLayoutParams();
mylayout.weight = 2;
myview.setLayoutParams(mylayout);

- 181
- 1
- 4
Using Kotlin you can do something like:
import android.content.Context
import android.support.v4.content.ContextCompat
import android.support.v7.widget.CardView
import android.widget.*
import android.widget.LinearLayout
class RespondTo : CardView {
constructor(context: Context) : super(context) {
init(context)
}
private fun init(context: Context) {
val parent = LinearLayout(context)
parent.apply {
layoutParams = LinearLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT, 1.0f).apply {
orientation = LinearLayout.HORIZONTAL
addView(EditText(context).apply {
id = generateViewId()
layoutParams = LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 0.9f).apply {
}
})
addView(ImageButton(context).apply({
layoutParams = LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 0.1f)
background = null
setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_save_black_24px))
id = generateViewId()
layoutParams = RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT).apply {
addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
// addRule(RelativeLayout.LEFT_OF, myImageButton.id)
}
}))
}
}
this.addView(parent)
}
}

- 22,789
- 24
- 132
- 203
if you want to set weight to LinearLayout then use code mentioned below or set weight to Relative Layout then replace it with Relative Layout
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT,
1.0f
); YOUR_VIEW.setLayoutParams(param);

- 171
- 1
- 4
You can try this approach. First, you get the LayoutParams associated with this Button. Perform a type conversion.
LinearLayout.LayoutParams linearLayoutParams = (LinearLayout.LayoutParams) button.getLayoutParams();
Then assign the required value of the "weight" parameter.
linearLayoutParams.weight = 5;
Redraw the layout.
button.requestLayout();

- 29
- 3
This is how you do it in Kotlin:
val params = mButton.layoutParams as LinearLayout.LayoutParams
params.weight = 1.0f
mButton.layoutParams = params

- 64,245
- 87
- 278
- 460