Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android set button margin programmatically

Tags:

android

button

I know this question has been asked before, like this:

similar questions

But my issue is when I do this:

LayoutParams params = new LayoutParams(
        LayoutParams.WRAP_CONTENT,      
        LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);

It shows there's no setMargins for params. Who can help?

like image 934
David Avatar asked Aug 27 '15 06:08

David


3 Answers

LayoutParams params = new LayoutParams(
            LayoutParams.WRAP_CONTENT,      
            LayoutParams.WRAP_CONTENT
    );
params.setMargins(left, top, right, bottom);

You are missing this line i guess

button.setLayoutParams(params);
like image 199
Hanish Sharma Avatar answered Nov 20 '22 07:11

Hanish Sharma


You need to use this:

LinearLayout.LayoutParams or RelativeLayout.LayoutParams.

like image 27
Vigor Avatar answered Nov 20 '22 08:11

Vigor


You are missing setLayoutParams(params) Attribute

    LayoutParams params = new LayoutParams(
        LayoutParams.WRAP_CONTENT,      
        LayoutParams.WRAP_CONTENT
);
params.setMargins(left, top, right, bottom);
Your_Layout.setLayoutParams(params);

I hope it will helps you .

like image 6
IntelliJ Amiya Avatar answered Nov 20 '22 06:11

IntelliJ Amiya