tags:

views:

38

answers:

2

Hi.

I have a layout that contains several sub-views all which set height like:

<View android:layout_width="fill_parent"
    android:layout_height="18dip"
    android:background="#ff000000" />

How can I retrieve the value stored in layout_height in code?

Ultimately, what I would like to do is being able to adjust the size defined in the layout so I can dynamically change the value defined by layout_height depending on the resolution of the screen resolution and the pixel density. Is the only way to do so during the onLayout callback?

It would be much easier to redefined the XML on onCreate.

Comments, advices would be much appreciated.

Thanks JY

+1  A: 

Acces the View's Layout and then access the 'height' field: View.getLayoutParams().height

Peter Knego
A: 

Should have searched a tad further prior to post a question. This does the trick it seems...

DisplayMetrics dm = new DisplayMetrics();
    this.getWindowManager().getDefaultDisplay().getMetrics(dm);
    factor_y = (dm.ydpi * dm.heightPixels) / (160 * 480);
    factor_x = (dm.xdpi * dm.widthPixels) / (160 * 320);

LinearLayout layout = (LinearLayout) findViewById(R.id.mainlayout);
for (int i = 0; i < layout.getChildCount(); i++)
{
    View v = layout.getChildAt(i);
    int height = v.getLayoutParams().height;
    if (height > 0)
    {
        v.getLayoutParams().height = (int) ((float) height * factor_y);
    }
    int width = v.getLayoutParams().width;
    if (width > 0)
    {
        v.getLayoutParams().width = (int) ((float) width * factor_x);
    }
}
jyavenard