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.
- 
                    Check out this - http://stackoverflow.com/questions/2481455/set-margins-in-a-linearlayout-programmatically – AkashG Jul 16 '12 at 12:51
- 
                    please look at: http://stackoverflow.com/questions/4814124/how-to-change-margin-of-textview – Hiral Vadodaria Jul 16 '12 at 12:51
- 
                    Please see this way at KOTLIN https://stackoverflow.com/a/72072222/12272687 – Mori Apr 30 '22 at 21:15
8 Answers
   LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)textview.getLayoutParams();
     params.setMargins(20, 0, 0, 0); 
     textview.setLayoutParams(params);
 
    
    - 53,910
- 52
- 193
- 240
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);
 
    
    - 3,680
- 4
- 26
- 35
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);
 
    
    - 1,518
- 12
- 28
 
    
    - 7,868
- 3
- 28
- 43
- 
                    I precise , its : params.setMargins(text.setMargins(left, top, right, bottom);//pass int values for left,top,right,bottom params.setLayoutParams(params); – Bourdier Jonathan Nov 10 '14 at 20:59
- 
                    
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.
 
    
    - 11,345
- 4
- 67
- 71
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);
 
    
    - 15,883
- 5
- 59
- 72
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);
- 
                    1I 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
You can use the setMargins() on the LinearLayout.LayoutParams. See the answer of this StackOverflow question for more information.
        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.
 
    
    - 4,281
- 4
- 44
- 47
 
     
     
     
     
    