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:
Step 2
XML file
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
-
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Text" />
-
- </LinearLayout>
Step 3
Create a Java file as in the following:
- package com.SampleCanvas;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
-
- public class Second extends Activity {
- SampleCanvasActivity drawView;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- drawView = new SampleCanvasActivity(this);
- drawView.setBackgroundColor(Color.GRAY);
- setContentView(drawView);
- }
- }
Step 4
Create another Java file as in the following:
- package com.SampleCanvas;
-
- import java.util.ArrayList;
-
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Path;
- import android.graphics.Rect;
- import android.graphics.RectF;
- import android.graphics.drawable.Drawable;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.MotionEvent;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.View;
- import android.view.View.OnTouchListener;
- import android.view.ViewGroup.LayoutParams;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.EditText;
- import android.widget.FrameLayout;
-
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout;
-
- public class SampleCanvasActivity extends View {
- Paint paint = new Paint();
- public SampleCanvasActivity(Context context) {
- super(context);
- }
-
- @Override
- public void onDraw(Canvas canvas) {
-
- paint.setColor(Color.BLACK);
- paint.setStyle(Paint.Style.STROKE);
- paint.setStrokeWidth(5);
- canvas.drawRect(730, 430, 40, 20, paint);
-
- Paint paint2=new Paint();
- paint2.setTextSize(35);
- canvas.drawText("Deposit Ammount in your account", 140- paint2.getTextSize(), 100, paint2);
- paint.setTextSize(25);
- canvas.drawText("Enter Amount", 300- paint2.getTextSize(), 200, paint2);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
- }
Step 5
Android Manifest.xml file
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.SampleCanvas"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk android:minSdkVersion="6" />
-
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:name=".Second"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
-
- </manifest>
Step 6
Now you have text inside the rectangle using the Canvas.