How to set Custom EditTextField properties in android? – Education Career Blog

I am new to android .can any one solve the following problem?
I just create the class like below .I need to know how to set property for the Edit text field

public class CustomEditText extends EditText{

public CustomEditText(Context context) {
    // TODO Auto-generated constructor stub


Note: I mean the property like this
Thanks in advance.


You need to create member variable and methods inside your CustomEditText.
Once you have them you can access it.


So there are a couple ways this can be interpreted and I will try to cover all of them.

EditText has multiple constructors. The one you have overridden requires that you as the developer set the properties in code for the rest of this instances usage. So you can actually just call setText(someString) from within this class or since the method is public call it directly on an instance of your class.

If you override the constructor that contains an attributeSet,

 EditText(Context, AttributeSet)

You can use your component as part of an xml layout and set attributes on it there as if it were another EditText (as long as you call super(context, attributeSet). If you want to define your own custom attributes on top of that then that’s actually quite neat how you do this.

In your project hierarchy, from the root you should either have or need to create a folder called “res/values” Within that folder a file named attr.xml should be created.

<declare-styleable name="myCustomComponents">
<!-- Our Custom variable, optionally we can specify that it is of integer type -->
<attr name="myCustomAttribute" format="integer"/>
<!-- as many as you want -->

Now within your new constructor that makes use of the AttributeSet, you can read this new attribute, “myCustomAttribute”.

public CustomEditText(Context context, AttributeSet set) {
    super(context, set);
    // This parses the attributeSet and filters out to the stuff we care about
    TypedArray typedArray = context.obtainStyledAttributes(R.stylable.myCustomComponents);
    // Now we read let's say an int into our custom member variable.
    int attr = typedArray.getInt(R.styleable.myCustomComponents_myCustomAttribute, 0);
    // Make a public mutator so that others can set this attribute programatically
    // Remember to recycle the TypedArray (saved memory)

Now that we have declared our new attribute and have setup code to read it, we can actually use it programatically or in an XML layout. So let’s say you have an Xml layout in file, layout.xml




So in this we create a layout like normal, but notice we declare a new xml namespace. This is for your new component and your apk. So in this case “custom” is being used it will look inside your defined stylables for new parameters. By doing the previous steps with attr.xml, you have declared “myCustomAttribute” as a component attribute off of the namespace. After that it’s up to you to decide what attributes you would like to expose and what those attributes actually mean. Hope that helps.

Leave a Comment