Forum Xamarin.Android

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Gridlayout has 0 width

I have a gridlayout in my LinearLayout. I just want to inflate some Textviews with equal width size in my GridLayout. The problem is that I get a value of 0 for my GridLayout. I need the the width to decide how big the textview can be to display them on the GridLayout. This is the code I use:

Layout:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <WidthGridLayout.CustGridl android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/gl" /> </LinearLayout>

code of Activity:

protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView (Resource.Layout.Main); var gl = FindViewById<GridLayout>(Resource.Id.gl); int w = gl.Width; // is still 0 }

The class of my GridLayout:

`public class CustGridl : GridLayout
{

    public int ratioWidth = 0;
    public int ratioHeight = 0;

    public CustGridl(Context context, IAttributeSet attrs) :
        base(context, attrs)
    {
        Initialize();
    }

    public CustGridl(Context context, IAttributeSet attrs, int defStyle) :
        base(context, attrs, defStyle)
    {
        Initialize();
    }

    private void Initialize()
    {
    }


    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.GetSize(widthMeasureSpec);
        int height = MeasureSpec.GetSize(heightMeasureSpec);
        if (0 == ratioWidth || 0 == ratioHeight)
            SetMeasuredDimension(width, height);
        else
        {
            if (width < height * ratioWidth / ratioHeight)
            {
                SetMeasuredDimension(width, width * ratioHeight / ratioWidth);
            }
            else
            {
                SetMeasuredDimension(height * ratioWidth / ratioHeight, height);
            }
        }
    }
}`

The OnMeasure method will be called after the code of the Activity had run. I need the values of the OnMeasure method back before my code asks for the width of the GridLayout (int w = gl.Width;)

Sign In or Register to comment.