0

I am new to android game programming and I can't seem to find a way to get the background image to stay behind the game, I don't have an xml file for this activity so I added the background in the java code but it is covering the whole game.

Thanks in advance

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _game = new Game(this);
     // _game.setBackgroundResource(R.drawable.background_ingame);
        setContentView(_game);

    }
ADM
  • 20,406
  • 11
  • 52
  • 83
Tony
  • 1
  • 1

1 Answers1

0

Game is View . If you callsetBackgroundResource on game object the it will set it to games background . And it will scale as per the size of Game view . If you want to set the background to screen and Game will be above layer . You can try something like this :-

 @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FrameLayout frameLayout=new FrameLayout(this);
    frameLayout.setBackgroundResource(R.drawable.background_ingame);
    game = new Game(this);
    frameLayout.addView(game);
    setContentView(frameLayout);
}
ADM
  • 20,406
  • 11
  • 52
  • 83
  • setBackgroundResource is deprecated, use setBackground – dogood Jan 29 '19 at 12:33
  • `setBackgroundDrawable` is deprecated not `setBackgroundResource`. Also i am not making a statement about setting background drawable here . There are multiple way to do this . – ADM Jan 29 '19 at 12:36
  • Cheers for the answers guys, this way didnt seem to work for me. I just tried https://stackoverflow.com/questions/5176441/drawable-image-on-a-canvas and now its working. Thank you! – Tony Jan 29 '19 at 13:10