1

I am trying to create a custom edit text and got stuck up here...

Please see my code below

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


public static  class MyEditText1 extends EditText{

    Paint mPaint;




    public MyEditText1(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

    }

    public MyEditText1(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint=new Paint();
        mPaint.setColor(Color.BLACK);


    }

    @Override
    protected  void onDraw(Canvas c){
        super.onDraw(c);
        int height=getHeight();
        int width=getWidth();
        int linespace=10;
    int count=height/linespace;

        for(int i=0;i<count;i++){
            c.drawLine(0, i*linespace, width, i*linespace, mPaint);
        }


    }



     }


}

MyEditText class is the inner class

and i am refering to this view in my xml as

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >



<com.example.customedittext.MainActivity.MyEditText1

    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:ems="10" 
    android:inputType="textNoSuggestions"/>


 </RelativeLayout>

But the app is getting force closed immediately after starting.But when i keep the inner class in a seperate file as MyEditText.java(and refering to it in xml) it works fine ..what is the problem with keeping it as inner class..even though i have made my inner class static?

amj
  • 367
  • 1
  • 8
  • 26

1 Answers1

6

Because your view is an inner class, you need to refer to it slightly different from your XML (note the $ symbol):

com.example.customedittext.MainActivity$MyEditText1
wsanville
  • 37,158
  • 8
  • 76
  • 101
  • Making it as you said gives me the following error in xml file ---"Multiple annotations found at this line: - Element type "com.example.customedittext.MainActivity" must be followed by either attribute specifications, ">" or "/>". " – amj Oct 25 '12 at 18:32
  • 1
    Thanks @wsanville for your hint...i figured it out..actually when the view class is a nested we need to use the class attribute in xml Here is the link.. to [Use Custom component](http://developer.android.com/guide/topics/ui/custom-components.html#modifying) – amj Oct 25 '12 at 19:07
  • @amj You should select his answer as the correct one- his answer just helped me take care of the issue I was having. – Kirk Sep 06 '13 at 14:00