0

I have multiple FrameLayout inside a ScrollView. In one of the FrameLayout I'm loading a PreferenceFragment. Now I need to set the height of the FrameLayout to the height of the PreferenceFragment to avoid nested scrolling (the PrefrenceFragment is scrollable too).

How can I get the height of the PreferenceFragment?

I tried to override onCreateView() in the PreferenceFragment class, and call View.getHeight(), but its just returning 0.

Setting the FrameLayout to wrap_content, isn't working as well, its just taking the space of a single preference then...

edit: I tried now to get the height by using addOnGlobalLayoutListener() but the returned value is just the displayed height, not the full height of the PreferenceFragment. So I'm just getting the height of one preference for wrap_content or e.g. 300 if I set the height of the FrameLayout to 300 in the xml. I need the full height of all preferences (visibile and not).

@Override
public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle)
{
  final View v = super.onCreateView(paramLayoutInflater, paramViewGroup, paramBundle);
  if (v != null) {
    ViewTreeObserver vto = v.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
      Log.d("TEST", "Height = " + v.getHeight() + " Width = " + v.getWidth());
      ViewTreeObserver obs = v.getViewTreeObserver();
      obs.removeGlobalOnLayoutListener(this);
    }
    });
  }
  return v;
}
agrajag
  • 451
  • 1
  • 4
  • 13
  • Check this stackoverflow.com/questions/12352920/measure-view-in-fragment. You need to wait till the view is layed and then measure. – Raghunandan Jan 18 '16 at 18:07
  • not sure but look at this one ....http://stackoverflow.com/questions/16925317/getwidth-and-getheight-always-returning-0-custom-view and http://stackoverflow.com/questions/8170915/getheight-returns-0-for-all-android-ui-objects – Nik Jan 18 '16 at 18:25
  • @Raghunandan and Nik: thanks for your links, unfortunately its still not working for me, see the edit – agrajag Jan 18 '16 at 18:42

2 Answers2

0

You should wait for WindowFocusChange in your activity:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    if(hasFocus)
        //LAYOUT FRAGMENT NOW
}
Droid Chris
  • 3,455
  • 28
  • 31
0

try this

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            height = parentLayout.getheight();// the root layout of the fragment
            //TODO: 
        }
    }, 200);
Avinash Joshi
  • 1,307
  • 8
  • 25