Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7834575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:14:48+00:00 2026-06-02T13:14:48+00:00

I have been researching and struggling with my code to get a textview to

  • 0

I have been researching and struggling with my code to get a textview to update the score when a “ghost” is touched.

This is my first Android application and so far as i can tell(i added Log.w() to the ontouch but a log is never posted as well as the score not being incremented or even appearing) the onTouch(View v, MotionEvent event) is never called. Below is my code.

package com.cs461.Ian;

//TODO:Add accelerometer support
//TODO:Add Clickable ghosts
//TODO:Add camera background
/*Completed:(As of 2:30am 4/16)
 * Activity launches
 * Ghost appears on screen
 * Ghost will randomly move around the screen
 */

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.TextView;

public class CameraActivity extends Activity{

Bitmap g;
Ghost a;
Ghost still;
SurfaceHolder mSurfaceHolder;
boolean firsttime=true;
int draw_x,draw_y,xSpeed,ySpeed,score=0;
TextView t;




@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);
    a = new Ghost(getApplicationContext());
    still = new Ghost(getApplicationContext());
    //requestWindowFeature(Window.FEATURE_NO_TITLE);
    a.Initalize(BitmapFactory.decodeResource(getResources(), R.drawable.ghost), 20, 20);
    still.Initalize(BitmapFactory.decodeResource(getResources(), R.drawable.ghost), 120, 120);
    t = (TextView) findViewById(R.id.t);
    t.setText("TEST SCORE TO SEE IF TEXTVIEW SHOWS UP");

    a.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN){
                score++;
                t.setText("Score: "+score);

            }
            return true;
        }
    });
    still.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN){
                score++;
                t.setText("Score: "+score);
                Log.w("CLICKED","Clicked on ghost "+score+" times");

            }
            return true;
        }
    });



    setContentView(new Panel(this));
}


class Panel extends View {
    public Panel(Context context) {
        super(context);
    }

    @Override
    public void onDraw(Canvas canvas) {
        //g.Initalise(BitmapFactory.decodeFile("/res/drawable-hdpi/ghost.png"), 200, 150, 5, 5);;
        update(canvas);
        invalidate();
    }

    /*Places ghost on screen and bounces it around in the screen. My phone is apparently only API level 4(the most up to date is 15) so i didn't code it
     *for the accelerometer yet.
     */
    public void update(Canvas canvas) {
        Random rand = new Random();
        if(firsttime){
            draw_x = Math.round(System.currentTimeMillis() % (this.getWidth()*2)) ;
            draw_y = Math.round(System.currentTimeMillis() % (this.getHeight()*2)) ;
            xSpeed = rand.nextInt(10);
            ySpeed = rand.nextInt(10);

            firsttime=false;
        }
         draw_x+=xSpeed;
         draw_y+=ySpeed;
        draw_x = Math.round(System.currentTimeMillis() % (this.getWidth()*2)) ;
        draw_y = Math.round(System.currentTimeMillis() % (this.getHeight()*2)) ;
         if (draw_x>this.getWidth()){
          draw_x = (this.getWidth()*2)-draw_x;
          xSpeed = rand.nextInt(10);
          if(xSpeed >=5)
              xSpeed=-xSpeed;
         }
         if (draw_y>this.getHeight()){
          draw_y = (this.getHeight()*2)-draw_y;
          ySpeed = rand.nextInt(10);
          if(ySpeed >=5)
              ySpeed=-ySpeed;
         }
         g = BitmapFactory.decodeResource(getResources(), R.drawable.ghost);
         canvas.drawBitmap(g, draw_x, draw_y, null);
         still.draw(canvas);
         a.update(canvas);


        }
    }


}

Forgot to add class Ghost:

package com.cs461.Ian;

import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class Ghost extends View implements View.OnTouchListener{
public Ghost(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}

private Bitmap mAnimation;
private int mXPos;
private int mYPos;
private Rect mSRectangle;
private int mSpriteHeight;
private int mSpriteWidth;
View v;
Rect dest;
int score = 0;

/*public Ghost() {
    mSRectangle = new Rect(0,0,0,0);
    mXPos = 80;
    mYPos = 200;
}*/



public void Initalize(Bitmap theBitmap, int Height, int Width) {
    mSRectangle = new Rect(0,0,0,0);
    mXPos = 80;
    mYPos = 200;
    mAnimation = theBitmap;
    mSpriteHeight = Height;
    mSpriteWidth = Width;
    mSRectangle.top = 0;
    mSRectangle.bottom = mSpriteHeight;
    mSRectangle.left = 0;
    mSRectangle.right = mSpriteWidth;
    dest = new Rect(mXPos, mYPos, mXPos + mSpriteWidth,
            mYPos + mSpriteHeight);

}
public void draw(Canvas canvas) {


    canvas.drawBitmap(mAnimation, mXPos, mYPos, null);
}

public void update(Canvas canvas) {
    new Random();

    mXPos = Math.round(System.currentTimeMillis() % (canvas.getWidth()*2)) ;
    mYPos = Math.round(System.currentTimeMillis() % (canvas.getHeight()*2)) ;
    if (mXPos>canvas.getWidth())
      mXPos = (canvas.getWidth()*2)-mXPos;
    if (mYPos>canvas.getHeight())
      mYPos = (canvas.getHeight()*2)-mYPos;

     draw(canvas);


}

public Rect getRect() {
    return mSRectangle;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
    score++;
    //CameraActivity.t.setText("Score: "+CameraActivity.score);
    Log.w("CLICKED","Clicked on ghost "+score+" times");
    return true; //doesn't work if returns false either
}


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:14:52+00:00Added an answer on June 2, 2026 at 1:14 pm

    Override dispatchTouchEvent(MotionEvent event) in your Activity to handle all the touch events that may occur. Return false to pass down the MotionEvent to the next view\layout in the hierachy if they they handle events.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so I have been researching this all morning- and I'm pretty sure the code
I have been researching the Samsung Galaxy Prevail for Android development. This phone is
I have been researching options for printing report-like data via a web application. Some
I have been researching how to get Cygwin to work under emacs . I
I have been researching this for a few hours and I feel that I
Okay I am newer to python and have been researching this problem but I
I am a JavaScript learner and have been researching this matter, but with no
I've been researching a solution to this all week and while there have been
I am beyond frustrated. I have been researching an answer to this for hours
This is something I have been struggling about for some time now. The thing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.