Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Change the colour of the underline in an entry when pressed

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

Best Answer

  • ColeXColeX Member, Xamarin Team Xamurai
    edited April 17 Accepted Answer
    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);
                          }
                      };         
                  }
              }
          }
      }
      

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited April 17 Accepted Answer
    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);
                          }
                      };         
                  }
              }
          }
      }
      

  • HunterCollyHunterColly Member ✭✭

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

  • ColeXColeX Member, Xamarin Team Xamurai

    @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 ?

  • HunterCollyHunterColly Member ✭✭

    @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

  • CaptainXamtasticCaptainXamtastic GBUniversity ✭✭✭
    edited July 21

    (Oops, comment deleted, I overlooked something!)

Sign In or Register to comment.