Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to increment Canvas Text in Android App?
    primarykey
    data
    text
    <p>How's it going guys. What I'm trying to do right now is increment this canvas text I have on the screen right now. I have a bitmap on the canvas that should will increment the number every time it is clicked. I printed the variable to the logcat and it is indeed incrementing but it's not being drawn on the screen. </p> <p>Here's a picture of what I have now for better idea:</p> <p><img src="https://i.stack.imgur.com/mvUs1.png" alt="enter image description here"></p> <p>Here's my drawing class:</p> <pre><code>package com.example.touchperson; 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.Rect; import android.util.Log; import android.view.View; public class Drawing extends View { Bitmap robot; Rect rec; Paint text; static int touchCount = 0; public Drawing(Context context) { super(context); robot = BitmapFactory.decodeResource(getResources(), R.drawable.character); Log.i("Robot coord", Integer.toString(robot.getHeight()));; rec = new Rect(0, 0, 200 , 200); text = new Paint(); text.setColor(Color.CYAN); text.setTextSize(100); } public boolean contains(int x, int y){ if(rec.contains(x, y)){ return true; } else{ return false; } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.WHITE); canvas.drawBitmap(robot, rec, rec, null); canvas.drawText(Integer.toString(touchCount) ,(int) (canvas.getWidth()/2) , (int) (canvas.getHeight()/2), text); } } </code></pre> <p>Here's my main activity class:</p> <pre><code>package com.example.touchperson; import android.os.Bundle; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; public class MainActivity extends Activity implements OnTouchListener { Drawing view; Drawing count; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); view = new Drawing(this); view.setOnTouchListener(this); setContentView(view); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch(event.getAction()){ case MotionEvent.ACTION_DOWN: if(view.contains((int) event.getX(), (int) event.getY())){ Drawing.touchCount++; count = new Drawing(this); Log.i("touched", Integer.toString(Drawing.touchCount)); } break; } return false; } } </code></pre> <p>Any help would be much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload