Android Mobile Food Ordering App For Restaurant – Project Idea Source Code

This is part of the android student’s project idea. I have received many request on Android Mobile Food Ordering App for Restaurant from readers on how to create it in android.

This is not a complete project for anybody working on a related topics but rather it is for learning purpose. By going through the source code you can learn how to implement a feature(s) you might need in your project.

This project will give you an insight on how to create android food ordering application and you can use the source code as a foundation to achieve your project requirement.

The application comes with different features. You can easily customize it the way you want.

This project comes with source code, documentation, Database Schema, Use Case Diagram and Web admin panel in Php and Mysql.

BUY NOW

 

I also have custom android and iOS applications I have develop that will be of help to students planning their thesis, dissertation or course project in mobile related fields.

Below are some of the screen-shots associated with this project.

Login and Registration Section

shot1 shot2

Menu Category and Menu Items

menusection

Order History, Favorite and Hot Deals

sectiontwo

Cart, Checkout, Payment and Order Confirmation

payment1 payment2

Database EER Diagram

dbdesign

Sample Code Snippet from the project

codes

Server Side Web Admin Panel

panel

 

APK File for testing purposes.

Please note that the latest changes might not reflect in the apk file. Kindly ask me any question with respect to that.

 

Package Includes

1 Android Java Source code

2. Web Admin Panel (Php and MYSQL)

3. Database file (.sql file)

4. Documentation

 

Link to documentation and setup questions

 

Promotion Price : $10

 

If you do not have any experience with android or Slim 3 Php framework then this source code is not for you. But if you still need to and will like me to set it up for you then you can contain for the setup price.

How to scale Canvas in Android

In this android programming source code example, we are going to scale Canvas in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to scale Canvas in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/canvas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/scale"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:textColor="#ffffff"
        android:layout_alignParentBottom="true"
        android:text="Scale Canvas"/>

</RelativeLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;


