Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the right way to extend EditText to give it additional "default" functionality

I'm wondering if it's possible to add functionality to EditText such that when I include my newly extended field in the layout xml, I don't have to then add any code to the Activity class to get it to behave in specific ways.

For example, I'd like to make a EditPhone field which is just an EditText that has the added feature of listening for key events and modifying the field to include parenthesis and dashes in their appropriate locations.

At the moment, I'm always having to include the listener code and attach it to the view, manually. But obviously the class has a ton of default behavior that is wrapped up in it (for example, it brings up the keyboard when you click it). So, I'm guessing it shouldn't be all that tough, but I'm not clear on what the steps would be to accomplish this.

And to be clear, I don't need help with the Phone specific feature described above (I have that all worked out), I'm trying to understand how to extend View in a way that it takes on additional functionality by default, so as not to have to clutter my Activities with the same code over and over.

like image 343
Yevgeny Simkin Avatar asked Sep 22 '11 16:09

Yevgeny Simkin


People also ask

Is there a way to define a min and max value for EditText in android?

setFilters(new InputFilter[]{ new InputFilterMinMax("1", "12")}); This will allow user to enter values from 1 to 12 only. EDIT : Set your edittext with android:inputType="number" .

How do I know if EditText is focused?

You can use View. OnFocusChangeListener to detect if any view (edittext) gained or lost focus. This goes in your activity or fragment or wherever you have the EditTexts.

How do I change my EditText value?

Set the Text of Android EditText In android, we can set the text of EditText control either while declaring it in Layout file or by using setText() method in Activity file. Following is the example to set the text of TextView control while declaring it in XML Layout file.


1 Answers

Actually there is nothing complicated about that. Usually you would apply a InputFilter to your EditText in your code and this would do the job. But if you see a pattern in that and want a EditText which always behaves that way you could create a custom widget that way:

public class PhoneEditText extends EditText {

    public PhoneEditText(Context context) {
        super(context);
        init();
    }

    public PhoneEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PhoneEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
            // set your input filter here
    }
}

In XML layout you would simply use the full path to your custom class instead EditText:

<my.package.path.to.PhoneEditText
   attribute="value (all EditText attributes will work as they did before)" />
like image 164
Knickedi Avatar answered Oct 20 '22 20:10

Knickedi