Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set size and layout in onSizeChanged?

I can't get my elements, for example ListView, to resize properly.

I got a ListActivity for searching, with a dashboard, an editbox and a listview. The idea is to hide the actionbar while the keyboard is showing.

I extended LinearLayout, to listen to view size changes (as recommended here) :

public class SizeChangingLinearLayout extends LinearLayout {
    //...
    @Override
    protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld)
    {        
        View actionbar = mainView.findViewById(R.id.actionbar);

        if (yNew > yOld) 
            actionbar.setVisibility(View.VISIBLE);
        else if (yNew < yOld) 
            actionbar.setVisibility(View.GONE);

        super.onSizeChanged(xNew, yNew, xOld, yOld);

    }
}

The hiding/showing of the actionbar works as expected but there is a gap below the ListView of the same height as the hidden actionbar. As soon as any graphics change (e.g. writing in the editbox) the ListView fills the gap. A similar behavior appears in reverse, when hiding the keyboard.

enter image description here

[Edit]

The same problem also appears in changing other layout. Changing Visibility of things in onSizeChanged shows directly, but changing sizes and margins does not show until some other user action redraws those views. Invalidation does not work from onSizeChanged.

[/Edit]

I have tried to refresh the graphics by:

  • invalidate-ing both the custom LinearLayout and the ListView (as recommended here)
  • notifyDataSetChanged on the list's adapter
  • forceLayout

... and more, without success. Why is the ListView not resizing at first? Do I have to override onLayout in my extended LinearLayout? What have I missed?

like image 331
JOG Avatar asked Jun 07 '11 08:06

JOG


People also ask

What is the difference between sizechanged and layoutupdated events?

If the position of the object within a parent container changes, but not the size, SizeChanged won't occur. LayoutUpdated is a similar event, but LayoutUpdated is also fired for position changes.

How does the adjust layout feature work?

The Adjust Layout feature can do much of that work automatically by adjusting the page elements in your document layout when the page size, page margin, or bleed of the document changes. To adjust the layout for a document:

What does size changed mean in Android app?

For example, this might happen if the user has resized the app window and the overall app view is now a narrow view. SizeChanged occurs during initial layout of elements on a page, when the app first is activated, because the ActualHeight and ActualWidth values for UI elements are undefined before layout happens.

Why does my layout space change when a list changes?

Other changes that happen at run-time that change layout space even if they're not directly changing FrameworkElement layout properties. For example, a list that's based on databinding to items might refresh or update, and that could cause size changes in items, items controls, list views, and so on.


1 Answers

Adding the following at the end of onSizeChanged() will solve it:

Handler handler = new Handler();
handler.post(new Runnable() {

    @Override
    public void run() {
        requestLayout();
    }
});

See the answer to this question: Views inside a custom ViewGroup not rendering after a size change

like image 97
aspartame Avatar answered Sep 28 '22 06:09

aspartame