12

I want to determine the available height and width of the parent layout to which I have to add my view.

I have used many methods on layout like

  • layout.getHeight()
  • layout.getRootView().getHeight()

All these methods return 0(zero) as the result.

My main requirement is to be able to give width to a view that is some % of the width of the layout .

Also I dont want to use tag for this. I want to do it through code.

Thanks in advance.

ashimashi
  • 463
  • 1
  • 5
  • 14
Robin
  • 6,879
  • 7
  • 37
  • 35

6 Answers6

7

Ok. I found a solution to it. The way we can get hieght of the screen in activity is

getApplicationContext().getResources().getDisplayMetrics().heightPixels

But I still dont know how to get the pixel dimentions of parent layout?

Robin
  • 6,879
  • 7
  • 37
  • 35
  • This is not always the same. With getApplicationContext().getResources().getDisplayMetrics().heightPixels you are getting the screen height in pixels. If you have (for example) an action bar this includes the action bar as well, something that you probably want to avoid if you want to know the height in pixels of a layout or ui element (background or whatever) – Sotti Jun 05 '14 at 11:25
5

The issue here is you are trying to get the parent height/weight before the layoutmanager completes parent layout. You can only get parent dimensions, after its layout is complete.

The recommended way to do what you want to achieve is by using layout_weight field of LayoutParams. link text

In particular you should use:

LayoutParams lp = new LinearLayout.LayoutParams(width, height,
weight);

layout_weight value can be anything between 0.0 to 1.0. So for example if I have myButton within parentLayout, and I want myButton to occupy 30% of parent layout...I would use something like:

 myButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                                               LayoutParams.WRAP_CONTENT,0.3));

The layout_weight sum of all child elements should be 1. Also, layout_weight should not be used with view's width or height set to LayoutParams.FILL_PARENT.

  • But how do I get the the height and width of the parent layout. ........... LayoutParams lp = new LinearLayout.LayoutParams(width, height, weight); ......... To give height in % this method is only useful If I have parent layouts height and width – Robin Sep 07 '10 at 08:45
  • No, you dont need to know the exact width/height of parent for using this method. You can specify anything between 0 to 1 as a weight. So for example if I have myButton within parentLayout, and I want myButton to occupy 30% of parent layout...I would use something like: – Megha Joshi - GoogleTV DevRel Sep 07 '10 at 09:22
  • sorry comment space didnt suffice...I edited my answer above. – Megha Joshi - GoogleTV DevRel Sep 07 '10 at 09:29
  • Thanks for the reply Megha. My requirement is that I want to show an Image view in a linearlayout which has width same as that of linear layout and height should be variable. – Robin Sep 07 '10 at 10:09
  • Image view has this method ............setLayoutParams(ViewGroup.LayoutParams params) ........ This takes an argument of type ViewGroup.LayoutParams and this class has no such constructor which takes layout_width – Robin Sep 07 '10 at 10:11
  • Actually the sizes of my images are small.. but I want my images to stretch and use the entire width of screen – Robin Sep 07 '10 at 10:31
4

Try something like this :

view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    public void onGlobalLayout() {
        view.getHeight();
    }
  });
lokoko
  • 5,785
  • 5
  • 35
  • 68
2

One alternative to programmatically implement this is to use following method:

public void onWindowFocusChanged(boolean b) { ...}

By the time this method is invoked, the layouting-process has been finished. Thus you can obtain the parent attributes correctly.

As Example:

public class A extends Activity {
    LinearLayout parent;
    Button child;
    public void onCreate(Bundle b) {
        parent = (LinearLayout) findViewById(R.id.linear1);
        child = (Button) findViewById(R.id.button1);
        .
        .
    }

@Override
public void onWindowFocusChanged(boolean b) {
        LayoutParams lpc = child.getLayoutParams();
        lpc.width = parent.getWidth();
        child.postInvalidate();
        .
        .
    }
}
Robeth
  • 21
  • 2
0

In OnCreate() method ,if you use layout.getHeight() and layout.getWidth(), it will returns 0, instead you can follow this:

public void onClick(View v) {   
    // TODO Auto-generated method stub

             int layWidth = Layout.getLeft();
             int layHeight = Layout.getHeight();
         break;     
    }
RThomas
  • 10,702
  • 2
  • 48
  • 61
subaja
  • 29
  • 6
  • It returns `0` because the UI has not the time to be displayed. It might be better to use a `Runnable` inside `onCreate` or use [`onWindowFocusChanged`](http://developer.android.com/reference/android/view/View.html#onWindowFocusChanged%28boolean%29) method inside the Activity as [Robeth](http://stackoverflow.com/a/10269753/2668136) mentioned, instead of using a click listener to change the views. – Blo May 07 '14 at 07:20
0

You are probobly calling getHeight() to early. Ensure that getParent().getHeight() is being called after viewGroup.addView(view0) and/or setContentView(activity_main.xml). This has happened to me in the past and this fixed it.