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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T08:49:31+00:00 2026-06-06T08:49:31+00:00

I have an XML layout and want to add my own custom SurfaceView to

  • 0

I have an XML layout and want to add my own custom SurfaceView to it and draw on its canvas. When I debug, the canvas runs its onDraw and gets to unlockandpost function, but its always just a black screen. Here is my code. I’ve been trying to figure it out for hours and have tried multiple alternatives such as just adding my custom SurfaceView into the XML instead of adding it dynamically, but I got the same results.

Any help would be appreciated,

Thank you.

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
>
<ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/guitar_neck">
</ImageView>
<RelativeLayout
        android:id="@+id/letter_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
/>

 public class BeginGame extends Activity implements OnClickListener {
public RelativeLayout game_screen;
public LetterBar theBar;
public SurfaceHolder holder;
public static int width, height;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        theBar = new LetterBar(this);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Display display = getWindowManager().getDefaultDisplay(); 
    width = display.getWidth();  // deprecated
    height = display.getHeight();  // deprecated

    setContentView(R.layout.game_menu);
    game_screen = (RelativeLayout)findViewById(R.id.letter_bar);
    game_screen.addView(theBar);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

}

 public class LetterBar extends SurfaceView implements SurfaceHolder.Callback {
private int width, height, toneWidth, toneHeight;
private Paint backgroundPaint, paint;

private Resources res;
private Drawable myImage;
private Bitmap neck, b;

private LetterThread _thread;

public LetterBar(Context context) throws IOException {
    super(context);
    // TODO Auto-generated constructor stub

    getHolder().addCallback(this);
    _thread = new LetterThread(getHolder(), this);

    paint = new Paint();
    paint.setColor(0xFFFFFF);
    backgroundPaint = new Paint();
    backgroundPaint.setARGB(0, 255, 255, 255);

    res = context.getResources();

    neck = BitmapFactory.decodeFile("/assets/guitar_neck.gif");

    InputStream bitmap=null;
    b = BitmapFactory.decodeResource(context.getResources(), R.drawable.guitar_neck);
    toneWidth = b.getWidth();
    toneHeight = b.getHeight();
}

@Override
public void onDraw(Canvas canvas){
    canvas.drawText("TEST", 20, 20, paint);
    canvas.drawBitmap(b, 0, 0, paint);
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
    // TODO Auto-generated method stub
    setWillNotDraw(false);
    _thread.setRunning(true);
    _thread.start();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    // simply copied from sample application LunarLander:
    // we have to tell thread to shut down & wait for it to finish, or else
    // it might touch the Surface after we return and explode
    boolean retry = true;
    _thread.setRunning(false);
    while (retry) {
        try {
            _thread.join();
            retry = false;
        } catch (InterruptedException e) {
            // we will try it again and again...
        }
    }
}
 }


 public class LetterThread extends Thread {
private SurfaceHolder _surfaceHolder;
private LetterBar letter_bar;
private boolean _run = false;

public LetterThread(SurfaceHolder surfaceHolder, LetterBar bar) {
    _surfaceHolder = surfaceHolder;
    letter_bar = bar;
}

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

@Override
public void run() {
    Canvas c;
    while (_run) {
        c = null;
        try {
            c = _surfaceHolder.lockCanvas(null);
            synchronized (_surfaceHolder) {
                letter_bar.onDraw(c);
            }
        } finally {
            // do this in a finally so that if an exception is thrown
            // during the above, we don't leave the Surface in an
            // inconsistent state
            if (c != null) {
                _surfaceHolder.unlockCanvasAndPost(c);
            }
        }
    }
}
 }
  • 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-06T08:49:33+00:00Added an answer on June 6, 2026 at 8:49 am

    Currently your paint and backgroundPaint both have alpha 0.

    Try this:

    paint = new Paint();
    paint.setColor(0xFFFFFFFF); // Added alpha bits out front to make it visible white
    backgroundPaint = new Paint();
    backgroundPaint.setARGB(255, 255, 255, 255); // 255 alpha bit, so it's visible black
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listView with custom objects defined by the xml-layout below. I want
I want to add a custom Dialog, but I have problems creating a xml
in my app i have activity with XML layout, i want to build an
I want to add a webview to my layout.But that layout have few other
I have created some xml layouts. Now I am creating a custom layout (in
i have customview, i want to add that in the xml file i tried
I have a vertical linear layout and I want to add a background the
New to android programming. I have a layout XML, to which I want to
I have an XML layout that defines a TextView box 50px x 320px who
I have an XML layout similar to this (the XML and code is a

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.