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 8031817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:13:14+00:00 2026-06-05T01:13:14+00:00

In my Android app I need dynamically create few LinearLayout’s with text. But I

  • 0

In my Android app I need dynamically create few LinearLayout’s with text.

But I can not set weight of each element. I want that LL looks like in XML part:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10px"
    android:weightSum="1"
    android:id="@+id/qwe">
<TextView 
    android:layout_weight="0.1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="some"
/>
<TextView 
    android:layout_weight="0.8"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="word"
/>
<TextView 
    android:layout_weight="0.1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="here"
    android:gravity="right"
/>
</LinearLayout>

It looks good but I need the same dynamically.
In Java code I wrote:

LinearLayout ll = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 10, 10, 10);
ll.setLayoutParams(layoutParams);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setBackgroundColor(0xFF888888);
rootLL.addView(ll);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(10, 10, 10, 10);

LinearLayout.LayoutParams params1 = params;
params1.weight = 0.15f;
TextView one = new TextView(context);
one.setLayoutParams(params1);
one.setText("some");
ll.addView(one);

LinearLayout.LayoutParams params2 = params;
params2.weight = 0.7f;
TextView two = new TextView(context);
two.setLayoutParams(params2);
two.setText("word");
ll.addView(two);

LinearLayout.LayoutParams params3 = params;
params3.weight = 0.15f;
TextView three = new TextView(context);
three.setLayoutParams(params3);
three.setText("here");
ll.addView(three);

But in this case I obtain three textView’s with equal width. It looks that I did not add weightSum attribute for main LL but I don’t know how to do it.

  • 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-05T01:13:16+00:00Added an answer on June 5, 2026 at 1:13 am
    1. prefer integer over float . this way you can get any kind of fraction you wish (even 1/3) .

    2. if you set the weight of each of the views , you don’t need to set the weightSum .

    3. if you set the weightSum , you can leave one view without any weight , giving it the rest of the space available.

    4. it seems you give all of the views the same layoutparams instead of cloning it for each of them . when you use “params2 = params;” , it means that you set a reference to it and not that you create a new one . in the end of the method , all will point to the same layoutParams , with the weight of 0.15f (since that’s the last one) .

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

Sidebar

Related Questions

I'm working with an existing Android app and I need to have a dynamically
I need to develop an android app that achieves the following: Create an activity
I am trying to create my first Android app and need some help. Basically
So for my Android app I need a centralized server so multiple phones can
My Android app need the user to create an account to be able to
I'm starting to create an iPhone/Android app that will need to use Plone users
In android app, I need to dynamically generate html and load that into webview
In my android app I need to display a text on an image. The
I need to dynamically add some tablerows to a linearlayout in my app. I
My android app project need to add a new function of saving the click

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.