Learn How to Draw Text in Canvas In Android

Introduction

 
This article explains how to draw text inside a rectangle using the canvas in Android. Android Studio is used for the sample.
 
In this, you will first create a paint object. Then call the setColor() method of paint to set the color of the rectangle by ing a color variable. The setStyle() method sets the style of the rectangle, here I ed Stroke as an argument that sets the rectangle having a stroke on its side.
 
The setStrokeWidth() method sets the stroke width of the Rectangle. Finally,b I called darwRect() to draw the rectangle. 
 
I have also drawn the text using the drawText() method of the canvas and also called setTextsize() to change the size of the text.
 
Step 1
 
Create a project like this:
 
Clipboard06.jpg
 
Step 2
 
XML file
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.    
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="Text" />  
  11.    
  12. </LinearLayout> 
Step 3
 
Create a Java file as in the following:
  1. package com.SampleCanvas;  
  2. import android.app.Activity;  
  3. import android.graphics.Color;  
  4. import android.os.Bundle;  
  5.    
  6. public class Second extends Activity {  
  7.     SampleCanvasActivity drawView;  
  8.    
  9.     @Override  
  10.     public void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.    
  13.         drawView = new SampleCanvasActivity(this);  
  14.         drawView.setBackgroundColor(Color.GRAY);  
  15.         setContentView(drawView);  
  16.     }  
Step 4
 
Create another Java file as in the following:
  1. package com.SampleCanvas;  
  2.    
  3. import java.util.ArrayList;  
  4.    
  5. import android.app.Activity;  
  6. import android.content.Context;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.BitmapFactory;  
  9. import android.graphics.Canvas;  
  10. import android.graphics.Color;  
  11. import android.graphics.Paint;  
  12. import android.graphics.Path;  
  13. import android.graphics.Rect;  
  14. import android.graphics.RectF;  
  15. import android.graphics.drawable.Drawable;  
  16. import android.os.Bundle;  
  17. import android.view.Gravity;  
  18. import android.view.MotionEvent;  
  19. import android.view.SurfaceHolder;  
  20. import android.view.SurfaceView;  
  21. import android.view.View;  
  22. import android.view.View.OnTouchListener;  
  23. import android.view.ViewGroup.LayoutParams;  
  24. import android.view.Window;  
  25. import android.view.WindowManager;  
  26. import android.widget.EditText;  
  27. import android.widget.FrameLayout;  
  28.    
  29. import android.content.Context;  
  30. import android.graphics.Canvas;  
  31. import android.graphics.Color;  
  32. import android.graphics.Paint;  
  33. import android.view.View;  
  34. import android.widget.ImageView;  
  35. import android.widget.LinearLayout;  
  36. import android.widget.RelativeLayout;  
  37.    
  38. public class SampleCanvasActivity extends View {  
  39.     Paint paint = new Paint();  
  40.     public SampleCanvasActivity(Context context) {  
  41.         super(context);  
  42.     }  
  43.    
  44.     @Override  
  45.     public void onDraw(Canvas canvas) {  
  46.    
  47.         paint.setColor(Color.BLACK);  
  48.         paint.setStyle(Paint.Style.STROKE);  
  49.         paint.setStrokeWidth(5);  
  50.         canvas.drawRect(7304304020, paint);  
  51.    
  52.         Paint paint2=new Paint();  
  53.         paint2.setTextSize(35);  
  54.         canvas.drawText("Deposit Ammount in your account"140- paint2.getTextSize(), 100, paint2);  
  55.         paint.setTextSize(25);  
  56.         canvas.drawText("Enter Amount"300- paint2.getTextSize(), 200, paint2);  
  57.    
  58.         /*ImageView imageView = new ImageView(getContext()); 
  59.         Bitmap mainImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
  60.         imageView.setImageBitmap( mainImage );*/  
  61.        // Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.iamge1);  
  62.    
  63.         //canvas.drawBitmap(bitmap, 120, 500, null);  
  64.         //canvas.drawBitmap(bitmap, 150, 500, null);  
  65.    
  66.    
  67.        // layout.setGravity(Gravity.BOTTOM);  
  68.        //paint.setStyle(Paint.Style.STROKE);  
  69.        // paint.setColor(Color.WHITE);  
  70.         //canvas.drawRect(130, 130, 80, 80, paint);  
  71.         //paint.setColor(Color.BLACK);  
  72.         //canvas.drawText("SomeText",25,20,paint);  
  73.         //paint.setTextSize(20);  
  74.         //canvas.drawText("Some Text", 10, 25, paint);  
  75.        /* canvas.drawRect(33, 60, 77, 77, paint ); 
  76.         paint.setColor(Color.YELLOW); 
  77.         canvas.drawRect(33, 33, 77, 60, paint );*/  
  78.    
  79.     }  
Step 5
 
Android Manifest.xml file
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.SampleCanvas"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk android:minSdkVersion="6" />  
  8.    
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" >  
  12.         <activity  
  13.             android:name=".Second"  
  14.             android:label="@string/app_name" >  
  15.             <intent-filter>  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.    
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.     </application>  
  22.    
  23. </manifest> 
Step 6
 
Clipboard02.jpg
 
Now you have text inside the rectangle using the Canvas.


Similar Articles