public class CanvasActivity16 extends AppCompatActivity {
    private RelativeLayout view;
    private Button scale;
    private canvas16 customcanvas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canvas16);
        view = (RelativeLayout) findViewById(R.id.canvas);
        scale = (Button) findViewById(R.id.scale);
        customcanvas = new canvas16(CanvasActivity16.this, null);
        view.addView(customcanvas);
        scale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customcanvas.scaleCanvas();
            }
        });
    }
    public static class canvas16 extends View {
        private Paint paint;
        private boolean cc = false;
        private Canvas mcanvas;
        public canvas16(Context context, AttributeSet attributeSet){
            super(context, attributeSet);
            paint = new Paint();
            paint.setColor(getResources().getColor(R.color.colorAccent));
            paint.setStrokeWidth(10);
            paint.setStyle(Paint.Style.STROKE);
        }

        @Override
        protected void onDraw(Canvas canvas){
            super.onDraw(canvas);
            mcanvas = canvas;
            if(cc){
                mcanvas.save();
                mcanvas.scale(0.5f,0.5f);
                mcanvas.drawRect(50, 50, 200, 200, paint);
                mcanvas.restore();
            }else{
                mcanvas.drawRect(50, 50, 200, 200, paint);
                cc = false;
            }
        }

        public void scaleCanvas(){
            cc = true;
            invalidate();
        }
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to convert Canvas to Bitmap in android

In this android programming source code example, we are going to convert Canvas to Bitmap in android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to convert Canvas to Bitmap in android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>

</RelativeLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;


public class CanvasActivity17 extends AppCompatActivity {
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canvas17);
        img = (ImageView) findViewById(R.id.image);
        View view = new canvas17(CanvasActivity17.this, null);
        Bitmap bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        img.setImageBitmap(bitmap);
    }

    public static class canvas17 extends View {
        private Paint paint;
        public canvas17(Context context, AttributeSet attributeSet){
            super(context, attributeSet);
            paint = new Paint();
            paint.setColor(getResources().getColor(R.color.colorAccent));
            paint.setStrokeWidth(10);
            paint.setStyle(Paint.Style.STROKE);
        }

        @Override
        protected void onDraw(Canvas canvas){
            super.onDraw(canvas);
            canvas.drawRect(50,50,400,250, paint);
        }
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to undo Canvas drawing in Android

In this android programming source code example, we are going to undo Canvas drawing in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to undo Canvas drawing in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/canvas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">
        <Button
            android:id="@+id/undo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:textColor="#ffffff"
            android:text="Undo"/>

        <Button
            android:id="@+id/redo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:layout_marginLeft="2dp"
            android:textColor="#ffffff"
            android:layout_weight="1"
            android:text="Redo"/>
    </LinearLayout>

</RelativeLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import java.util.ArrayList;

public class CanvasActivity18 extends AppCompatActivity {
    private RelativeLayout view;
    private Button redo;
    private Button undo;
    private canvas18 customcanvas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canvas18);
        view = (RelativeLayout) findViewById(R.id.canvas);
        redo = (Button) findViewById(R.id.redo);
        undo = (Button) findViewById(R.id.undo);
        customcanvas = new canvas18(CanvasActivity18.this, null);
        view.addView(customcanvas);
        redo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customcanvas.onClickRedo();
            }
        });
        undo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customcanvas.onClickUndo();
            }
        });
    }

    public static class canvas18 extends View {
        private Canvas mCanvas;
        private Path mPath;
        private Paint mPaint;
        private ArrayList<Path> paths = new ArrayList<Path>();
        private ArrayList<Path> undonePaths = new ArrayList<Path>();
        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;

        public canvas18(Context context, AttributeSet attributeSet) {
            super(context, attributeSet);
            mPaint = new Paint();
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setColor(getResources().getColor(R.color.colorAccent));
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(6);
            mCanvas = new Canvas();
            mPath = new Path();
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            for (Path p : paths){
                canvas.drawPath(p, mPaint);
            }
            canvas.drawPath(mPath, mPaint);
        }

        public void onClickUndo () {
            if (paths.size()>0) {
                undonePaths.add(paths.remove(paths.size()-1));
                invalidate();
            }else{
            }
        }

        public void onClickRedo (){
            if (undonePaths.size()>0){
                paths.add(undonePaths.remove(undonePaths.size()-1));
                invalidate();
            }else {
            }
        }

        @Override
        public boolean onTouchEvent(MotionEvent event){
            float x = event.getX();
            float y = event.getY();
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    undonePaths.clear();
                    mPath.reset();
                    mPath.moveTo(x, y);
                    mX = x;
                    mY = y;
                    invalidate();
                    break;

                case MotionEvent.ACTION_MOVE:
                    float dx = Math.abs(x - mX);
                    float dy = Math.abs(y - mY);
                    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                        mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
                        mX = x;
                        mY = y;
                    }
                    invalidate();
                    break;

                case MotionEvent.ACTION_UP:
                    mPath.lineTo(mX, mY);
                    mCanvas.drawPath(mPath, mPaint);
                    paths.add(mPath);
                    mPath = new Path();
                    invalidate();
                    break;
            }
            return true;
        }
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to move text in Android Canvas

In this android programming source code example, we are going to move text in Android Canvas.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to move text in Android Canvas.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <view class="com.bluapp.androidview2.Canvas.CanvasActivity19$canvas19"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


public class CanvasActivity19 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canvas19);
    }
    public static class canvas19 extends View {
        private float x = 50;
        private float y = 60;
        private Paint paint;

        public canvas19(Context context, AttributeSet attributeSet) {
            super(context, attributeSet);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            paint = new Paint();
            paint.setColor(getResources().getColor(R.color.colorAccent));
            paint.setStyle(Paint.Style.FILL);
            paint.setTextSize(30);
            canvas.drawText("Hello World", x, y, paint);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event){
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    break;

                case MotionEvent.ACTION_MOVE:
                    x = (int)event.getX();
                    y = (int)event.getY();
                    invalidate();
                    break;

                case MotionEvent.ACTION_UP:
                  //  x = (int)event.getX();
                   // y = (int)event.getY();
                    break;
            }
            return true;
        }
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to move image in android Canvas

