How to set hint (placeholder) color for material entry in android handler?

Igor Kravchenko 281 Reputation points
2022-04-27T15:56:28.307+00:00

I want to set placeholder color for my material entry.

public static void MapPlaceholderColor(IMaterialEntryHandler handler, IMaterialEntry entry)
 {
 if (entry.PlaceholderColor == null)
 return;
 handler.PlatformView.DefaultHintTextColor = ???
 }

PlatformView is Google.Android.Material.TextField.TextInputLayout. There are two properties for hint text color: DefaultHintTextColor and HintTextColor. The both are of type Android.Content.Res.ColorStateList. How to set maui color to these properties? And what the property should I use?
Thanks.

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

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 71,836 Reputation points Microsoft Vendor
    2022-04-28T06:51:56.867+00:00

    Hello,​

    textInputLayout.DefaultHintTextColor; Sets the text color used by the hint in both the collapsed and expanded states

    textInputLayout.HintTextColor Sets the collapsed hint text color

    They are different, Which one to use depends on your choice.

    I make a test with DefaultHintTextColor and create a ColorStateList to set the enabled state, and set color to red for enabled state.

       protected override TextInputLayout CreateNativeView()  
               {  
                   int[][] states= new int[][] {  
                           new int[] { Android.Resource.Attribute.StateEnabled}, // enabled                    
                          };  
                   int[] colors = new int[] {  
                           Android.Graphics.Color.Red,                    
                       };  
                   ColorStateList myList = new ColorStateList(states, colors);  
                   TextInputLayout layout = new TextInputLayout(Context);       
                   layout.DefaultHintTextColor = myList;  
         
                   TextInputEditText inputEditText = new TextInputEditText(layout.Context);            
                   inputEditText.Hint = "this is a hint text";  
                   
                   layout.AddView(inputEditText);  
                   return layout;  
               }  
    

    Best Regards,

    Leon Lu


    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