2

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!");
    }

}

2 Answers 2

5

Add this constructor to your custom class

public MyEditText(Context context,AttributeSet attr) {
    super(context,attr);
    // TODO Auto-generated constructor stub
}
2
  • I've tried this (and adding the other constructor), and I got an error saying I need to specify width... which I'm doing. I tried changing the opening tag to simply <MyEditText></MyEditText> and I got the inflate exception.
    – loeschg
    May 23, 2011 at 16:51
  • Ah... figured out my issue. Had too many right brackets >. Thanks!
    – loeschg
    May 23, 2011 at 16:58
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.