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
  • 19,158
  • 5
  • 39
  • 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,910
  • 52
  • 193
  • 240
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,680
  • 4
  • 26
  • 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,518
  • 12
  • 28
AkashG
  • 7,868
  • 3
  • 28
  • 43
3

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
  • 11,345
  • 4
  • 67
  • 71
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,883
  • 5
  • 59
  • 72
1

Try This: It worked...

LinearLayout.LayoutParams params = new  LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT,
        LinearLayout.LayoutParams.WRAP_CONTENT
    );

 params.setMargins(0, 10, 0, 0);

firstName.setLayoutParams(params);
Muneesh
  • 433
  • 10
  • 19
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
AggelosK
  • 4,313
  • 2
  • 32
  • 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
  • 4,281
  • 4
  • 44
  • 47