How to expand (set match_parent) my material Entry in android handler?

Igor Kravchenko 281 Reputation points
2022-04-27T10:11:15.457+00:00

I have a custom Entry extended by handler to be material.

public class MaterialEntry : Entry  
    {  
    }  

public interface IMaterialEntryHandler : IViewHandler  
    {  
        new TextInputLayout PlatformView { get; }  
    }  
  
public partial class MaterialEntryHandler : IMaterialEntryHandler  
    {  
        public static IPropertyMapper<IEntry, IMaterialEntryHandler> Mapper = new PropertyMapper<IEntry, IMaterialEntryHandler>(ViewMapper)  
        {  
            [nameof(IEntry.Placeholder)] = MapPlaceholder,  
        };  
  
        public MaterialEntryHandler(IPropertyMapper mapper, CommandMapper commandMapper = null) : base(mapper, commandMapper)  
        {  
        }  
  
        public MaterialEntryHandler() : base(Mapper)  
        {  
        }  
    }  
  
public partial class MaterialEntryHandler : ViewHandler<IEntry, TextInputLayout>, IMaterialEntryHandler  
{  
    protected override TextInputLayout CreatePlatformView()  
    {  
        TextInputLayout layout = new TextInputLayout(Context);  
        TextInputEditText inputEditText = new TextInputEditText(layout.Context);  
        //layout.LayoutParameters is null.  
        layout.LayoutParameters.Width = ViewGroup.LayoutParams.MatchParent;  
        layout.LayoutParameters.Height = ViewGroup.LayoutParams.WrapContent;  
        layout.AddView(inputEditText);  
        return layout;  
    }  
  
    public static void MapPlaceholder(IMaterialEntryHandler handler, IEntry entry) => handler.PlatformView.Hint = entry.Placeholder;  
}  

Result:
196910-screenshot-1651053882.png

How to expand it by width? I am trying to set layout.LayoutParameters.Width = ViewGroup.LayoutParams.MatchParent but layout.LayoutParameters is null.
Thanks.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,208 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Igor Kravchenko 281 Reputation points
    2022-04-29T10:16:25.207+00:00

    Workaround (don't know how good it is):

    protected override TextInputLayout CreatePlatformView()
            {
                //Creating from xml
                LayoutInflater inflater = LayoutInflater.FromContext(Context);
                Android.Views.View view = inflater.Inflate(Resource.Layout.OutlineTextInputLayout, null);
                TextInputLayout layout = (TextInputLayout)view;
    
                //Creating in code
                //TextInputLayout layout = new TextInputLayout(Context);
                //TextInputEditText inputEditText = new TextInputEditText(layout.Context);
                //layout.BoxBackgroundMode = TextInputLayout.BoxBackgroundOutline;
                //layout.AddView(inputEditText);
    
                //Setting minwidth to expand to maximum width.
                layout.MinWidth = layout.MaxWidth;
                return layout;
            }
    
    0 comments No comments