locked
Change the colour of the underline in an entry when pressed RRS feed

  • Question

  • User393994 posted

    Hey, just wondering if anyone knows how to change the colour of the underline in an entry when it is pressed, or when the entry it is not being edited the line is not visible and when it is being edited the line shows. Kind regards Hunter

    Friday, April 17, 2020 9:01 AM

Answers

  • User369978 posted
    1. Create custom renderer for Entry .

    2. Set the custom color on underline when the entry gets focused.

    3. Hide the underline when the entry gets unfocused.

      [assembly: ExportRenderer(typeof(Entry), typeof(MyRenderer))]
      namespace App2.Droid
      {
          class MyRenderer : EntryRenderer
          {
              public MyRenderer(Context context) :base(context)
              {
      
              }
      
              void SetColor(Android.Graphics.Color color)
              {
                  if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
                  {
                      Control.BackgroundTintList = ColorStateList.ValueOf(color);
                  }
                  else
                  {
                      Control.Background.SetColorFilter(color, PorterDuff.Mode.SrcAtop);
                  }
              }
      
              protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
              {
                  base.OnElementChanged(e);
      
                  if (Control != null)
                  {
                      SetColor(Android.Graphics.Color.Transparent);
      
                      this.EditText.FocusChange += (sender,ee) => {
                          bool hasFocus = ee.HasFocus;
                          if (hasFocus)
                          {
                              SetColor(Android.Graphics.Color.Blue);
                          }
                          else
                          {
                              SetColor(Android.Graphics.Color.Transparent);
                          }
                      };         
                  }
              }
          }
      }
      

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, April 17, 2020 11:35 AM

All replies

  • User369978 posted
    1. Create custom renderer for Entry .

    2. Set the custom color on underline when the entry gets focused.

    3. Hide the underline when the entry gets unfocused.

      [assembly: ExportRenderer(typeof(Entry), typeof(MyRenderer))]
      namespace App2.Droid
      {
          class MyRenderer : EntryRenderer
          {
              public MyRenderer(Context context) :base(context)
              {
      
              }
      
              void SetColor(Android.Graphics.Color color)
              {
                  if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
                  {
                      Control.BackgroundTintList = ColorStateList.ValueOf(color);
                  }
                  else
                  {
                      Control.Background.SetColorFilter(color, PorterDuff.Mode.SrcAtop);
                  }
              }
      
              protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
              {
                  base.OnElementChanged(e);
      
                  if (Control != null)
                  {
                      SetColor(Android.Graphics.Color.Transparent);
      
                      this.EditText.FocusChange += (sender,ee) => {
                          bool hasFocus = ee.HasFocus;
                          if (hasFocus)
                          {
                              SetColor(Android.Graphics.Color.Blue);
                          }
                          else
                          {
                              SetColor(Android.Graphics.Color.Transparent);
                          }
                      };         
                  }
              }
          }
      }
      

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, April 17, 2020 11:35 AM
  • User393994 posted

    I'm having trouble using the custom entry, how do I use it. Code would help. Thanks, Hunter

    Friday, April 17, 2020 11:42 PM
  • User369978 posted

    @HunterColly said: I'm having trouble using the custom entry, how do I use it. Code would help. Thanks, Hunter

    sorry , i didn't get it , do you mean how to make custom renderer work on custom entry ?

    Monday, April 20, 2020 6:43 AM
  • User393994 posted

    @ColeX said:

    @HunterColly said: I'm having trouble using the custom entry, how do I use it. Code would help. Thanks, Hunter

    sorry , i didn't get it , do you mean how to make custom renderer work on custom entry ?

    nevermind I got it, thanks

    Monday, April 20, 2020 9:49 AM
  • User46653 posted

    (Oops, comment deleted, I overlooked something!)

    Tuesday, July 21, 2020 4:50 PM