Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically set weight for Button

Tags:

android

I need to implement Dialog for my Android app through Java code, so I can't use XML.

I have root LinearLayout where I implement range seek bar, then I have another LinearLayout under root layout, with horizontal orientation, where I want to add two buttons in same row. So I need to set weight to 1, and width to FILL_PARENT and height to WRAP_CONTENT.

How I can do that with Java code?

like image 261
Zookey Avatar asked Oct 22 '12 17:10

Zookey


People also ask

How do you set linear layout weight programmatically?

LinearLayout. LayoutParams param = new LinearLayout. LayoutParams( 0, LayoutParams. MATCH_PARENT, 1);

What is Layout_weight?

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.

What is layout params?

This set of layout parameters defaults the width and the height of the children to ViewGroup. LayoutParams. WRAP_CONTENT when they are not specified in the XML file. RelativeLayout.LayoutParams. Specifies how a view is positioned within a RelativeLayout .


1 Answers

LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); p.weight = 1;  rangeSeekBar.setLayoutParams(p); 

I'm not sure which view you want to set the layout params on. I just assumed the rangeSeekbar to show an example. Change if you need.

When using the layout params always use the root's param type..

Ex. if you have a View you want to apply params to within a RelativeLayout use RelativeLayout.LayoutParams..

like image 187
dymmeh Avatar answered Sep 28 '22 02:09

dymmeh