tags:

views:

113

answers:

1

Hi, I am creating a TextView dynamically from Activity. I created 2 Textviews and I want to maintain some gap between 2 TextViews. How can this be achieved through code rather than from XML. Normally in XML we use android:layout_marginLeft/Right tag to maintain gap between 2 TextViews. How can this be achieved through code?
Thanks in Advance,

A: 

It depends on the layout you are using. This example places a RelativeLayout in a LinearLayout

LinearLayout linearLayoutParent;
RelativeLayout relativeLayout;

RelativeLayout.LayoutParams margin = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
margin.setMargins(0, 0, 0, 7); //7px bottom margin

//get or create the linear and the relative layouts

...

// Add view with its margins
linearLayoutParent.addView(relativeLayout, margin);
Maragues
Thank you, ur suggestion worked for me.
Android_programmer_camera