1

I need to measure the width of an ImageView, but when i measure it after setting android:adjustViewBounds on the ImageView the measuredWidth is incorrect... What am i doing wrong and how should i fix this?

My layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/lines2"
android:orientation="vertical"
android:weightSum="917">
<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="143">
    <RelativeLayout
        android:id="@+id/rectangleLayout"
        android:background="@drawable/customborder"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <ImageView
        android:id="@+id/circle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/circle" 
        android:src="@drawable/circle" 
        />
</RelativeLayout>

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        view = inflater.Inflate (Resource.Layout.pager1Fragment, container, false);

        rectangleLayout = view.FindViewById<ViewGroup> (Resource.Id.rectangleLayout);
        circle = view.FindViewById<ImageView> (Resource.Id.circle);
        circle.Measure ( Android.Views.View.MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified),
            Android.Views.View.MeasureSpec.MakeMeasureSpec(0, MeasureSpecMode.Unspecified));

        Initialize ();

        return view;
    }


    public void Initialize ()
    {
        var rectanglePadding = (int)((double)circle.MeasuredWidth / 2d);
        //measuredWidth is wrong when adjustviewbounds is set

        Console.WriteLine ("padding " + rectanglePadding);

        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams (RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.MatchParent);
        p.LeftMargin = rectanglePadding;

        rectangleLayout.LayoutParameters = p;

    }
Joske369
  • 505
  • 1
  • 6
  • 18

1 Answers1

1

Ok getting View asap when my fragment is created.

    ImageView Example = (ImageView) parentview.findViewById(R.id.Example);

    Example.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @SuppressWarnings("deprecation")
                @Override
                public void onGlobalLayout() {
                    Example.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    int width = Example.getWidth() <--- this gives you width.
                }
            });

Just convert your code to that and your set.

Xjasz
  • 1,238
  • 1
  • 9
  • 21
  • No, there's a green bar that has to stop on half of the width of the circle imageview like this: O=== that green bar is called "rectanglelayout" in my code – Joske369 Mar 16 '15 at 18:41
  • i'll give it a try one sec – Joske369 Mar 16 '15 at 19:00
  • It works, thank you sooo much , i still wonder why onmeasure returned wrong values with adjustviewbounds, i guess it's a bug – Joske369 Mar 16 '15 at 19:05