I have a class called Game that extends View. I have an xml called game.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/game_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
As you can see all it is is a LinearLayout. Now what i want to do is make my game view show up in the LinearLayout. Heres the relevent code in Game:
public Game(Activity activ)
{
activ.setContentView(R.layout.game);
...
layout = (LinearLayout)activ.findViewById(R.id.game_layout);
layout.addView(this);
...
}
public void onDraw(Canvas canvas)
{
board.draw(canvas);
}
now when i run this it is just a blank screen. What am i doing wrong? The onDraw method is fine because when i did this it worked fine:
public Game(Activity activ)
{
activ.setContentView(this);
}
public void onDraw(Canvas canvas)
{
board.draw(canvas);
}
but the reason why i cant do it like that anymore is because on tablets there is a bar at the bottom where the back button and the show keyboard buttons are. When i do it like this it draws under that bar and it cuts off images. If i do it like this then Android sets the bounds of the layout so that it isnt under that bar. I would prefer to do it with the xml layout anyway because I think Android works better when you use the xml layouts instead of setting your custom view to the content view. All relevent answers are appriciated!
I think you are not able to see your view because you havent sepecified any layout params.
Try this before you call addView method.
The xml way of doing this would be.
Inside you xml file you can include your custom view like