2

I have added 3 custom views (SmallTile.java) to another custom view (MyView.java; it is based on the deprecated AbsoluteLayout - but I plan to change the base class later).

I am trying to position the 3 children by the following code in the parent:

@Override
protected void onSizeChanged (int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);

    Log.d(APP, "w=" + w + "; h=" + h + ", oldw=" + oldw + ", oldh=" + oldh);

    int count = getChildCount();

    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        if (child.getVisibility() == GONE)
            return;

        int x = mRandom.nextInt(w - child.getWidth());
        int y = mRandom.nextInt(h - child.getHeight());
        AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, 
            LayoutParams.WRAP_CONTENT, 
            x, 
            y
        );

        Log.d(APP, i + ": x=" + x + "; y=" + y);
    }

    requestLayout();
}

However eventhough I can see feasible positions in the debugger -

console

In the emulator all children are positioned wrong - at the origin:

emulator

What is going wrong here? I thought calling requestLayout() would call AbsoluteLayout.onLayout() and thus redraw the children at the new random positions?

Here is a random picture from internet for a View lifecycle (I realize, that it isn't 100% true):

View lifecycle

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
  • 1
    hmm, and where are you setting the position for children? you just create the `params` and do nothing with it ... also you do not use setX/Y of child view ... – Selvin Oct 24 '14 at 13:28
  • 1
    http://selvin.pl/testabsworking.png (this solution needs API >= 11) ... but if you wana ask me if using View for each letter on board is good i would have to answer: no ... go for custom drawed view(or even some 2d graphics library with sprites) – Selvin Oct 24 '14 at 13:44
  • +1 thanks, Selvin - I have removed `requestLayout()` and added `child.setLayoutParams(params)` – Alexander Farber Oct 24 '14 at 14:34

0 Answers0