9

If I use xml file I have a option called layout_margin (for example layout_margin ="1dp" for text view ) but I want to set programmatically and I dont know how to do it.

Hiral Vadodaria
  • 18,920
  • 5
  • 37
  • 56
sivan esan
  • 143
  • 1
  • 3
  • 8

8 Answers8

15
   LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)textview.getLayoutParams();
     params.setMargins(20, 0, 0, 0); 
     textview.setLayoutParams(params);
Andro Selva
  • 53,136
  • 52
  • 190
  • 238
6

Please Google before adding your question to StackOverflow.

TextView tv = (TextView)findViewById(R.id.my_text_view);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)tv.getLayoutParams();
params.setMargins(0, 0, 10, 0); //substitute parameters for left, top, right, bottom
tv.setLayoutParams(params);
MicroEyes
  • 3,620
  • 4
  • 24
  • 35
3

You can do by this:

TextView text = (TextView) findViewById(R.id.text);   
LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);//pass int values for left,top,right,bottom
text.setLayoutParams(params);
Jignesh Jain
  • 1,478
  • 12
  • 26
AkashG
  • 7,787
  • 3
  • 27
  • 43
2
TextView tv = (TextView) findViewById(R.id.tvId);   
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
llp.setMargins(50, 0, 0, 0); // llp.setMargins(left, top, right, bottom);
tv.setLayoutParams(llp);
Nermeen
  • 15,770
  • 5
  • 57
  • 71
2

Note, that not all LayoutParams has method setMargins();

RelativeLayout, LinearLayout etc has their own inner class LayoutParams, so availability of setMargins is not always available.

ViliusK
  • 10,879
  • 4
  • 64
  • 70
1

Try This : It worked ....

LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);

Params.SetMargins(0, 10, 0, 0);

FirstName.LayoutParameters = Params;
AlexB
  • 6,970
  • 12
  • 51
  • 71
Ankita
  • 11
  • 1
  • 1
    I don't know if you have really tested it but as of 07.23 it is full of errors and you don't name variables starting with the capital letter. I don't recommend you nswering questions without really testing them – Vendetta8247 Jul 23 '15 at 16:20
0

You can use the setMargins() on the LinearLayout.LayoutParams. See the answer of this StackOverflow question for more information.

Community
  • 1
  • 1
Angelo
  • 4,288
  • 2
  • 29
  • 37
0
        TextView tv = (TextView)findViewById(R.id.item_title));
        RelativeLayout.LayoutParams mRelativelp = (RelativeLayout.LayoutParams) tv
                    .getLayoutParams();
        mRelativelp.setMargins(DptoPxConvertion(15), 0, DptoPxConvertion (15), 0);
        tv.setLayoutParams(mRelativelp);

    private int DptoPxConvertion(int dpValue)
    {
       return (int)((dpValue * mContext.getResources().getDisplayMetrics().density) + 0.5);
    }

getLayoutParams() of textview should be casted to the corresponding Params based on the Parent of the textview in xml.

<RelativeLayout>
   <TextView
    android:id="@+id/item_title">
</RelativeLayout>

If parent of the TextView is RelativeLayout means, then RelativeLayout.LayoutParams as above. If parent is LinearLayout means then

LinearLayout.LayoutParams mLinearlp = (LinearLayout.LayoutParams) tv
                    .getLayoutParams();

To render the same real size on different devices use DptoPxConvertion() method which I have used above. setMargin(left,top,right,bottom) params will take values in pixel not in dp.

anand krish
  • 3,655
  • 4
  • 37
  • 44