loeschg
loeschg

Reputation: 30581

Custom EditText in Android: How do you reference in XML Layout?

I've created a custom EditText object called MyEditText (extends EditText) in order to override the onSelectionChanged(int start, int end) method. When I do this and attempt to reference this object type in my XML layout, I always get an error inflating class message. This is what I currently have:

<cse.intro.networking.security.MyEditText
     android:layout_width="match_parent" android:id="@+id/editText2"
     android:layout_height="match_parent" android:text="@string/editTextBoxInit">
</cse.intro.networking.security.MyEditText>

MyEditText.java:

package cse.intro.networking.security;

import android.content.Context;
import android.util.Log;
import android.widget.EditText;

public class MyEditText extends EditText {
    private final String tag = "SimpleGUI";
    public MyEditText(Context context) {
        super(context);
    }

    @Override
    public void onSelectionChanged(int selStart, int selEnd) {
        Log.v(tag, "onSelectionChanged!");
    }

}

Upvotes: 2

Views: 4174

Answers (2)

Sujit
Sujit

Reputation: 10622

Add this constructor to your custom class

public MyEditText(Context context,AttributeSet attr) {
    super(context,attr);
    // TODO Auto-generated constructor stub
}

Upvotes: 5

Neo
Neo

Reputation: 169

You need to overwrite all the constructors for the EditText if you want to use it in the XML layout.

Upvotes: 1

Related Questions