Victor Grazi
Victor Grazi

Reputation: 16480

Custom Android EditText not working

I am trying to implement a custom onDraw() method in my EditText. onDraw is getting called - I can see the log messages, but it is not drawing anything.

Can anyone please tell me what I am doing wrong?

Here is an excerpt from my layout:

    <view xmlns:android="http://schemas.android.com/apk/res/android"
          class ="my.package.NotePadEditView"
            android:inputType="textMultiLine"
            android:id="@+id/edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top"
            android:background="@android:color/transparent"
            android:singleLine="false"
            >
        <requestFocus/>
    </view>
</ScrollView>

Here is the class (just with some test code for now):

public class NotePadEditView extends EditText {
Paint paint = new Paint();
public NotePadEditView(Context context, AttributeSet attrs, int defStyle) {
   super(context, attrs, defStyle);
   paint.setStyle(Paint.Style.STROKE);
   paint.setStrokeWidth(3);
   paint.setColor(0xFF0000);
 }
 @Override
 protected void onDraw(Canvas canvas) {
   Log.d("NotePadEditView", "Calling onDraw()"); // These log messages are displaying
   canvas.drawLine(0, 0, 50, 50, paint); // just some random stuff so we know when we are done. (Note: these are not displaying - what's up with that???)
   canvas.drawText("Hello, World", 30, 30, paint);
   super.onDraw(canvas);
  }

// more constructors, etc

Upvotes: 1

Views: 2630

Answers (2)

Victor Grazi
Victor Grazi

Reputation: 16480

ok, finally figured it out. Looks like you need to set the alpha byte on the color assignment:

paint.setColor(0x80FF0000); 

not

paint.setColor(0xFF0000);

Apparently by excluding the alpha byte, you are implicitly passing in zero, which means the color is completely transparent. Java AWT doesn't work that way - who thought that was a good idea?!

Upvotes: 2

Herry
Herry

Reputation: 7087

I think you should try this thing to work Custom EditText in xml of android Layout.

Here is some change made by me in your Class.

public class NotePadEditView extends EditText{
@Override
protected void onDraw(Canvas canvas) {

    Log.d("NotePadEditView", "Calling onDraw()"); // These log messages are displaying
       canvas.drawLine(0, 0, 50, 50, paint); // just some random stuff so we know when we are done. (Note: these are not displaying - what's up with that???)
       canvas.drawText("Hello, World", 30, 30, paint);
       super.onDraw(canvas);

}
Paint paint;

public NotePadEditView(Context context, AttributeSet attrs){
    super(context, attrs);
    //this Contructure required when you are using this view in xml 
    paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(3);
    paint.setColor(Color.BLUE);
}

public NotePadEditView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    paint = new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth(3);
    paint.setColor(0xFF0000);

   }

}

Use in your xml like this ,

   <my.package.NotePadEditView 
            android:id="@+id/edit_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:gravity="top"
            android:inputType="textMultiLine"
            android:singleLine="false" />

hope this will make your work.

Upvotes: 2

Related Questions