How to Set Margins for Textview Programmatically

How to set margins for TextView programmatically?

set to LayoutParams.

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(10,10,10,10);
tv1.setLayoutParams(params);

Set margins from textviews programmatically

Margin is set in the LayoutParams (e.g., RelativeLayout.LayoutParams in your case) as

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
viewEvent.setLayoutParams(params);

Update

If you need to add the TextViews vertically then use LinearLayout as

<!-- inside scrollview -->
<LinearLayout
android:id="@+id/eventLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />

And programmatically as

LinearLayout linearLayout = (LinearLayout) eventView.findViewById(R.id.eventLinearLayout);

for (int i = 0; i < partEvents.length; i++) {

TextView viewEvent = new TextView(getActivity());

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);
viewEvent.setLayoutParams(params);

// rest of your code

linearLayout.addView(viewEvent);
}

Using LinearLayout, would automatic handling your TextView expansion and shrinking.

In Android, how do I set margins in dp programmatically?

You should use LayoutParams to set your button margins:

LayoutParams params = new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
yourbutton.setLayoutParams(params);

Depending on what layout you're using you should use RelativeLayout.LayoutParams or LinearLayout.LayoutParams.

And to convert your dp measure to pixel, try this:

Resources r = mContext.getResources();
int px = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
yourdpmeasure,
r.getDisplayMetrics()
);

How to set margin of textview programatically within tablerow

Try the below code and let me know if it works

android.widget.TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 13, 17, 9);
row1.addView(jenis, layoutParams);
//ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams)jenis.getLayoutParams();
//mlp.setMargins(37, 13, 17, 0);

row1.addView(spin, layoutParams);

How Can I Set Margin with 'dp' programmatically?

Well you got to know the parent layout first if it's RelativeLayout you'll need:

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(this);

and if it is Linear,Make sure you replace Relative with Linear to get the right layoutParams.Now you can use:

layoutParams.setMargins(i1,i2,i3,i4);

all the values should be in pixel so if you need a method,it should convert dp to px for you.Feel free to use mine then:

public int dpToPx(Context context, float dp) {
return Math.round(dp * getDisplayMetrics(context).density);
}

now just set the layoutParams to the child view:

textView.setLayoutParams(layoutParams);

android set margin programmatically

Try this code:

parameter =  (RelativeLayout.LayoutParams) txtField.getLayoutParams();
parameter.setMargins(leftMargin, parameter.topMargin, parameter.rightMargin, parameter.bottomMargin); // left, top, right, bottom
txtField.setLayoutParams(parameter);


Related Topics



Leave a reply



Submit