1

I got a Layout and it's height is set to wrap_content. Now I need to get the real hight dynamically at run time.

How can I do this?

I tried this:

layout.getLayoutParams().height

But this will give me "-2" which is Wrap_Content.

Can anyone help me?

progNewbie
  • 4,362
  • 9
  • 48
  • 107

3 Answers3

2

Just give like

viewObj.getHeight():

Instead of this

layout.getLayoutParams().height

It will be work

Raja
  • 2,775
  • 2
  • 19
  • 31
0

You are trying to get the height of layout, that weren't drawn yet.

Use the code snippet below to get the height of the layout

layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                layout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                height = layout.getHeight(); // here get the height of the view
            }
        });
Arpan Sarkar
  • 2,301
  • 2
  • 13
  • 24
-1

when you getting in view or layout height and width value used below code..

 textView = findViewById(R.id.textCurve); // first object define it not null then getting value.

        Log.d("Height Value:::",""+textView.getHeight());// view control or layout object.

wrap_content and match parent value given 0.