Msui android entry focus line issue

Haviv Elbsz 1,926 Reputation points
2022-12-28T20:56:14.67+00:00

Hi all

Is it possible to show the line in the android entry control
only when the entry get the focus
and to hide it when not

Thank you very much

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

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 36,456 Reputation points Microsoft Vendor
    2022-12-29T08:07:20.493+00:00

    Hello,

    It is more recommended to hide the underline of entry. Then, you could use a BoxView as the underline of the entry to implement this feature.

    This is because for an element style, it would be better if we could implement it using cross-platform UI classes. This helps maintain the code and facilitates subsequent expansion to other platforms.

    You could refer to the following code sample to implement this feature:

       // First hide the underline of entry via handler:  
       // Call this method in the constructor method of your Page.  
       void ModifyEntry()  
       {  
           Microsoft.Maui.Handlers.EntryHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>  
           {  
       #if ANDROID  
               GradientDrawable gd = new GradientDrawable();  
               gd.SetColor(global::Android.Graphics.Color.Transparent);  
               handler.PlatformView.SetBackground(gd);  
               handler.PlatformView.SetRawInputType(InputTypes.TextFlagNoSuggestions);  
               handler.PlatformView.SetHintTextColor(ColorStateList.ValueOf(global::Android.Graphics.Color.White));  
       #endif  
           });  
       }  
    

    Then, you could use the BoxView to draw a underline for the Entry:

       <ContentView Margin="10">  
           <VerticalStackLayout>  
               <Entry Focused="Entry_Focused" Unfocused="Entry_Unfocused"/>  
               <BoxView x:Name="underline" IsVisible="false" BackgroundColor="Black" HeightRequest="1"/>  
           </VerticalStackLayout>  
       </ContentView>  
    

    Finally, you just need to set the visibility of BoxView in focused and unfocused events of the Entry, like underline.IsVisible = false;.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful