New Tutorial 2022
View All Tutorials
Android - Canvas Paint Example
Added April 2022
For this tutorial, we are going to learn about
Canvas Paint, and create an app which shows the basics of implementing a canvas onto your screen.
We are going to
create a new app for this tutorial,
and name is -
Canvas Paint
Creating Our New App
Open AIDE, Select New Project, from the left menu, or center menu hierachy of files; and do the following:
Select
choose NEW ANDROID APP(Gradle/Android SDK/java/xml) if your using the newest version of AIDE.
Choose HELLO WORLD example app template(java/xml);if your using an older version of AIDE,
For app name; type 'Canvas Paint'
For app package name type:
com.aac.canvaspaint_example
Click Create
Your new app is now created for you - named 'Canvas Paint'. For now, you will have the 'little green Android image' as your app icon.
Once you add your apps' icon image and run your app with the updated and new pages you create, and the apps' other images; you'll see the customized 'App Icon' instead of little green Android icon. Note: Not all tutorials have a customized icon or require new pages to be created.
AIDE automatically opens your new app in the editor for you.
Where Code is Coded
For this tutorial
, we must edit the
main.xml, and edit the
MainActivity.java, aka, our java class. And, add our app icon image to the res/drawable-hdpi folder. You can use any image you want for the apps' icon. For this app I took a screenshot of the app homepage and used that as the app icon.
First we add our LinearLayout to the layout at main.xml; and add code as shown in this image.
All codes for this tutorial are at the Example Code section on this page.
Next we edit our java page;
MainActivity.java.
In our java page; we add all the code to create the canvas. Then once we run our code the canvas paint is shown on the app homepage; aka main.xml. The code has notations to describe what each does. Basically we choose which shapes to add, include the code for them, and then add the colors by adding the color numbers.
We edit our java page and add the code as shown in this image.
Image of Code at MainActivity.java
We must also add the proper
'import statements' as shown in this next image. If we do not, or add the wrong ones, the app won't function, and you get error messages when you Run the app code.

Take note, we must add import statements for Bitmap, Paint, Color, Canvas, and Context.
In java code, we first must create and name variable; 'Demoview', then we attach it to our 'SetContentView' method.
All code for this tutorial is at the Example Code section on this page.
Only copy and replace code for the pages mentioned that require updating.
At Example Code section on this page; any page notated as 'no edit required', means you don't have to copy and paste that page - added only for you to view it.
TIPS
Navigating The App Project Hierachy
To view A FILE do either: from right menu choose FILES or GOTO, this will show the apps files along the top of editor, dropdown, and left
side menu. After editing a file, SAVE it, right side menu, choose SAVE.
Creating Pages
Check for the filename extensions of page your creating. If for example, your creating a new Java page, (java folder)you want to see pages already created in that folder that have the .java file extension. Click 'create new java class' to create your page.
For xml files, you must create these in the 'res/layout' folder of your app project. Click 'add new xml file', to create your page.
Additional Reading on Canvas Paint, at Android Developer website
Graphics - Canvas Paint
- located at AppProjects/CanvasPaint in AIDE; OPEN the AIDE app, then open App Projects, THEN select the 'Canvas Paint',
app from your files hierachy(left menu); then DOUBLE CLICK on any file from the app, like strings.xml, to open it in AIDE.
Make sure at top of page, the app name 'CanvasPaint' is there next to
AppProjects like so:
then you know you have the proper App.
Once a file is in the editor, you can edit, save it, then RUN your APP, install, to
Open your app.
You can open the app Simply by Clicking on the Android little green App Icon(or included app icon at Example Code),with the wording 'Canvas Paint' on your device. It will
be with your other installed apps.
If you make further changes to this App, you need to SAVE, RUN, UPDATE, INSTALL, OPEN the app again.