Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6371733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:07:22+00:00 2026-05-25T01:07:22+00:00

So I have a LinearLayout in horizontal mode which I add buttons to at

  • 0

So I have a LinearLayout in horizontal mode which I add buttons to at random times in code and I like them all to fill up the space equally.

This is my current code so far:

layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                             ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.FILL;

and I add it to my view here:

linearLayout.addView(button, layoutParams);

All it does is add the buttons as if it was wrap_content and not expanding their width to fill up the available space as in the buttons look left justified.

I also have tried linearLayout.setGravity(Gravity.FILL_HORIZONTAL); and

linearLayout.setWeightSum(++weightSum);
linearLayout.addView(button, layoutParams);

where layoutParams in this case is:

layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                             ViewGroup.LayoutParams.WRAP_CONTENT, 1);

I also tried setting the layout width to zero per answers to other questions.

Am I missing another technique?

Edit: I figured it out, I forgot to set the width of the LinearLayout to match_parent instead of wrap_content.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:07:23+00:00Added an answer on May 25, 2026 at 1:07 am

    Button‘s background has “margins” so they will always have gaps even if there’s no space between buttons. You have to change background to your own image which doesn’t have these margins.

    EDIT: You should use weights for buttons.

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                0, LayoutParams.WRAP_CONTENT);
        params.weight = 1;
        for( int i = 0; i < 5; ++i) {
            Button button = new Button(this);
            button.setText("Button" + (i + 1));
            linearLayout.addView(button, params);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 4 buttons in a horizontal linear layout. I'd like the right edge
I have the following Layout which does not work: <LinearLayout android:orientation=horizontal android:layout_width=match_parent android:id=@+id/experienceLayout android:background=#ffffff
I have a horizontal LinearLayout, inside which I have 2 TextViews. Let's say that
Intro: I have a LinearLayout, which contains two sub LinearLayouts, like so: <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
Ok so I have a layout which looks something like this: LinearLayout TextView LinearLayout
I have a horizontal LinearLayout that's programmatically filled with its children. I would like
I have a Custom ListView that contains a vertical LinearLayout of horizontal LinearLayouts for
I have a LinearLayout which has a background image (a 9 patched png file).
i have my controls in a vertical LinearLayout. All my controls are place below
I have a simple horizontal LinearLayout with four TextViews with the same style inside.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.