Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen is the width of an Android View set?
    primarykey
    data
    text
    <p>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.</p> <p>So, over to the problem. In my RelActivity I do this:</p> <ol> <li>Create a RelativeLayout (name it relLayout) </li> <li>Iterate a cursor with cusViews recieved from a database</li> <li>For the first cusView -> Set position by topMargin and leftMargin using a LayoutParameter</li> <li>For the other cusViews -> calculate their topMargin and leftMargin by using one of the other cusViews and a LayoutParameter</li> <li>Set RelActivity's contentView to relLayout</li> </ol> <p>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.</p> <p>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?</p> <p>Edit: Here is a minimum working example:</p> <p>Here is my onCreate in RelActivity:</p> <pre><code>public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); relLayout = new RelativeLayout(this); cusViews = new ArrayList&lt;CusView&gt;(); listParams = new ArrayList&lt;RelativeLayout.LayoutParams&gt;(); readDBandSetLayout(); setContentView(relLayout); } </code></pre> <p>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:</p> <pre><code>queryCursor = customApplication.customData.query( number); //Fetches cursor for ( int i = 0; i &lt; 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)); } </code></pre> <p>However, what I want to do in the else statement is something like:</p> <pre><code>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); } </code></pre> <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.</p> <p>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?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload