3

I have a LinearLayout(A) which is set to GONE in layout xml. What I want to achieve is that when a button(B) is clicked, the LinearLayout(A) will be set to VISIBLE and then I get its height to update the height of its parent layout. I managed to set the LinearLayout(A) to visible and also get its height, however I fail to update the height of its parent layout directly. The problem from my current code is that, the parent layout will only update itself after another UI Event is triggered (such as spinner drop down menu is opened, soft keyboard shown, etc.). What I really want is that the parent layout update itself directly after button(B) is clicked.

Here is my code.

@Override
public void onClick(View v) {
    if(v==buttonB) {
        ShowLinearLayoutA();
    }
}

private ViewTreeObserver vto;

private void ShowLinearLayoutA() {
    LinearLayoutA.setVisibility(View.VISIBLE);
    if (vto == null) {
        vto = LinearLayoutA.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ParentLayout.getLayoutParams().height=LinearLayoutA.getHeight();
                ParentLayout.invalidate();
            }
        });
    }
}

What did i do wrong ?

Excuse my English and Thank you very much in advance.

EDIT 1 : I tried using runOnUiThread but it doesn't work

My Edited Code

private void ShowLinearLayoutA() {
    LinearLayoutA.setVisibility(View.VISIBLE);
    if (vto == null) {
        vto = LinearLayoutA.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                ParentLayout.getLayoutParams().height=LinearLayoutA.getHeight();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ParentLayout.invalidate();
                    }
                });
            }
        });
    }
}

EDIT 2 :

I gave up trying this method. Instead, I used animation and got it working. Thanks :)

JrL
  • 221
  • 1
  • 7
  • 19

4 Answers4

1

You are performing UI work on wrong thread. Perform UI operations on UiThread like..,.

private void ShowLinearLayoutA() {
    YourActivity.this.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            LinearLayoutA.setVisibility(View.VISIBLE);
        }
    });

if (vto == null) {
    vto = LinearLayoutA.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            YourActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ParentLayout.getLayoutParams().height=LinearLayoutA.getHeight();
                    ParentLayout.invalidate();
                }
            });
        }
    });
}
}
MKB
  • 7,587
  • 9
  • 45
  • 71
  • Thanks for answering. I tried it but it doesn't work. Please see my edit :) – JrL Dec 02 '12 at 13:51
  • add YourActivity.this with runOnUI. I have edited my post. Sorry for my old post that have a topo – MKB Dec 02 '12 at 13:56
  • Thanks. I tried to add MyActivity.this before the runOnUiThread, but it still doesn't work. I even tried to invalidate all the layout inside runOnUiThread. – JrL Dec 02 '12 at 14:01
  • Ok last try... put LinearLayoutA.setVisibility(View.VISIBLE); in runOnUiThread as well – MKB Dec 02 '12 at 14:07
1

I know this is really old question but i guess it has no answer. I struggled the same problem and the key point is you are trying to update the height before it has changed on runtime, simple delayed task might be a solution.

_layout.postDelayed(new Runnable() {
        @Override
        public void run() {
            _layout.setBackgroundDrawable(backgroundImage);
        }
    }, 100); // delayed for 0.1 sec
mendorphis
  • 63
  • 6
0

call

LinearLayoutA.invalidate()
Kiran Kumar
  • 1,192
  • 8
  • 10
0

I gave up trying this method. Instead, I used animation and got it working. Thanks :)

JrL
  • 221
  • 1
  • 7
  • 19
  • What animation you used i am also having same problem i want to update the width and height please tell me? – Ravi Jan 23 '13 at 05:22
  • please tell me how to use that animation i am seeting webview parameters but height parameters are not being updated – Ravi Jan 23 '13 at 09:03
  • I don't understand your problem but I make a class extending Animation and change the height/width of the view from that class. Maybe you should make a new question, so that you can make yourself clear and get better answers from others. – JrL Jan 23 '13 at 09:46
  • Thanks What my problem is- i am having a webview and in webview i am loading the page .There i am having two button for zoomin and zoomout, every time on zoomin and zoomout i am changing the webview parameters and after setting parameters i called the invalidate also but still the height i not being set.here width is ok and if any click event happens so height also comes correct so i think you only can help me to sort out this issue .Thanks – Ravi Jan 23 '13 at 12:11
  • i posted question also-http://stackoverflow.com/questions/14493873/webview-height-is-not-being-update – Ravi Jan 24 '13 at 06:37