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 :)