Posted By Our Mentors
Amreen Khan
Android Developer
animesh bansal
iphone App Developer
Devendra chouksey
Android Developer
Dilip Saket
IOS apps developer
Hemraj Jhariya
iOS Developer
Jaihind singh Rajput
Smart Phone App Dev
Mujahid khan
Android developer
Narendra Yadav
android developer
Narinder Gupta
Mobile App Architect
Pradeep singh Thakur
Mobile App Architect
Sanjay Sharma
Andriod Developer
Shakti Rajpal
IT Architect
Soniya Vishwakarma
iPhone App Developer
Sunil Raghuwanshi
iPhone App Developer
Supriya Kapse
Senior developer
View current Topics
Draw primitive shapes such as rectangle, circle etc in ANDROID using canvas- Part II Android
In the second part of this tutorial series, I will show you what you can do with canvas and paint. Here I used canvas to draw the rectangle primitive.
Posted By: Amreen Khan Date: 12 Jul 2012 View: 1178
      Rate This !!

The following example shows how to draw a rectangle using the Paint class in ANDROID.

package com.canvas;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
public class CanvasFirstActivity extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        setContentView(new myView(this));
    //Visual indicator of progress in some operation.  
    //SurfaceView Provides a dedicated drawing surface embedded inside of a view hierarchy. 
    private class myView extends View{
        public myView(Context context) {
        //The Canvas class holds the "draw" calls. To draw something, 
        //you need 4 basic components: A Bitmap to hold the pixels, 
        //a Canvas to host the draw calls (writing into the bitmap),
        //a drawing primitive (e.g. Rect, Path, text, Bitmap), and 
        //a paint (to describe the colors and styles for the drawing). 

        protected void onDraw(Canvas canvas) {
        //The Paint class holds the style and color information about 
        //how to draw geometries, text and bitmaps. 

            Paint myPaint = new Paint();
            //Set the paint's color.
            //Set the paint's style, used for controlling how primitives' geometries are interpreted 
            //Set the width for stroking. 
            //Draw the specified Rect using the specified paint.
            //The rectangle will be filled or framed based on the Style in the paint.

            canvas.drawRect(10, 10, 100, 100, myPaint);



In both the article of canvas main.xml is used as default.There is no changes applied in layout files.

Any queries or suggestions are most welcome! Thank's

Amreen Khan

Posted Queries