77454

How get height of the a view with gone visibility and height defined as wrap_content in xml?

I have a ListView with visibility=gone and height=wrap_content, and i need its height to can make an animation of expand.

I already tried it:

view.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); int height = view.getMeasuredHeight();

and

v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); int height = view.getMeasuredHeight();

and

v.measure(View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.MATCH_PARENT, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.UNSPECIFIED)); int height = view.getMeasuredHeight();

But it returned me a lesser value.

I am trying to do this after onResume.

Answer1:

The only way that i found was setting the width of the my view from width of a visible header view, then the below code returned me the right value.

int widthSpec = MeasureSpec.makeMeasureSpec(headerView.getWidth(), MeasureSpec.EXACTLY); int heightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); v.measure(widthSpec, heightSpec); int height = v.getMeasuredHeight();

Answer2:

When an object is gone, it no longer is part of the layout. Maybe you mean to set your object to invisible, in which case you'll probably get a sensical value

Answer3:

view.post(new Runnable() { @Override public void run() { view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); int height = view.getMeasuredHeight(); } });

This worked for me. The method "post" makes sure the view is already added to the screen.

Recommend

  • Force re-layout on a viewgroup including all its children
  • Code inserted triplicate records a few times and now works periodically
  • ImageMagick: how to minimally crop an image to a certain aspect ratio?
  • Typescript error “class is not a constructor”
  • GC Lagging app when scrolling my custom component
  • Regex of a string [duplicate]
  • A slot can take less arguments than provided by the signal, HOW? - Qt
  • detect if views are overlapping
  • How to add a tab in PySide
  • get height of layout that is set to wrap content
  • if i take screenshot, the screen shot image show in my activity.how to hide it? [closed]
  • Auto-Scroll the Webview
  • Current activities onResume() is not called when another Activity is launched from lock screen via n
  • getMeasuredHeight() returns 16777215 on Android 4.2.2
  • Android sensor listener does not get unregistered or sensor still retrieves values
  • the five top rows of the oval are erased. why?
  • How to get rows of a dataframe that contain values between two other values?
  • Facebook API: cannot login on Android
  • Consuming a web service with the Netbeans Platform
  • How to protect an asp:textbox from user input?
  • Fat binaries in iOS
  • CloseOptionsMenu doesn't work?
  • MultiLevel ExpandableListView not highlighting properly
  • Efficient algorithm to find additions and removals from 2 collections
  • How do I get the list of bad records that didn't load in Bigquery?
  • Ember.js model to be organised as a tree structure
  • Thread safety of a fluent like class using clone() and non final fields
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Deleting and Updating values from a cusrsor adapter
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Cannot Parse HTML Data Using Android / JSOUP
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Getting Messege Twice Using IMvxMessenger
  • Linking SubReports Without LinkChild/LinkMaster
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal
  • How to load view controller without button in storyboard?