1

I need an advice...I want to create custom view group that will have different layout depending on size of this view.

An example:

Two views:

  • View A: child view that is at least 50dp height
  • View B: my custom view that extends vertically oriented LinearLayout and is 200 dp height

What I want:

  1. When I insert 2 Views A into View B:
    • I want to stretch these two Views A to 100 dp. So B will be common linear layout with 2 children.
  2. When I insert 5 Views A into View B:
    • I want to add scroll view into B. All 5 Views A will be inside this scroll view and they will be 50 dp height.

Usually, I add views to my custom view in constructor. But I can't do it here because I don't know height of B in constructor. I know only height of A. So please advice me which callback method should I use...when height of B will be known so I can add all child views according this height.

Or if you know any other approach...please let me know...

Thank you very much!

Bhiefer
  • 1,613
  • 4
  • 16
  • 31
  • 1
    If the height of B should be const, then just check the common height of child A's. If aggregated height will be more, then 200 (or 4+ views at all) - create a ScrollView inside B and push your A views into it, then set ScrollView's layout params to match_parent programmatically. Otherwise (if A views just 1-3) - set them layout_weight param to 1. – Archinamon Jun 08 '13 at 10:27
  • That is exactly what I want. But I don't know when the value 200 will be known, i.e. which method to override. height of B isn't const height and is screen size dependent. As I wrote: this value isn't known in constructor... Thanks for your reply... – Bhiefer Jun 10 '13 at 13:17

2 Answers2

2

you should consider to put all views into a vertical linear layout which is in a scrollview , or better yet: use a single listView instead of layouts and scrollViews .

The reason is that it will handle the scrolling automatically if needed, depending on the available space on the screen and the size of your views.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Unfortunately, this is not the desired behaviour. I want to stretch the child views when scrollview is not needed...i.e. I will use weights when aggregated size of inner views is smaller and scrollview when size is bigger. And I don't know how it is possible with either listview or simple scrollview. And btw. the layout will be a little bit more complicated than my example. It will be a table layout with multiple columns.. That is why I don't want to use listview. – Bhiefer Jun 10 '13 at 13:30
  • but android supports many screens resolutions and densities. you should think how to handle different configurations so that you would use the needed layout. even when using weights, you need to check if the views got enough space for their content to be shown. how would you do it? – android developer Jun 10 '13 at 14:14
  • sure I know that...that is the reason why I want to use this custom layout. When screens/resolution are big enough or when you just change device dpi to lower value, the content is small and it doesn't look good when it isn't fullscreen (it occupies just part of screen)...so I want to stretch it using weights. on the other hand, when screen is small, I have to use the scrollview. And how I will distinguish between these two situations? That is what I am asking here:) I want to get height by getHeight() method and according this value, choose the layout. But I don't know in which View method. – Bhiefer Jun 10 '13 at 15:44
  • 1
    ok, i understand. for getting the height of each of the views, you can use this method: http://stackoverflow.com/a/10923514/878126 – android developer Jun 10 '13 at 16:52
0

You can use this code the get the actual height of main layout. Then you can use that height in a if-else or switch-case block to check the needed conditions.

public int getLayoutSize() {
// Get the layout id
    final LinearLayout root = (LinearLayout) findViewById(R.id.mainroot);
    final AtomicInteger layoutHeight = new AtomicInteger();

    root.post(new Runnable() { 
    public void run() { 
        Rect rect = new Rect(); 
        Window win = getWindow();  // Get the Window
                win.getDecorView().getWindowVisibleDisplayFrame(rect);

                // Get the height of Status Bar
                int statusBarHeight = rect.top;

                // Get the height occupied by the decoration contents 
                int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();

                // Calculate titleBarHeight by deducting statusBarHeight from contentViewTop  
                int titleBarHeight = contentViewTop - statusBarHeight; 
                Log.i("MY", "titleHeight = " + titleBarHeight + " statusHeight = " + statusBarHeight + " contentViewTop = " + contentViewTop); 

                // By now we got the height of titleBar & statusBar
                // Now lets get the screen size
                DisplayMetrics metrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(metrics);   
                int screenHeight = metrics.heightPixels;
                int screenWidth = metrics.widthPixels;
                Log.i("MY", "Actual Screen Height = " + screenHeight + " Width = " + screenWidth);   

                // Now calculate the height that our layout can be set
                // If you know that your application doesn't have statusBar added, then don't add here also. Same applies to application bar also 
                layoutHeight.set(screenHeight - (titleBarHeight + statusBarHeight));
                Log.i("MY", "Layout Height = " + layoutHeight);   

            // Lastly, set the height of the layout       
            FrameLayout.LayoutParams rootParams = (FrameLayout.LayoutParams)root.getLayoutParams();
            rootParams.height = layoutHeight.get();
            root.setLayoutParams(rootParams);
        }
    });

return layoutHeight.get();
}
smartmouse
  • 13,912
  • 34
  • 100
  • 166