Search This Blog

28 May 2010

Customizing the Views

Let's start by creating a basic class:
F:\work> mvn archetype:generate -DarchetypeCatalog=http://kallisti.eoti.org:8081/content/repositories/snapshots/archetype-catalog.xml
Choose the galatea-archetype plugin
groupId: org.eoti.android
artifactId: CustomViewTest
version: 1.0-SNAPSHOT
package: org.eoti.android


Assuming your emulator is running...
F:\work> cd CustomViewTest
F:\work\CustomViewTest> mvn clean install

Create a new class.  In my case I created src\main\java\org\eoti\android\CustomTextView.java

public class CustomTextView
extends TextView
{
    public CustomTextView(Context context) {
        super(context);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Paint cornsilk = new Paint(Paint.ANTI_ALIAS_FLAG);
        cornsilk.setColor(Color.rgb(255,248,220));

        canvas.drawLine(0, 0, this.getMeasuredWidth(), this.getMeasuredHeight(), cornsilk);
        canvas.drawLine(this.getMeasuredWidth(), 0, 0, this.getMeasuredHeight(), cornsilk);
       
        super.onDraw(canvas);
    }
}


Update src\main\android\res\layout\main.xml
Replace this line:
    <TextView 
with this line:
    <org.eoti.android.CustomTextView

Redeploy (mvn clean install) and run the app. You should see lines behind the text.

No comments:

Post a Comment