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 8940407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:01:41+00:00 2026-06-15T11:01:41+00:00

I am trying to make a simple 2D Android game. In this, game I’m

  • 0

I am trying to make a simple 2D Android game. In this, game I’m trying to make some buttons to control the character, while the character is shown in a SurfaceView. Currently, the SurfaceView is rendering only a black screen.

This is my MainActivity, which extends Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    GameView gm = (GameView)findViewById(R.id.snake_surface);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.layout.main, menu);
    return true;
}

The GameView class, which extends SurfaceView:

   private HeroSprite hero;
   public GameView(Context context, AttributeSet attributeSet) {

         super(context, attributeSet);

         gameLoopThread = new GameLoopThread(this);

         getHolder().addCallback(new SurfaceHolder.Callback() {

                public void surfaceDestroyed(SurfaceHolder holder) {
                       boolean retry = true;
                       gameLoopThread.setRunning(false);
                       while (retry) {
                              try {
                                    gameLoopThread.join();
                                    retry = false;
                              } catch (InterruptedException e) {}
                       }
                }

                public void surfaceCreated(SurfaceHolder holder) {
                       createSprites();
                       createHero();
                       gameLoopThread.setRunning(true);
                       gameLoopThread.start();
                }


                public void surfaceChanged(SurfaceHolder holder, int format,int width, int height) {
                }
         });
         bmpBlood = BitmapFactory.decodeResource(context.getResources(), R.drawable.blood1);
   }


      private void createHero(){
       hero=(createHeroSprite(R.drawable.aslan));

   }
      private HeroSprite createHeroSprite(int resouce) {
          Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
          return new HeroSprite(this, bmp);
    }
   private void createSprites() {
         sprites.add(createSprite(R.drawable.tavsan));
         sprites.add(createSprite(R.drawable.fare));
         sprites.add(createSprite(R.drawable.sansar));
         sprites.add(createSprite(R.drawable.tavsan));
         sprites.add(createSprite(R.drawable.fare));
         sprites.add(createSprite(R.drawable.sansar));
         sprites.add(createSprite(R.drawable.tavsan));
         sprites.add(createSprite(R.drawable.fare));
         sprites.add(createSprite(R.drawable.sansar));
         sprites.add(createSprite(R.drawable.tavsan));
         sprites.add(createSprite(R.drawable.tavsan));

   }

   private Sprite createSprite(int resouce) {
         Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
         return new Sprite(this, bmp);
   }

   @Override
   protected void onDraw(Canvas canvas) {
         canvas.drawColor(Color.GREEN);
         for (int i = temps.size() - 1; i >= 0; i--) {
                temps.get(i).onDraw(canvas);
         }
         for (Sprite sprite : sprites) {
                sprite.onDraw(canvas);
         }
         hero.onDraw(canvas);
   }

   @Override
   public boolean onTouchEvent(MotionEvent event) {
         if (System.currentTimeMillis() - lastClick > 300) {
                lastClick = System.currentTimeMillis();
                float x = event.getX();
                float y = event.getY();
                synchronized (getHolder()) {
                       for (int i = sprites.size() - 1; i >= 0; i--) {
                              Sprite sprite = sprites.get(i);
                              if ((sprite).isCollition(x, y)) {
                                    sprites.remove(sprite);
                                    temps.add(new TempSprite(temps, this, x, y, bmpBlood));
                                    break;
                              }
                       }
                }
         }
         return true;
   }

My GameLoopThread class, which extends Thread

static final long FPS = 10;
private GameView view;
private boolean running = false;

public GameLoopThread(GameView view) {
    this.view = view;
}

public void setRunning(boolean run) {
    running = run;
}

@Override
public void run() {
    long ticksPS = 1000 / FPS;
    long startTime;
    long sleepTime;
    while (running) {
        Canvas c = null;
        startTime = System.currentTimeMillis();
        try {
            c = view.getHolder().lockCanvas();
            synchronized (view.getHolder()) {
                view.onDraw(c);
            }
        } finally {
            if (c != null) {
                view.getHolder().unlockCanvasAndPost(c);
            }
        }
        sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
        try {
            if (sleepTime > 0)
                sleep(sleepTime);
            else
                sleep(10);
        } catch (Exception e) {
        }
    }
}

Here is my layout:

<LinearLayout  
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/score"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:text="Score:"
            android:textSize="20dp" />

        <Button
            android:id="@+id/btnThread"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal" />

        <TextView
            android:id="@+id/max"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:layout_marginLeft="100dp"
            android:text="Max:"
            android:textSize="20dp" />
    </LinearLayout>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.3" >

            <SurfaceView
                android:id="@+id/snake_surface"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="0.3" />

        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:weightSum="1" >

            <Button
                android:id="@+id/butUp"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center"
                android:text="UP" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <Button
                    android:id="@+id/butLeft"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:text="LEFT" />

                <Button
                    android:id="@+id/butRight"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:text="RIGHT" />
            </RelativeLayout>

            <Button
                android:id="@+id/butDown"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|center"
                android:text="DOWN" />
        </LinearLayout>

     </LinearLayout>

</LinearLayout>
  • 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-15T11:01:42+00:00Added an answer on June 15, 2026 at 11:01 am

    Your view in the layout is a SurfaceView, not a GameView. You need to change that:

            <your.package.name.GameView
                android:id="@+id/snake_surface"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="0.3" />
    

    And replace your.package.name with the actual name of the package containing GameView.

    Also, the line GameView gm = ... needs to go after setContentView.

    And lastly, don’t forget to call gm.invalidate() when you’ve modified the items that will be drawn!

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

Sidebar

Related Questions

I'm, trying to make a simple 3D game for Android, and I want to
Trying to make a simple number clicker control for BlackBerry 6/7, like this: At
I'm trying to make a simple breakout game with opengl es, on Android, and
I am trying to make simple notepad application for learning android development. So, to
Trying to make simple minesweeper game in python, but have one problem. I have
Trying to make a simple program to catalogue books. Something like this, for example:
Trying to make a simple program to catalogue books. Something like this, for example:
I'm trying to make a simple tasklist in Android using Eclipse. The layout i'm
I'm new at android developing. I'm trying to make a simple drumset app, when
i'm new to android. I'm just trying to make one simple search functionality for

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.