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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:30:09+00:00 2026-05-29T21:30:09+00:00

I have a little issue on what sequence things are being called when adding

  • 0

I have a little issue on what sequence things are being called when adding stuff to a RelativeLayout. I have a class extending Activity (name it RelActivity) where I want to create a RelativeLayout and put several custom Views (name it cusView) into that RelativeLayout. The topMargin and leftMargin of a custom View is calculated by using the position of another custom View (i.e. the first custom View has to be positioned directly by setting a number to topMargin and leftMargin). Please note that the Rules of RelativeLayout is not sufficient in this case.

So, over to the problem. In my RelActivity I do this:

  1. Create a RelativeLayout (name it relLayout)
  2. Iterate a cursor with cusViews recieved from a database
  3. For the first cusView -> Set position by topMargin and leftMargin using a LayoutParameter
  4. For the other cusViews -> calculate their topMargin and leftMargin by using one of the other cusViews and a LayoutParameter
  5. Set RelActivity’s contentView to relLayout

What happens is that all cusViews but the first one are squeezed in the top left corner because both leftMargin and topMargin are always calculated to be zero. This happens because I use the width of the cusViews to calculate the topMargin and leftMargin, and the width of the cusView has not given a value yet.

Is the width first calculated in the cusView’s overrided method onSizeChanged()? Is the onSizeChanged() method get called first when the layout is presented on the screen? If so, how do I work around this issue? Do I have to calculate the positionings after onSizeChanged() is done?

Edit: Here is a minimum working example:

Here is my onCreate in RelActivity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    relLayout = new RelativeLayout(this);
    cusViews = new ArrayList<CusView>();
    listParams = new ArrayList<RelativeLayout.LayoutParams>();

    readDBandSetLayout();

    setContentView(relLayout);
}

There is too much information in the readDBandSetLayout() method to present it all here. below are the most important details. If I create the LayoutParams in the following way it works fine, the cusViews are listed downwards and rightwards of eachother:

queryCursor = customApplication.customData.query( number); //Fetches cursor

for ( int i = 0; i < numberOfRows; i++ ){

        if ( i == 0 ){
            LayoutParams p = new LayoutParams(this.getResources().getDimensionPixelSize(R.dimen.small), this.getResources().getDimensionPixelSize(R.dimen.small));
            p.topMargin = 50;
            p.leftMargin = 50;
            listParams.add(p);
        }
        else{
            LayoutParams p = new LayoutParams(this.getResources().getDimensionPixelSize(R.dimen.large),this.getResources().getDimensionPixelSize(R.dimen.large));
            p.addRule(RelativeLayout.BELOW, cusViews.get(i-1).getId());
            p.addRule(RelativeLayout.RIGHT_OF, cusViews.get(i-1).getId());
            listParams.add(p);
        }

    relLayout.addView(cusViews.get(i), listParams.get(i));
}

However, what I want to do in the else statement is something like:

else{
        LayoutParams p = new LayoutParams(this.getResources().getDimensionPixelSize(R.dimen.large),this.getResources().getDimensionPixelSize(R.dimen.large));

//Here I want to calculate cusView2Topmargin and cusView2Leftmargin based on the widths of the first or previosly positioned cusViews. But here the widths are 0 since they haven't been calculated yet.

        p.topMargin = cusView2Topmargin; //Always zero
        p.leftMargin = cusView2Leftmargin; //Always zero
        listParams.add(p);
            }

So the problem lies in that the widths of the cusViews are zero at the point I need them to calculate the layout parameters topMargin and leftMargin.

Unfortunately I cannot use the RelativeLayout’s Rules for what I want to achieve. If there were some way to create rules like RelativeLayout.RIGHT_OF and RelativeLayout.BELOW I could do it like that. Is this possible?

  • 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-29T21:30:11+00:00Added an answer on May 29, 2026 at 9:30 pm

    Its not very clear what your goal is for this layout. It might well be possible to use a simple LinearLayout to get what you want.

    If you want to size these from a database lookup then try simply adding each of the views, using addView() first, storing a reference to each, then go back and sett the margins to place them in the proper positions.

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

Sidebar

Related Questions

I have a little issue concerning an animation-effect which loads a certain div into
Hey guys I have a little issue here. I have a panel where I
I have little issue with detecting internet connection while using static IP on device.
i have a table and i have little issue with groupby month CREATE TABLE
I have a little issue. I have to send NSDate as parameter to soap
I have a little issue with my coding. I am trying to create 2
I have a little architecture issue. On my site user can register and when
I have a strange little issue with a WCF RIA service I'm using in
This little issue is starting to do my head in, I have read around
I have a little issue with upgrading my sqlite database. For now I'm using

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.