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.
onCreateViewis called before your layouts have been inflated, so they have noheightorwidth. If you want to fiddle with how layouts andviewsare drawn, you can create your own view, extendingView, and implementonLayout(); If you Google, there’s plenty of examples of that.