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

  • Home
  • SEARCH
  • 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 9222199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:46:27+00:00 2026-06-18T03:46:27+00:00

I am having some spacing trouble when building part of my UI programmatically in

  • 0

I am having some spacing trouble when building part of my UI programmatically in Android 4.0. I am trying to add stylized buttons to a stylized LinearLayout. To space the buttons equally, each one is wrapped in a LinearLayout with a weight of 1. I started with a layout defined in XML (somewhat of a proof of concept,) which renders like I expect:

<LinearLayout android:id="@+id/dialog_footer" 
        android:layout_width="500dp"
        android:layout_height="wrap_content"
        android:background="@drawable/dialog_footer">

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:gravity="center">
        <Button android:id="@+id/cancel"
            style="@style/Button"
            android:layout_width="130dp"
            android:layout_height="38dp"
            android:text="Cancel" />
    </LinearLayout>

    <!-- Another LinearLayout with a nested Button like the one above -->

</LinearLayout>

To add buttons programmatically, I removed the inner LinearLayouts and put them in their own layout file that I can inflate and add to the outer LinearLayout in Java. It is nearly identical.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:gravity="center" >
    <Button android:id="@+id/button"
            style="@style/Button"
            android:layout_width="130dp"
            android:layout_height="38dp" />
</LinearLayout>

And this is roughly how I’m adding buttons in code:

LinearLayout dialogFooter = (LinearLayout)dialogView.findViewById(R.id.dialog_footer);
LinearLayout wrappedButton = (LinearLayout)getLayoutInflater().inflate(R.layout.dialog_button_wrapped, null);

Button button = (Button)wrappedButton.findViewById(R.id.button);
button.setText(R.string.button_one_text);

// button.setOnClickListener(...);

dialogFooter.addView(wrappedButton);

The buttons appear but now they are grouped together and shifted to the left. Is there something Android does when it parses a Layout that I would need to do myself if I’m adding to the dialog_footer? Since weights come into play here, I thought that calling setWeightSum() on the container I’m adding to (dialog_footer) might be necessary but that didn’t help. Does anyone have any ideas what might be causing the difference between the XML and Java approaches?

  • 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-06-18T03:46:29+00:00Added an answer on June 18, 2026 at 3:46 am

    I believe this is your problem:

    LinearLayout wrappedButton = (LinearLayout)getLayoutInflater().inflate(R.layout.dialog_button_wrapped, null);
    

    The null should be replaced with the parent view , so that it will get the layoutParams you want to set for it .

    Another thing is about the weight you set – you should set the width/height to 0px so that the weight won’t cause the layout process work in a weird/inefficient way .

    BTW , you can remove the inner layout (that has the button) and use a single button instead. just set the layout_gravity there to center_horizontal .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble trying to achieve some very basic layout behavior with Auto Layout.
Having some trouble with this code. Trying to return a tuple of tuples (coordinates)
Having some trouble with R's garbage collection, when passing objects to C++. We have
Having some trouble getting a screenscraper webservice up and running on a weblogic 10.3.3
Having some trouble with what should be a very simple scenario. For example purposes,
Am having some trouble with the SkyDrive download process and hoping you can help
Basically having some trouble with using Hover to hide or show an item. The
I am using jqplot and am having trouble with the spacing on the X
Having some trouble with some code i wrote 2 years ago, Getting spammed with
Having some trouble declaring functions within my script: %Read the raw audio data refData

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.