In this android programming source code example, we are going to move image in android Canvas.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to move image in android Canvas.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <view class="com.bluapp.androidview2.Canvas.CanvasActivity20$canvas20"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

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.os.Bundle;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


public class CanvasActivity20 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canvas20);
    }

    public static class canvas20 extends View {
        private Bitmap bitmap;
        private float x, y;
        private Paint paint;

        public canvas20(Context context, AttributeSet attributeSet) {
            super(context, attributeSet);
            bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_user);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.CYAN);
            canvas.drawBitmap(bitmap, x, y, paint);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event){
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    break;

                case MotionEvent.ACTION_MOVE:
                    x = (int)event.getX();
                    y = (int)event.getY();
                    invalidate();
                    break;

                case MotionEvent.ACTION_UP:
                    x = (int)event.getX();
                    y = (int)event.getY();
                    break;
            }
            return true;
        }
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to use Path to draw Cubic and Quadratic Bezier curves in android Canvas

In this android programming source code example, we are going to use Path to draw Cubic and Quadratic Bezier curves in android Canvas.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to use Path to draw Cubic and Quadratic Bezier curves in android Canvas.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <view class="com.bluapp.androidview2.Canvas.CanvasActivity21$canvas21"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;


public class CanvasActivity21 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canvas21);
    }

    public static class canvas21 extends View {
        private Paint paint;
        private Path path;
        public canvas21(Context context, AttributeSet attributeSet){
            super(context, attributeSet);
            path = new Path();
            paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
        }

        @Override
        protected void onDraw(Canvas canvas){
            super.onDraw(canvas);
            paint.setColor(getResources().getColor(R.color.colorAccent));
            paint.setStrokeWidth(3);
            path.moveTo(50,50);
            path.cubicTo(300, 50, 100, 400, 400, 400);
            canvas.drawPath(path, paint);

            path.reset();
            paint.setColor(getResources().getColor(R.color.colorPrimary));
            paint.setStrokeWidth(1);
            path.moveTo(50,50);
            path.lineTo(300,50);
            path.lineTo(100,400);
            path.lineTo(400,400);
            canvas.drawPath(path, paint);

        }

    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to rotate Canvas in Android

In this android programming source code example, we are going to rotate Canvas in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to rotate Canvas in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:id="@+id/canvas"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/rotate"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:textColor="#ffffff"
        android:layout_alignParentBottom="true"
        android:text="Rotate"/>

</RelativeLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;


public class CanvasActivity15 extends AppCompatActivity {
    private RelativeLayout view;
    private Button rotate;
    private canvas15 customcanvas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canvas15);
        view = (RelativeLayout) findViewById(R.id.canvas);
        rotate = (Button) findViewById(R.id.rotate);
        customcanvas = new canvas15(CanvasActivity15.this, null);
        view.addView(customcanvas);
        rotate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                customcanvas.rotateCanvas();
            }
        });
    }

    public static class canvas15 extends View {
        private Paint paint;
        private Canvas mcanvas;
        private boolean cc = false;
        public canvas15(Context context, AttributeSet attributeSet){
            super(context, attributeSet);
            paint = new Paint();
            paint.setColor(getResources().getColor(R.color.colorAccent));
            paint.setStrokeWidth(10);
            paint.setStyle(Paint.Style.STROKE);
        }

        @Override
        protected void onDraw(Canvas canvas){
            super.onDraw(canvas);
            mcanvas = canvas;
            if(cc){
                mcanvas.save();
                mcanvas.rotate(25, mcanvas.getWidth()/2, mcanvas.getHeight()/2);
                mcanvas.drawRect(50,50,200,200, paint);
                mcanvas.restore();
            }else{
                mcanvas.drawRect(50,50,200,200, paint);
                cc = false;
            }
        }

        public void rotateCanvas(){
            cc = true;
            invalidate();
        }
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to draw on Android Canvas using finger

In this android programming source code example, we are going to draw on Android Canvas using finger.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to draw on Android Canvas using finger.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <view class="com.bluapp.androidview2.Canvas.CanvasActivity14$canvas14"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


public class CanvasActivity14 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canvas14);
    }

    public static class canvas14 extends View {
        private Paint paint;
        private Path path;

        public canvas14(Context context, AttributeSet attributeSet) {
            super(context, attributeSet);
            paint = new Paint();
            path = new Path();
            paint.setColor(getResources().getColor(R.color.colorAccent));
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeJoin(Paint.Join.ROUND);
            paint.setStrokeCap(Paint.Cap.ROUND);
            paint.setStrokeWidth(10);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawPath(path, paint);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event){
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    path.moveTo(event.getX(), event.getY());
                    break;

                case MotionEvent.ACTION_MOVE:
                    path.lineTo(event.getX(), event.getY());
                    invalidate();
                    break;

                case MotionEvent.ACTION_UP:
                    break;
            }
            return true;
        }
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to draw a shape outside Canvas in Android

