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
  • 3,426
  • 9
  • 39
  • 90

3 Answers3

2

Just give like

viewObj.getHeight():

Instead of this

layout.getLayoutParams().height

It will be work

Raja
  • 2,666
  • 2
  • 17
  • 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,076
  • 2
  • 12
  • 22
0

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.

Mobile Team ADR-Flutter
  • 12,062
  • 2
  • 29
  • 49