Search code examples
javaandroidviewfragmentscale

Android: view layout resetting when adding/removing fragments


I am a bit new to android, but I know java fairly well.

As a simple means of scalability, i use these simple functions:

public void scaleMyView(float s) {

    scaleFactor = scaleFactor + s;
    // s will usually be like 0.1 or -0.1 = 10% size gain/loss

    View myMainView = findViewById(R.id.MyMainView);
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int x = size.x;
    int y = size.y;


    myMainView.setScaleX(scaleFactor);
    myMainView.setScaleY(scaleFactor);

    myMainView.layout(0, 0, ((int)(x/scaleFactor)), ((int)(y/scaleFactor)))

    myMainView.setPivotX(0);
    myMainView.setPivotY(getActionBar().getHeight());
}

Which does exactly what I want, changing the main view and all within it in size and keeping it in place.

Now comes the problem:

Whenever I add a fragment to a sub-view of that main-view, the "layout" part is reset, and though scale and position remain, size shrinks back to default. (The fragments are very simple, basically simple buttons.)

I tried quite a few things, and I still have no idea where or how to fix that.

Any help is highly appreciated!


Solution

  • Try to use "addOnLayoutChangeListener" method on your mainView:

    myMainView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(
            View view, int i, int i2, int i3, int i4, int i5, int i6, int i7, int i8)
        {
            // set view properties here
        }
    });
    myMainView.requestLayout();