Monday, February 27, 2012

Setting the background image of an Android TextView

TextView displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing; see for a subclass that configures the text view for editing.
In most cases u want add image as textView background. It's so easy. In layout xml file u must write:




<textview 
android:background="@drawable/bubble" 
android:id="@+id/textViewSMS" 
android:layout_gravity="center_vertical" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:text="A normal PNG file (*.png) will be loaded with an empty one-pixel border added around the image, in which you can draw the stretchable patches and content area. A previously saved 9-patch file (*.9.png) will be loaded as-is, with no drawing area added, because it already exists."
</textview>
Then u will see result as following:

Of course u want see text in bubble :) For this u must edit image with 9patch
Then save image as 9.png and add to project drawable folder. Save project and run again if everything goes ok u must see result as following:

If u find this post useful then don't forget leavecomment :)


4 comments:

  1. Hi, thanks for great tutorial.

    May i know exactly where to put the 9.png?

    ReplyDelete
  2. Not at all. Put your imagename.9.png file in the same folder which pngs are. So put in in your drawable folder and enjoy :)

    ReplyDelete
  3. thank you, it is working for me. :)

    ReplyDelete