Skip to main content

Android Custom TextView

Have you ever tried to add custom behavior to in-build Android Text View or create custom attributes? If yes, this article is going to help you.

Here we'll create Single Custom TextView with support for custom attributes to display First and Last Name in different font and colors. During this process we'll learn following topics-

1. How to override default Views in Android
2. How to define custom Layout Attributes in Android

So, Let's get started...


Following sections explains necessary changes required in Java code and XML layout files.

Create Custom Text View (MyTextView.java)

1. Override Android's default TextView

 2. Implement Constructors. If you want custom attributes, override Constructor having Attributes in argument.

3. Override onMeasure(): Calculate required width and height, based on Text Size and selected Font. Once calculation is complete, set updated measure using setMeasuredDimension(reqWidth, reqHeight)
Note: It’s really important to define the correct size of the required drawing area. Android platform will not let you draw anywhere except your defined drawing area.

4. Override onDraw(Canvas canvas): Create Paint object with desired Font and Color information. Paint objects need to be passed as design briefing while drawing text or shapes.
Note: Android color is 4 byte value and Most Significant byte indicates Alpha value (00- complete transparent i.e. background will be visible and FF- complete non transparent, background is not visible)

Define Custom Attributes

1. Define a new NameSpace: xmlns:my="http://schemas.android.com/apk/res/com.my" (“com.my” is the package name defined in Manifest.xml)

2. Define the custom attributes in /res/values/attrs.xml file. Android platform need to know the new attributes that you will be using. attrs.xml file solves the purpose.
Specify the attributes within . Multiple “attr” tags can be grouped inside of “declare-styleable”. Please refer Custom Layout Resources. We will define 2 attributes- “first_name” and “last_name”.

[xml version="1.0" encoding="utf-8"?]
[resources]
    [declare-styleable name="MyTextView"]
        [attr name="first_name" format="string"/]
        [attr name="last_name" format="string"/]
    [/declare-styleable]
[/resources]

3. Use Custom Attributes in Layout

[LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:my="http://schemas.android.com/apk/res/com.my"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
]
[com.my.ui.MyTextView
android:id="@+id/MyTextView"
android:layout_width="fill_parent"
android:layout_height="50px"
my:first_name="Phil"
my:last_name="Jones"
/]

Access Custom Attributes from Java Code

public MyTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    Log.i(TAG, "MyTextView(ctxt, attrs)");
    Log.i(TAG, "Attributes...");
    for(int i=0; i<attrs.getAttributeCount(); i++){
        Log.i(TAG, attrs.getAttributeName(i));
        /*
         * Read value of custom attributes
         */
        this.firstText = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.my", "first_name");
        this.lastText = attrs.getAttributeValue("http://schemas.android.com/apk/res/com.my", "last_name");
        Log.i(TAG, "firstText "+ firstText);
        Log.i(TAG, "lastText "+ lastText);
    }
    init();
}

Download complete source from Git Hub or Google Code. Share your comments.

Comments

  1. thanks you ,that is very good
    save much my time

    ReplyDelete
  2. Ah very informative, fixed my problem (not even on Custom TextViews, other stuff, but the info was helpful). Thanks.

    ReplyDelete
  3. plz provide example source code...
    download link not working..
    thanks

    ReplyDelete
  4. not able to download the zip redirecting to this link....http://blogresources.googlegroups.com/web/CustomTextView.rar?hl=en

    ReplyDelete
  5. i wish i still can download the CustomTextView demo! =/

    ReplyDelete
  6. Pls download the code from-
    http://code.google.com/p/android-java-sample-code/downloads/list

    -Prasanta

    ReplyDelete
    Replies
    1. huray, my wish come true! thankyou so much!^_^!

      Delete
  7. Unlike the regular TextView in which if the text is too long to fit on the screen, it would start a new line; However, on your example if the text is too long to fit the screen, it would just continue on the same line and then disappeared(the part that doesn't fit). Do you know of a way to draw the text(if its too long to fit in one line) on a newline instead?

    thanks for the great tutorial and let me know if you think of a solution. thanks

    ReplyDelete
  8. To add the text wrap functionality, you need to calculate Width of your complete Text and based on the Width available for TextView, need to break the text and shift Y-co-ordinate by the char height and some padding.
    Well you need to right this logic. I don't have any existing sample.

    ReplyDelete
  9. Thank you very much Prasanta...very good tutorial...helped me a lot...

    ReplyDelete
  10. i don't usually comments on articles , but really you maaaaaade my daaaaaaaay , i have been searching every where , i was trying to get the attributes from the class , but i was writing the schema name only without the full url , thaaaaanks a lot

    ReplyDelete

Post a Comment