1

I have following classes

TextLayout class, in which I have created a single textview and added it in onMeasure override method.

public class TextLayout : FrameLayout
{
    private TextView headerLabel;
    public TextLayout(Context context) : base(context)
    {

    }
    private void UpdateText()
    {
        if(headerLabel == null)
        this.headerLabel = new TextView(this.Context);
        headerLabel.LayoutParameters = (new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent));
        this.headerLabel.Text = "General Meeting";
        headerLabel.SetTextColor(Color.Green);
        headerLabel.Typeface = Typeface.DefaultBold;
        headerLabel.TextSize = 25;
    }

    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
        UpdateText();
        int minimumWidth = this.PaddingLeft + PaddingRight + SuggestedMinimumWidth;
        int widthOfLayout = ResolveSizeAndState(minimumWidth, widthMeasureSpec, 1);
        SetMeasuredDimension(widthOfLayout, 75);
        if (this.headerLabel.Parent == null)
        {
            this.AddView(this.headerLabel);
        }
    }
}

I have another class as an intermediate class, in which I have added the TextLayout class in OnMeasure method of this intermediate class as below,

public class IntermediateLayout : FrameLayout
{
    TextLayout textLayout;
    public IntermediateLayout(Context context) : base(context)
    {

    }

    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
        if (textLayout == null)
            textLayout = new TextLayout(this.Context);
        this.AddView(textLayout);
    }
}

Finally, in the main class I have added the intermediate class in the OnCreate of main class.

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        IntermediateLayout mainLayout = new IntermediateLayout(this);

        // Set our view from the "main" layout resource
        SetContentView(mainLayout);
    }

In this scenario, the textview is not rendered the view in Xamarin.Android platform.

Also in another scenario,when layout parameters for TextLayout class is set in the OnMeasure method of Intermediate class(where TextClass is defined), text is not rendered in the view.But when the layout parameters is set in the OnMeasure method of same class(TextLayout) text is rendered in the view.

Which is the correct way to set layout parameters of a class?

Is there any way to fix this problem?

Swathi Gopal
  • 103
  • 1
  • 7

1 Answers1

1

Which is the correct way to set layout parameters of a class?

onMeasure aims at determining the measured width and measured height of your view. It will be called for several times on initialization or when the size of your view has changed. So it's not a good place to place some initialization codes in onMeasure.

In your codes, this.AddView(textLayout); in IntermediateLayout will lead to an exception, when onMeasure is called for several times.

So the good place to put initialization codes of a view is in the constructor of the class:

TextLayout.cs:

public class TextLayout:FrameLayout
{
    private TextView headerLabel;
    public TextLayout(Context context) : base(context)
    {
        //put the initialization codes in contructor
            UpdateText();
            if (this.headerLabel.Parent == null)
            {
                this.AddView(this.headerLabel);
            }
    }


    private void UpdateText()
    {
        if (headerLabel == null)
            this.headerLabel = new TextView(this.Context);
        headerLabel.LayoutParameters = (new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.MatchParent));
        this.headerLabel.Text = "General Meeting";
        headerLabel.SetTextColor(Color.Green);
        headerLabel.Typeface = Typeface.DefaultBold;
        headerLabel.TextSize = 25;
    }


    protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        base.OnMeasure(widthMeasureSpec, heightMeasureSpec);
        int minimumWidth = this.PaddingLeft + PaddingRight + SuggestedMinimumWidth;
        int widthOfLayout = ResolveSizeAndState(minimumWidth, widthMeasureSpec, 1);
        SetMeasuredDimension(widthOfLayout, 75);
    }
}

IntermediateLayout.cs:

public class IntermediateLayout : FrameLayout
{
    TextLayout textLayout;
    public IntermediateLayout(Context context) : base(context)
    {
        if (textLayout == null)
            textLayout = new TextLayout(this.Context);
        this.AddView(textLayout);
    }
    //there is no need to override onMeasured here
}
Elvis Xia - MSFT
  • 10,801
  • 1
  • 13
  • 24