In this android programming source code example, we are going to draw a shape outside Canvas in Android.

You can copy and adopt this source code example to your android project without reinventing the wheel.

Below is a step by step source code to draw a shape outside Canvas in Android.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <view class="com.bluapp.androidview2.Canvas.CanvasActivity11$canvas11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;


public class CanvasActivity11 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canvas11);
    }

    public static class canvas11 extends View {
        private Paint paint;
        public canvas11(Context context, AttributeSet attributeSet){
            super(context, attributeSet);
            paint = new Paint();
            paint.setColor(getResources().getColor(R.color.colorAccent));
            paint.setStrokeWidth(10);
            paint.setStyle(Paint.Style.STROKE);
        }

        @Override
        protected void onDraw(Canvas canvas){
            super.onDraw(canvas);
            final int saveCount = canvas.save();
            try {
                canvas.translate(200, 0);
                canvas.drawRect(50,50,200,200, paint);
            }finally {
                canvas.restoreToCount(saveCount);
            }
        }

    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

How to measure text width and height on Android Canvas

You can copy and adopt this source code example to your android project without reinventing the wheel.

In this android programming source code example, we are going to measure the text width and height on Android Canvas.

Below is a step by step source code to measure the text width and height on Android Canvas.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <view class="com.bluapp.androidview2.Canvas.CanvasActivity13$canvas13"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Toast;

public class CanvasActivity13 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_canvas13);
    }

    public static class canvas13 extends View {
        private Paint paint;
        private Rect bounds;
        public canvas13(Context context, AttributeSet attributeSet){
            super(context, attributeSet);
            paint = new Paint();
            bounds = new Rect();
            paint.setColor(getResources().getColor(R.color.colorAccent));
            paint.setStyle(Paint.Style.FILL);
            paint.setTypeface(Typeface.DEFAULT);
            paint.setTextAlign(Paint.Align.CENTER);
            paint.setTextSize(30);
        }

        @Override
        protected void onDraw(Canvas canvas){
            super.onDraw(canvas);
            String text = "Hello World";
            paint.getTextBounds(text, 0, text.length(), bounds);
            int x = (canvas.getWidth()/2);
            int y = (int) ((canvas.getHeight()/2));
            int text_height = bounds.height();
            int text_width = bounds.width();
            canvas.drawText(text, x, y, paint);
            Toast.makeText(getContext(),"Height: "+String.valueOf(text_height)+" Width: "+String.valueOf(text_width),Toast.LENGTH_LONG).show();
        }
    }
}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.