Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (4k points)

I'm trying to use Java (not XML) to create a LinearLayout with buttons that fill the screen, and have margins. Here is code that works without margins:

LinearLayout buttonsView = new LinearLayout(this);

buttonsView.setOrientation(LinearLayout.VERTICAL);

for (int r = 0; r < 6; ++r) {

    Button btn = new Button(this);

    btn.setText("A");

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); // Verbose!

    lp.weight = 1.0f; // This is critical. Doesn't work without it.

    buttonsView.addView(btn, lp);

}

ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);

setContentView(buttonsView, lp);

So that works fine, but how on earth do you give the buttons margins so there is space between them? I tried using LinearLayout.MarginLayoutParams, but that has no weightmember so it's no good. And it doesn't work if you pass it lpin its constructor either.

Is this impossible? Because it sure looks it, and it wouldn't be the first Android layout task you can only do in XML.

2 Answers

0 votes
by (119k points)

You can try this code to create a LinearLayout with buttons that fill the screen and have margins:

 LayoutParams params = new LayoutParams(

            LayoutParams.WRAP_CONTENT,      

            LayoutParams.WRAP_CONTENT

    );

    params.setMargins(left, top, right, bottom);

    yourbutton.setLayoutParams(params);

If you want to learn Java from the top experts, then check out this Java EE Certification Course by Intellipaat.

0 votes
ago by (1.9k points)

One way to resolve this issue is you can directly add the margins to the button by using LinearLayout.LayoutParams and set the margins directly on each button’s layout parameters . here’s how you can adjust your code :- 

 

LinearLayout buttonsView = new LinearLayout(this);

buttonsView.setOrientation(LinearLayout.VERTICAL);

 

for (int r = 0; r < 6; ++r) {

    Button btn = new Button(this);

    btn.setText("A");

 

    // Set layout parameters with weight and add margins

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(

        LinearLayout.LayoutParams.MATCH_PARENT,

        0, // This will make the button’s height proportional based on weight

        1.0f // Weight is 1 for each button, distributed evenly

    );

 

    lp.setMargins(20, 10, 20, 10); // Add margins (left, top, right, bottom)

    buttonsView.addView(btn, lp);

}

 

// Set layout parameters for the main LinearLayout to fill the screen

ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(

    ViewGroup.LayoutParams.MATCH_PARENT,

    ViewGroup.LayoutParams.MATCH_PARENT

);

 

setContentView(buttonsView, lp);


 


You can set the margins using the setMargins() method on LinearLayout.LayoutParams for each button and you also need to set the height to 0 with the weight because it ensures that each button takes up an equal portion of available space within the parent layout 

This way you can achieve weight-based distribution and margins without needing XML.

Related questions

0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...