Why does setMargins() not work on buttons?











up vote
0
down vote

favorite












Why does setMargins() not work on buttons?



Screen



public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

GridLayout gridLayout = new GridLayout(this);
gridLayout.setRowCount(5);
gridLayout.setColumnCount(7);
setContentView(gridLayout);

FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(100,100);
layoutParams.setMargins(10,10,10,10);

for(int i=1;i<31;i++) {
Button button = new Button(this);
button.setText(String.valueOf(i));
button.setTextSize(18);
button.setBackgroundResource(R.drawable.button);
gridLayout.addView(button,layoutParams);
}



}
}









share|improve this question




















  • 1




    While it is fine to post a screenshot of an application / web page, posting screenshot of code should be avoided for many reasons. Please edit your question to add the relevant code. If possible produce a MCVE
    – Gabriel Devillers
    Nov 7 at 19:20












  • Can you please put your code in your question.
    – chetan mahajan
    Nov 7 at 19:39















up vote
0
down vote

favorite












Why does setMargins() not work on buttons?



Screen



public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

GridLayout gridLayout = new GridLayout(this);
gridLayout.setRowCount(5);
gridLayout.setColumnCount(7);
setContentView(gridLayout);

FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(100,100);
layoutParams.setMargins(10,10,10,10);

for(int i=1;i<31;i++) {
Button button = new Button(this);
button.setText(String.valueOf(i));
button.setTextSize(18);
button.setBackgroundResource(R.drawable.button);
gridLayout.addView(button,layoutParams);
}



}
}









share|improve this question




















  • 1




    While it is fine to post a screenshot of an application / web page, posting screenshot of code should be avoided for many reasons. Please edit your question to add the relevant code. If possible produce a MCVE
    – Gabriel Devillers
    Nov 7 at 19:20












  • Can you please put your code in your question.
    – chetan mahajan
    Nov 7 at 19:39













up vote
0
down vote

favorite









up vote
0
down vote

favorite











Why does setMargins() not work on buttons?



Screen



public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

GridLayout gridLayout = new GridLayout(this);
gridLayout.setRowCount(5);
gridLayout.setColumnCount(7);
setContentView(gridLayout);

FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(100,100);
layoutParams.setMargins(10,10,10,10);

for(int i=1;i<31;i++) {
Button button = new Button(this);
button.setText(String.valueOf(i));
button.setTextSize(18);
button.setBackgroundResource(R.drawable.button);
gridLayout.addView(button,layoutParams);
}



}
}









share|improve this question















Why does setMargins() not work on buttons?



Screen



public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

GridLayout gridLayout = new GridLayout(this);
gridLayout.setRowCount(5);
gridLayout.setColumnCount(7);
setContentView(gridLayout);

FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(100,100);
layoutParams.setMargins(10,10,10,10);

for(int i=1;i<31;i++) {
Button button = new Button(this);
button.setText(String.valueOf(i));
button.setTextSize(18);
button.setBackgroundResource(R.drawable.button);
gridLayout.addView(button,layoutParams);
}



}
}






android






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 20:30









forpas

4,4441216




4,4441216










asked Nov 7 at 19:11









Poltorashka

12




12








  • 1




    While it is fine to post a screenshot of an application / web page, posting screenshot of code should be avoided for many reasons. Please edit your question to add the relevant code. If possible produce a MCVE
    – Gabriel Devillers
    Nov 7 at 19:20












  • Can you please put your code in your question.
    – chetan mahajan
    Nov 7 at 19:39














  • 1




    While it is fine to post a screenshot of an application / web page, posting screenshot of code should be avoided for many reasons. Please edit your question to add the relevant code. If possible produce a MCVE
    – Gabriel Devillers
    Nov 7 at 19:20












  • Can you please put your code in your question.
    – chetan mahajan
    Nov 7 at 19:39








1




1




While it is fine to post a screenshot of an application / web page, posting screenshot of code should be avoided for many reasons. Please edit your question to add the relevant code. If possible produce a MCVE
– Gabriel Devillers
Nov 7 at 19:20






While it is fine to post a screenshot of an application / web page, posting screenshot of code should be avoided for many reasons. Please edit your question to add the relevant code. If possible produce a MCVE
– Gabriel Devillers
Nov 7 at 19:20














Can you please put your code in your question.
– chetan mahajan
Nov 7 at 19:39




Can you please put your code in your question.
– chetan mahajan
Nov 7 at 19:39












1 Answer
1






active

oldest

votes

















up vote
0
down vote













if you use ConstraintLayout in your layout you should make sure your button had chain from that side you want have margin
example



<Button
android:id="@+id/btn"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
/>


if you dont write



app:layout_constraintBottom_toBottomOf="parent"


in your Button's tag you cant set margin from bottom
and otherside like this.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196232%2fwhy-does-setmargins-not-work-on-buttons%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    if you use ConstraintLayout in your layout you should make sure your button had chain from that side you want have margin
    example



    <Button
    android:id="@+id/btn"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    />


    if you dont write



    app:layout_constraintBottom_toBottomOf="parent"


    in your Button's tag you cant set margin from bottom
    and otherside like this.






    share|improve this answer

























      up vote
      0
      down vote













      if you use ConstraintLayout in your layout you should make sure your button had chain from that side you want have margin
      example



      <Button
      android:id="@+id/btn"
      android:layout_marginBottom="8dp"
      app:layout_constraintBottom_toBottomOf="parent"
      />


      if you dont write



      app:layout_constraintBottom_toBottomOf="parent"


      in your Button's tag you cant set margin from bottom
      and otherside like this.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        if you use ConstraintLayout in your layout you should make sure your button had chain from that side you want have margin
        example



        <Button
        android:id="@+id/btn"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        />


        if you dont write



        app:layout_constraintBottom_toBottomOf="parent"


        in your Button's tag you cant set margin from bottom
        and otherside like this.






        share|improve this answer












        if you use ConstraintLayout in your layout you should make sure your button had chain from that side you want have margin
        example



        <Button
        android:id="@+id/btn"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        />


        if you dont write



        app:layout_constraintBottom_toBottomOf="parent"


        in your Button's tag you cant set margin from bottom
        and otherside like this.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 19:29









        Ardalan Sajedi

        11




        11






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53196232%2fwhy-does-setmargins-not-work-on-buttons%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini