Custom Controls for basic Android Controls

I was wondering if anyone knew of any custom controls that were available for Android specifically related to the EditText controls. For example, I would like a EditText to display "28 lbs" when it does not have focus, but with focus is attained, just display "28" and allow you to edit the number. This is similar to what Telerik has with their RADAJAX controls. By using the

NumberFormat-PositivePattern="n lbs"

Inside the markup, this would allow the lbs to show when the item is not selected.

I also know that this probably wouldn't be that difficult to build, but if someone had already built it, it would save me some time and effort to do so.

Thanks.

Chaitanya Marvici

Posts

  • CheesebaronCheesebaron DKInsider, University mod
    edited November 2012

    There are two ways you can obtain this:

    First one is to simply set the FocusChange event of an EditText to something like this:

    editText2.FocusChange += (sender, args) =>
    {
        if (args.HasFocus)
        {
            if (!string.IsNullOrEmpty(editText2.Text))
                editText2.Text =
                    editText2.Text.Substring(0, 
                        editText2.Text.Length - 
                        suffix.Length);
        }
        else
        {
            editText2.Text += suffix;
        }
    };
    

    Where suffix is the wanted suffix.

    The other way is to set this FocusChanged event handler in your own class inherited from EditText

    using System;
    using Android.Content;
    using Android.Runtime;
    using Android.Util;
    using Android.Widget;
    
    namespace DK.Ostebaronen.EditTextFun
    {
        public class MyEditText : EditText
        {
            public string Suffix { get; set; }
    
            protected MyEditText(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
            {
                FocusChange += OnFocusChange;
            }
    
            public MyEditText(Context context) : this(context, null)
            {
            }
    
            public MyEditText(Context context, IAttributeSet attrs) : this(context, attrs, 0)
            {
            }
    
            public MyEditText(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
            {
                FocusChange += OnFocusChange;
            }
    
            private void OnFocusChange(object sender, FocusChangeEventArgs focusChangeEventArgs)
            {
                if (!string.IsNullOrEmpty(Suffix))
                {
                    if (focusChangeEventArgs.HasFocus)
                    {
                        if (!string.IsNullOrEmpty(Text))
                            Text = Text.Substring(0, Text.Length - Suffix.Length);
                    }
                    else
                    {
                        Text += Suffix;
                    }
                }
            }
        }
    }
    

    And use this class instead of EditText just remembering to set the Suffix property.

    I have attached a sample project for you to try.

  • CheesebaronCheesebaron DKInsider, University mod

    You probably also want to remove the FocusChanged event handler by overriding Dispose:

    protected override void Dispose(bool disposing)
    {
        FocusChange -= OnFocusChange;
        base.Dispose(disposing);
    }
    
  • ChaitanyaMarviciChaitanyaMarvici USMember
    edited November 2012

    Well I have to say that you guys rock.

    While I was hoping for something a little more robust, your suggestion did the trick. I had to make some additional changes to it (shown below), but overall this works for what I need now.

        public class FormattedEditText : EditText
        {
            public String Suffix { get; set; }
    
            public new String Text
            {
                get { return base.Text.Substring(0, base.Text.Length - (Suffix.Length + 1)); }
                set
                {
                    base.Text = value;
                    SetText(false);
                }
            }
    
            protected FormattedEditText(IntPtr javaReference, JniHandleOwnership transfer)
                : base(javaReference, transfer)
            {
                FocusChange += OnFocusChange;
            }
    
            public FormattedEditText(Context context)
                : this(context, null)
            {
            }
    
            public FormattedEditText(Context context, IAttributeSet attrs)
                : this(context, attrs, 0)
            {
            }
    
            public FormattedEditText(Context context, IAttributeSet attrs, int defStyle)
                : base(context, attrs, defStyle)
            {
                FocusChange += OnFocusChange;
            }
    
            private void OnFocusChange(object sender, FocusChangeEventArgs focusChangeEventArgs)
            {
                if (string.IsNullOrEmpty(Suffix)) return;
    
                SetText(focusChangeEventArgs.HasFocus);
            }
    
            private void SetText(Boolean hasFocus)
            {
                if (hasFocus)
                {
                    if (!string.IsNullOrEmpty(Text))
                    {
                        base.Text = base.Text.Substring(0, base.Text.Length - (Suffix.Length + 1));
                        base.SetSelection(base.Text.Length);
                    }
                }
                else
                    base.Text += " " + Suffix;
            }
    
            protected override void Dispose(bool disposing)
            {
                FocusChange -= OnFocusChange;
                base.Dispose(disposing);
            }
        }
    

    I needed the cursor to appear at the end of the actual text when focus was acquired by the edit text as well strip out the suffix when the text property was recovered from the item.

    I hope that this helps someone else out!

    Thanks!

    Chaitanya Marvici

Sign In or Register to comment.