How to set margin dynamically in Android?
Asked Answered
F

6

38

I am currently doing an android application that contains customize alert dialog. It contains a button , but i can't set the margin for the button . the code is given below. setmargin method is not working

AlertDialog.Builder myDialog = new AlertDialog.Builder(Login.this);
Button button = new Button(Login.this);

button.setText("Send");
LayoutParams buttonLayoutParams 
    = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

button.setLayoutParams(buttonLayoutParams);

resetPassword=editText.getText().toString();

LinearLayout layout = new LinearLayout(Login.this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textView);
layout.addView(editText);
layout.addView(button);

myDialog.setView(layout);
Forethought answered 16/6, 2012 at 9:15 Comment(0)
P
73

Write below code to set margin, it may help you.

AlertDialog.Builder myDialog = new AlertDialog.Builder(Login.this);
Button button = new Button(Login.this);
EditText editText = new EditText(Login.this);
TextView textView = new TextView(Login.this);
button.setText("Send");
LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
buttonLayoutParams.setMargins(50, 10, 0, 0);
button.setLayoutParams(buttonLayoutParams);
String resetPassword = editText.getText().toString();
LinearLayout layout = new LinearLayout(Login.this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textView);
layout.addView(editText);
layout.addView(button);
myDialog.setView(layout);
myDialog.show();

Use LinearLayout.LayoutParams or RelativeLayout.LayoutParams according to parent layout of the child view

Publias answered 16/6, 2012 at 9:22 Comment(2)
no it is not working. it shows an error add cast to buttonlayoutparams in setMargin method.Forethought
Please Import import android.widget.LinearLayout.LayoutParams; in your activity.Publias
V
6

Just sharing a slightly different approach.

Instead of casting to LinearLayout.LayoutParams, RelativeLayout.LayoutParams, etc, you can just cast to MarginLayoutParams.

Casting to MarginLayoutParams is better because you can later update your layout and you don't need to return to your java code to change from LinearLayout to RelativeLayout or any other Layout type

You can do something like:

MarginLayoutParams layoutParams = (MarginLayoutParams) view.getLayoutParams();

// Set bottom margin
layoutParams.bottomMargin = x;

// Set top margin
layoutParams.topMargin = x;

// Set left margin
// This won't have effect if you set any relative margin (start) previously or in the layout.xml
layoutParams.leftMargin = x;

// Set left margin
// This won't have effect if you set any relative margin (end) previously or in the layout.xml
layoutParams.rightMargin = x;

// Set start margin
layoutParams.setMarginStart(x);

// Set end margin
layoutParams.setMarginStart(x);

// Set all left, top, right, bottom margins at once
// Note that here, left and right margins are set (not start/end).
// So, if you have used start/end margin before (or via layout.xml), 
// setting left/right here won't have any effect.
layoutParams.setMargins(left, top, end, bottom)

// Then re-apply the layout params again to force the view to be re-draw
// This step may not be necessary because depending where you set the margin, 
// view is already scheduled to be drawn
// For any case, to ensure the view will apply the new margin, call:
view.setLayoutParams(layoutParams);
Verde answered 26/6, 2019 at 15:59 Comment(0)
O
4
buttonLayoutParams.bottomMargin
buttonLayoutParams.topMargin
buttonLayoutParams.leftMargin
buttonLayoutParams.rightMargin

can be used to set margins

Osswald answered 16/6, 2012 at 9:18 Comment(1)
Are you setting the layout parameters for your LinearLayout?Osswald
M
2

The setMargin() method is available if you're using LinearLayout.LayoutParams but not if you're using ViewGroup.LayoutParams. Dipak Keshariya alludes to this but doesn't say it in so many words.

Maurya answered 8/5, 2014 at 16:49 Comment(0)
G
2
You can set in LinearLayout margin

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
Button okButton=new Button(this);
okButton.setText("some text");
ll.addView(okButton, layoutParams);
Guillermo answered 2/2, 2016 at 9:37 Comment(0)
C
0

This works for me (The support library (need to use AndroidX):

Kotlin

val params = LinearLayoutCompat.LayoutParams(
               LinearLayout.LayoutParams.WRAP_CONTENT,
               LinearLayout.LayoutParams.WRAP_CONTENT
               ).apply {
                 setMargins(0,16,0,16)
               }

Java

LinearLayoutCompat.LayoutParams params = LinearLayoutCompat.LayoutParams(
                   LinearLayout.LayoutParams.WRAP_CONTENT,
                   LinearLayout.LayoutParams.WRAP_CONTENT
                   )
                   params.setMargins(0,16,0,16)
Caralie answered 6/8, 2021 at 2:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.