Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why can we not manually add Android Views to the hierarchy in Android code? Cannot debug existing problem ... #22876

Closed
jonmdev opened this issue Jun 6, 2024 · 2 comments
Labels
platform/android 🤖 t/bug Something isn't working

Comments

@jonmdev
Copy link

jonmdev commented Jun 6, 2024

Description

I am still trying to figure out why Android Labels are sizing, ,clipping and wrapping wrong in Maui as discussed here:

I have now found that even if you manually create a MauiTextView like this, it is sized wrong compared to what is expected of StaticLayout or the equivalent .NET AppCompatTextView or TextView:

            //ADD "TEXT VIEW" MANUALLY IN ANDROID CODE TO ABSOLUTE LAYOUT
            abs.HandlerChanged += delegate {


#if ANDROID

                var absTrueView = abs.ToPlatform(abs.Handler.MauiContext) as LayoutViewGroup;
                
                MauiTextView text = new(absTrueView.Context);

                var scaledDensity = DeviceDisplay.Current.MainDisplayInfo.Density; //= 2.75 on Pixel 5 emulator
                float fontSize = 20;
                string testString = "HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO HELLO";

                text.Text = testString;
                var fontManager = abs.Handler.MauiContext.Services.GetRequiredService<IFontManager>();
                var font = Microsoft.Maui.Font.OfSize("montserratextrabold", fontSize); 
                Android.Text.TextPaint textPaint = new();
                text.SetTypeface(font.ToTypeface(fontManager), 0);  
                text.TextSize = (float)(fontSize);
                text.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
                absTrueView.AddView(text);
                text.Measure(1000, 1000);

                Debug.WriteLine("CUSTOM MAUI TEXT MEASURED " + text.MeasuredHeight / scaledDensity + " w " + text.MeasuredWidth / scaledDensity);

#endif
            };

This returns the identical same wrong measurements as Maui's Label class which is exhibiting the glitching wrapping behavior. So the problem is certainly still there even with that code above for MauiTextView.

I would like to then add directly a plain Android.Widget.TextView or AndroidX.AppCompat.Widget.AppCompatTextView instead to see how they measure.

I expect these would measure correctly (since they measure fine in .NET Android).

However, I find I cannot add any Android class views directly to the hierarchy. There are no errors, but I won't get the size debug output properly and if I open the APK in Android Studio the hierarchy shows it was not added.

For example, if I change the above from

MauiTextView text = new(absTrueView.Context);

to:

AndroidX.AppCompat.Widget.AppCompatTextView text = new(absTrueView.Context);

or:

Android.Widget.TextView text = new(absTrueView.Context);

And try to parameterize them and add them the same, nothing gets added to the hierarchy and I cannot test the behavior.

I note this is similar for all Android objects. For example if I try to add:

Android.Widget.AbsoluteLayout newView = new(absTrueView.Context);

This also fails, while the Microsoft version succeeds:

LayoutViewGroup newView = new(absTrueView.Context);

Is this normal behavior? Why (or how) are we prevented from adding Android views directly to the hierarchy? Is something in Maui removing these after they are added? Like garbage collecting or pruning them?

If so, how can we stop this for test purposes? How can we figure out why the MauiTextView is not sizing the same as StaticLayout or the true Android classes AppCompatTextView or TextView?

Furthermore, what if anything might MauiTextView be doing wrong in the above code to not give the same result as AppCompatTextView or TextView in .NET Android code?

@jonathanpeppers ?

Thanks for any help.

Version with bug

8.0.3 GA

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

Unknown/Other

Affected platforms

Android

@jonmdev jonmdev added the t/bug Something isn't working label Jun 6, 2024
Copy link
Contributor

github-actions bot commented Jun 6, 2024

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

@jonmdev
Copy link
Author

jonmdev commented Jun 6, 2024

Never mind. I am able to add the Android views (even if Maui is pruning them I can debug their size momentarily) but they are still sizing wrong. I still cannot figure out why.

@jonmdev jonmdev closed this as completed Jun 6, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jul 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
platform/android 🤖 t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants