views:

67

answers:

2

Trying to write a game such that most of the screen gets filled with my GameView (custom view, derived from View)

I then want to have an area at the bottom of the screen for messages etc. In the following I'm just trying to put a button there to illustrate the issue.

<?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">

    <my.GameView 
        android:id="@+id/gameview" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" />

    <Button 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Action1" />

</LinearLayout>

When this runs the button never shows. If I move the button before the GameView then it works with the button at the top of the screen. As it is above GameView is somehow grabbing all the screen. Tried also giving GameView a layout_weight of 1 with button having 0 (and vice-versa)

Do I have to implement the onMeasure stuff in GameView (which I couldn't quite get my head round yet) or am I missing something?

+2  A: 

If the size of your GameView is as big as the screen, your Button won't show, because with "wrap_content" you give your GameView the permission to take as much of the LinearLayout as it needs.

Try it something like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<Button
    android:id="+@id/myButton"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"/>
<my.GameView 
    android:id="@+id/gameview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_above="@id/myButton"/>
</RelativeLayout>

Hope this helps

qedejavu
Thanks - that works :)Still think it should be possible with LinearLayout if I could just tell it to give GameView the remaining space when all other views have been placed (or at least get GameView to say it was happy to just fill whatever space it was given)
MrPurpleStreak
+1  A: 

With the LinearLayout, just give a layout_weight of 1 to the GameView, don't add any weight to the button. That should work.

If it still doesn't work, then also use 0px for the GameView layout_height, eg:

<my.GameView 
    android:id="@+id/gameview" 
    android:layout_width="fill_parent" 
    android:layout_height="0px" 
    android:layout_weight="1"/>
Olivier
Adding the layout_weight 1 does it. I really really thought I'd tried that already.The odd thing now is that when it starts up I turn the FULL SCREEN stuff on, but as the header bar (with the wi-fi status icons etc.) scrolls up it screws the layout, so the whole thing is a few pixels down. It will have a menu screen first which will fix that though (i hope)
MrPurpleStreak
Maybe that it first didn't work because you also added a layout_weight 0 to the button, I'm unsure. What you report about fullscreen is odd, maybe that you should search or ask another question about that.
Olivier
A bit more googling found a solution to that.. (from http://groups.google.com/group/android-developers/browse_thread/thread/2de77043f32835aa/315d393bb8334db5)After setting full screen flags set the following: getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
MrPurpleStreak