Reputation: 4108
I created LinearLayout
and Button
via class not via XML. I know how to inflate layout view and button view dont know how to set margins of them. I need to set margins of my button and my linearlayout. If I use XML, it is very simple: <android:marginLeft="10px">
.
But, what should I do if I want to set margin by class not by XML?
Upvotes: 0
Views: 1933
Reputation: 2936
In this we have linear layout in main.xml named lyt1 and we add edittext at runtime and set left margin value
please use bleow code :
lyt = (LinearLayout)findViewById(R.id.lyt1);
EditText txt = new EditText(WvActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
lp.leftMargin = 10;
txt.setLayoutParams(lp);
lyt.addView(txt);
lyt.invalidate();
Upvotes: 2
Reputation: 29199
Use:
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
Upvotes: 2