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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:11:34+00:00 2026-06-12T01:11:34+00:00

I am trying to create multiple linear layouts inside main LinearLayout. But i am

  • 0

I am trying to create multiple linear layouts inside main LinearLayout. But i am having some trouble with margin. Here is my code and output: –

public class CustomActivity extends Activity {

private LinearLayout mainLayout; 
Float value;
int dpi;
private String[] data = new String[] {"Hello World", "Hello World", "Hello World", "Hello World", ""};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom);
    mainLayout = (LinearLayout)findViewById(R.id.mainLayout);
    mainLayout.setOrientation(LinearLayout.VERTICAL);
    value = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
    dpi = Math.round(value);
    addLayouts();
}

private void addLayouts() {
    int marginBottom = 0;
    int count = 0;
    for (int i = 0; i < data.length; i++) {
        OutLinedLL linearLayout = new OutLinedLL(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dpi*22,dpi*35);
        if(i==0)
        {   
            linearLayout.setBackgroundColor(Color.RED);
            params.setMargins(dpi*7, dpi, dpi*4, marginBottom);
            linearLayout.setLayoutParams(params);
            mainLayout.addView(linearLayout);
        }
        else 
        {
            count += dpi*2;
            Log.e("Count", count+"");
            marginBottom -= (dpi*28)+count;
            Log.e("Margin Bottom", marginBottom+"");
            linearLayout.setBackgroundColor(Color.GREEN);
            params.setMargins(dpi*7, dpi, dpi*4, marginBottom);
            linearLayout.setLayoutParams(params);
            mainLayout.addView(linearLayout);
        }
        marginBottom = 0;
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_custom, menu);
    return true;
}

}

Stack Trace: –

09-29 11:49:23.844: E/Count(16149): 40
09-29 11:49:23.844: E/Margin Bottom(16149): -600
09-29 11:49:23.852: E/Count(16149): 80
09-29 11:49:23.852: E/Margin Bottom(16149): -640
09-29 11:49:23.852: E/Count(16149): 120
09-29 11:49:23.852: E/Margin Bottom(16149): -680
09-29 11:49:23.852: E/Count(16149): 160
09-29 11:49:23.852: E/Margin Bottom(16149): -720

Output :- enter image description here

As you can see margin bottom isn’t same for all the layouts. I am unable to find out why its happening. I am trying to learn custom views at the moment so any help would be really appreciated.

Thanks.

  • 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-12T01:11:35+00:00Added an answer on June 12, 2026 at 1:11 am

    I think this bit is your problem:

    marginBottom -= (dpi*28)+count;
    

    You are changing the amount of margin on each loop through. Just set it to a static value and all your views should have the same margin.

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

Sidebar

Related Questions

I'm having some problems trying to dynamically create multiple RadDocks at the same time.
I'm trying to create a target which matches multiple directories, but no files. Is
I am trying to create multiple proximity alerts but I cant get it to
I am trying to create multiple processes using fork() and execvp() calls, but so
I am trying to create a multiple choice test app. I have this code
i am trying to create tablelayout inside scrollview but it gives me erorr i
I'm trying to create multiple sheets by iteration in phpexcel: $i=0; while ($i <
I am trying to create a multiple image uploader and I have come across
I am trying to create a method that accepts multiple types of controls -
I'm trying to create an app that multiple users would log into a server

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.