Reputation: 367
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?
Upvotes: 1
Views: 1052
Reputation: 37516
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
Upvotes: 6