Canvas doesn't draw text?


code M

I want to display some text on the screen when the user clicks buttonwith canvasin android studio. I've been watching a lot of posts and videos on how to do this. The problem is, when I try their code, when I click the button, nothing is displayed. Does anyone know what I am doing wrong?

Here is my Javaclass:

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button drawCanvas = findViewById(R.id.draw_canvas_button);

    drawCanvas.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Paint paint = new Paint();
            Canvas canvas = new Canvas();
            paint.setColor(Color.BLACK);
            paint.setStyle(Paint.Style.FILL);
            paint.setTextSize(20f);
            canvas.drawText("My Text", 10f, 15f, paint);

        }
    });
}
}
code M

It seems you have to use the method called onDrawvia importing View.

import android.view.View;

public class myClass extends View{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


}
@Override
protected void onDraw(Canvas canvas){
    super.onDraw(canvas);
    //Where you put the code
}

Related


Canvas doesn't draw in a loop

Saba Silagadze Why draw arent rectangles in a loop? I just started a snake game n trying to draw the snake's body through a loop, but for some reason ctx is not drawing the loop. If I write the rectangle without the loop, it works. var canv = document.getEleme

Canvas doesn't draw in a loop

Saba Silagadze Why draw arent rectangles in a loop? I just started a snake game n trying to draw the snake's body through a loop, but for some reason ctx doesn't draw the loop. If I write the rectangle without the loop, it works. var canv = document.getElement

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw anything? Kotlin

David Wenzel When I write this code it doesn't work: if(shapeSpinner?.selectedItemPosition == 0){ canvas.drawCircle((0..dimensionX).random().toFloat(), (0..dimensionY).random().toFloat(), (0..dimensionX).random().toFloat(), paint) } if(shapeSpin

Canvas doesn't draw IE 11

Barr I have a canvas code to draw the signature. The code works fine on chrome and Firefox, but doesn't draw at all on IE 11. My canvas is: <canvas id="signitureCanvas" style="border: 3px solid #000; cursor:crosshair; background-color:white;"></canvas> My cod

Canvas.drawRect() doesn't draw anything

Cody My painter in the Canvas class doesn't draw the rectangle, although I configure it to do so. I have a simple canvas layout, I have set up 6 rectangle painters that should draw a grey rectangle if the corresponding boolean flag is true. Still, for some rea

Canvas doesn't draw anything? Kotlin

David Wenzel When I write this code it doesn't work: if(shapeSpinner?.selectedItemPosition == 0){ canvas.drawCircle((0..dimensionX).random().toFloat(), (0..dimensionY).random().toFloat(), (0..dimensionX).random().toFloat(), paint) } if(shapeSpin

canvas.drawBitmap doesn't draw anything

Eye I have covered onDragShadowfor DragShadowBuilderthis @Override public void onDrawShadow(Canvas canvas) { super.onDrawShadow(canvas); Bitmap bitmap = InGameActivity.getRandomBitmap(); Rect source = new Rect(0, 0, bitmap.getWidth(

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Script doesn't draw lines by coordinates on Canvas

Anton I'm trying to use Canvas to let users draw them on a PC or smartphone. I don't understand why this script doesn't work. The script doesn't give any errors. The coordinates are set correctly. I tried setting it manually, but even then nothing appears on t

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

Canvas doesn't draw rectangle after for loop

Digital Brent I'm trying to create a news ticker that renders text in small square "pixels". I say "pixels" because they look like pixels, but the actual displayed square is larger than 1px. So far I can get all the rendered letters from the object I build (co

ffmpeg doesn't draw text

Michael Heuberger Hopefully one of you can tell me why my ffmpeg command doesn't draw the desired text. The video made does not have this video. Well done: ffmpeg -f image2 -thread_queue_size 64 -framerate 15.1 -i /home/michael-heuberger/binarykitchen/code/vid

Canvas draw text - can't get string

Italy String str = new String(); str = "111"; x = width / 2; y = Math.round(height / 1.3); while (pressed != true){ if (!holder.getSurface().isValid()) continue; Canvas c = holder.lockCanvas(); c.drawBitmap(galaxy, 0, 0, null); c.draw

Canvas draw text - can't get string

Italy String str = new String(); str = "111"; x = width / 2; y = Math.round(height / 1.3); while (pressed != true){ if (!holder.getSurface().isValid()) continue; Canvas c = holder.lockCanvas(); c.drawBitmap(galaxy, 0, 0, null); c.draw

Canvas doesn't show text

CIsForCookies My canvas shows a rectangular grid of randomly changing colors, but when I want it to end, it doesn't show the "Goodbye!" message I added to the canvas. why is that? Even if I comment the line for i in ...inside __init__(ie the for loop that crea

Draw multiline text on canvas

Paul Managa A hopefully quick question, but I can't seem to find any examples... I want to write a custom Viewpass in multiline text Canvas, and where onDraw()I have: ... String text = "This is\nmulti-line\ntext"; canvas.drawText(text, 100, 100, mTextPaint); .

draw text in canvas element

Ahmed Can you tell me what's wrong with the text on the canvas? I tried changing the text size, if I make it bigger it looks fine, but if I make it smaller it looks weird. I'm not sure what exactly is affecting the text; I've tried using the font property with

Draw multiline text on canvas

Paul Managa A hopefully quick question, but I can't seem to find any examples... I want to write a custom Viewpass in multiline text Canvas, and where onDraw()I have: ... String text = "This is\nmulti-line\ntext"; canvas.drawText(text, 100, 100, mTextPaint); .

Draw multiline text on canvas

Paul Managa A hopefully quick question, but I can't seem to find any examples... I want to write a custom Viewpass in multiline text Canvas, and where onDraw()I have: ... String text = "This is\nmulti-line\ntext"; canvas.drawText(text, 100, 100, mTextPaint); .

Canvas drawing doesn't draw correctly despite setting properties

macaron lover I just created 2 drawings, trying to make one look the same as the other, but I have no luck. Regarding the screenshots, the graph at the top was created using canvas, while the graph at the bottom was created using XML. XML drawing works perfect

Canvas drawing doesn't draw correctly despite setting properties

macaron lover I just created 2 drawings, trying to make one look the same as the other, but I have no luck. Regarding the screenshots, the graph at the top was created using canvas, while the graph at the bottom was created using XML. XML drawing works perfect

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon

React-Native-Canvas doesn't draw whole rectangle

Brian Ton While making another app, I ran into a problem react-native-canvas, so I created this minimal project. The problem is that the WebView is much smaller than the Canvas and doesn't fill the entire canvas when appropriate. Here is the code (very similar

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon

canvas.drawRect doesn't draw a rectangle on the image

Krishna Here canvas.drawRect will be different when using different bitmaps. I want to draw a rectangle over the top image and want the part of the image outside the rectangle to be darkened or blurred. please help me. draw.setOnClickListener(new View.OnClickL

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon

Canvas drawing doesn't draw correctly despite setting properties

macaron lover I just created 2 drawings, trying to make one look the same as the other, but I have no luck. Regarding the screenshots, the graph at the top was created using canvas, while the graph at the bottom was created using XML. XML drawing works perfect

Why doesn't HTML5 drawImage draw image on canvas?

humble 7 I tried adding an eventListener to draw the image only after the image is loaded. I've also tried putting event listeners before and after setting the image src, but still nothing seems to work. I can't seem to figure out what's causing it. Can someon