2

I'm working on a small game. However I want my activity to have the contentview of the XML, but also want it to show the view I created later.

This is my code:

public class GameView extends Activity{
MediaPlayer backgroundMusic;
TextView mTextField;
int Px = 0;
int Py = 0;
Point size = new Point();
Display display;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    //setContentView(new myView(this));
    setContentView(R.layout.gameviewlayout);
    mTextField = (TextView)findViewById(R.id.timer1);       
        new CountDownTimer(45000, 1000) {

            public void onTick(long millisUntilFinished) {
                mTextField.setText("Seconds Remaining: " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                mTextField.setText("done!");
            }
         }.start();
    display = getWindowManager().getDefaultDisplay();
    display.getSize(size);
    int width = size.x;
    int height = size.y;
    backgroundMusic = MediaPlayer.create(GameView.this, R.raw.gallery_music);
    backgroundMusic.start();

}
@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    backgroundMusic.release();

}

private class myView extends View{
    public myView(Context context) {
    super(context);

     }

    @SuppressLint("DrawAllocation")
    @Override
     protected void onDraw(Canvas canvas) {
      // TODO Auto-generated method stub
            Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.lil_peanut);
            canvas.drawBitmap(Bitmap.createScaledBitmap(bm, 83, 131, false), Px, Py, null);
        }
    }
}

so I want both setContentView(new myView(this)); and SetContentView(R.layout.gameviewlayout); How do I accomplish this?

bemeyer
  • 6,154
  • 4
  • 36
  • 86

1 Answers1

0

This question is oft-asked (there will likely be dupes). Have a look at this for a start- this is basically what you want.

As with the more crucial things, there are many ways of doing it. My preferred way is:

1.) Give your root views in your layout files ids. This is unnecessary, see here, but the SDK is designed to encourage that you use findViewById wherever possible, and it should (in theory) be faster.

2.) Your root views should be instances of ViewGroups, except in special circumstances.

3.) Once you've got a reference to the view, you can use the addView as detailed in the documentation- this adds the view as a "child" to the layout you've got.

4.) Android uses LayoutParameters to measure and draw views. So you'll need to set xxxLayout.LayoutParams(...) on your new view, the best design for me is to do it as you add it. But be warned! The view your adding to the layout needs to have the right layout parameters- replace xxx with "Linear" or "Relative". But if you're adding, say, a LinearLayout to a RelativeLayout you want to set relative layout params- because the measurer really needs to know how each view it "visits" relates to the container above. And remember, if you use import instead of more "qualified class names", you should import the right one. Getting mixed up with both gets me from time to time and I've been playing this game for a while.

Sadly, this wonderful question will be a dupe, so we should close it as such.

Community
  • 1
  • 1
Tom
  • 1,773
  • 15
  • 23
  • Hi Tom, as you might noticed I am a beginning programmer, and I have searched all day for similar terms as used in my original post. I had no knowledge of terms like addView or the root views. Sorry about that. Thanks for you help, I am trying this now. – Luuk van Aggelen Apr 23 '13 at 19:03
  • Oh no! This is still the kind of thing that surprises me from time to time. These important questions I have personally found the most helpful on SO in my experience! It would be lovely if one were allowed to ask the best questions many times, and having said that, I've yet to find an exact dupe. – Tom Apr 23 '13 at 19:13