0

I have a Layout containing a fragment. I set width of Layout to let's say 300dip.

I want to calculate the width of the children programmatically in relation to the 300dip of the parent.

I can't do it in XML since it has some "tricks" and using weigths or RelativeLayout will not work and I have to use maths.

Here is the fragment with the containing layout...

<LinearLayout 
    android:layout_width="300dip"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <fragment
        android:id="@+id/tabbar"
        android:name="com.test.MyFragment"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

In MyFragment there's the method:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.d("test", "container: "  + container);
    View view = inflater.inflate(R.layout.mylayout, container, false);
    Log.d("test", "view width: "  + view.getWidth());

    //do things with view

    return view;
}

I get the outputs container: null and view width: 0 and I can't find any onMeasure method in the fragment or similar.

User
  • 31,811
  • 40
  • 131
  • 232

3 Answers3

1

onCreateView is called before your layouts have been inflated, so they have no height or width. If you want to fiddle with how layouts and views are drawn, you can create your own view, extending View, and implement onLayout(); If you Google, there's plenty of examples of that.

Sam
  • 1,124
  • 1
  • 8
  • 12
Christine
  • 5,617
  • 4
  • 38
  • 61
  • I thought about creating a custom view but I don't know how to do it if it's a fragment... I'm following this example about creating a tab view using fragments, the tabbar is also implemented as a fragment http://neilgoodman.net/2012/03/12/working-with-fragments-on-android-part-2 that's what I want to "customize" – User Jun 13 '12 at 08:32
  • You can create a view, like CustomButton, which extends Button or View or TextView, then you can implement onLayout to layout your button exactly as you want it. But again, I think the standard android layout features should be able to provide you with the right layout, in a much simpler way. – Christine Jun 13 '12 at 20:59
  • Yes I know that, but the question was how to do it with a *fragment*. Because the code I'm using uses a fragment and I want to customize that. I don't know if it's generally wrong and I have to use a view instead? Does something like "custom fragment" exist or am I completly wrong? – User Jun 18 '12 at 21:29
  • The fragment uses a layout file. If you want to customize the Fragment view, you can customize the layout that the fragment inflates. If I understand your question correctly.... – Christine Jun 19 '12 at 14:52
  • Yes but I also need programmatic customization for the fragment view, so change the XML will not be enough. – User Jun 19 '12 at 15:12
  • lxx, there's a layout file containing views for displaying the fragment, and theres a view in which the fragment is called. What other fragment views are there? – Christine Jun 19 '12 at 16:21
  • Ok, I decided to implement custom layout and even left idea of making as a fragment because I don't need it at all. But I run into a (different) issue, maybe you're interested :) http://stackoverflow.com/questions/11187394/cant-deselect-buttons-using-a-custom-layout – User Jun 25 '12 at 14:41
0

I think you can find the dimensions of your parent view (LinearLayout) programmatically in the onActivityCreated method:

@Override 
public void onActivityCreated(Bundle savedInstanceState){
     super.onActivityCreated(savedInstanceState);

     myActivityView =  (LinearLayout)  
             getActivity().findViewById(((View) getView().getParent()).getId());

     myFragment = (RelativeLayout) getView().findViewById(R.layout.mylayout);
//Change RelativeLayout to whatever your fragment is using
}

So then myActivityView should be your parent view.

EDIT

You could then use a ViewTreeObserver in onActivityCreated to get the dimensions of your parent view. myFragment should be your fragment, so you could place it relative to the dimensions returned from myActivityView. Good luck.

thomas.cloud
  • 881
  • 13
  • 30
  • The views still haven't been laid out during onActivityCreated. I just tried this and the width and height of all of my views are still 0 at this point. – Kenny Wyland Mar 18 '13 at 05:21
  • Are you setting your parent view width in your xml file like the OP did? I'm pretty sure I was using this at some point and it worked. If you post some of your xml and some of your java file maybe it will be easier to check out. – thomas.cloud Mar 22 '13 at 03:55
  • Yes, the parent is set to match_parent/match_parent. – Kenny Wyland Mar 22 '13 at 05:13
  • Ah yep, sorry about that, I was getting the dimensions of the parent from a ViewTreeObserver: http://developer.android.com/reference/android/view/ViewTreeObserver.html. Here is an example of how to use it: http://stackoverflow.com/questions/7733813/how-can-you-tell-when-a-layout-has-been-drawn. I guess I was more concerned with how to gain access to the parent layout in this question than the method to return the actual dimensions. Thanks for pointing this out. You can use this VTO within your onActivityCreated method. – thomas.cloud Mar 25 '13 at 13:36
  • +1 for triggering something in my head. Getting the dimensions of a views is tricky. – M. Reza Nasirloo Jun 27 '14 at 21:17
0

Use Handler will get height and width. but this is not a proper way.

new Handler().postDelayed(new Runnable() {
      @Override
      public void run() {
         Log.d("test", "view width: "  + view.getWidth());
      }
    },1000);
Muhammed Haris
  • 340
  • 1
  • 5
  • 15