Reputation: 513
I declared a linearlayout with horizontal orientation programatically.
How to set width,height,margins for that layout dynamically.
I tried like the below :
leftMargin = 40;
rightMargin = 20;
widgetWidth = 300;
topMargin = 5;
bottomMargin = 5;
widgetHeight = 100;
LayoutParams params = new LayoutParams((int) widgetWidth, (int) widgetHeight);
params.setMargins((int) leftMargin, (int) topMargin,
(int) rightMargin, (int) bottomMargin);
newHorizontalLayout = new LinearLayout(ctx);
newHorizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
newHorizontalLayout.setLayoutParams(params);
But I'm getting null pointer exception.
can anyone help me in sorting out his issue.
Upvotes: 0
Views: 251
Reputation: 3824
First of all you have to import correct layout. Here is a example of table-row layoutParams:
To set height and width:
TextView tb_row_proName;
android.widget.TableRow.LayoutParams lp_pro_name = new android.widget.TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT,
android.widget.TableRow.LayoutParams.WRAP_CONTENT);
tb_row_proName.setLayoutParams(lp_pro_name);
To set margin:
int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, r.getDisplayMetrics());
lp_pro_name.bottomMargin = margin;
Upvotes: 1