Tutorial :Android view fills screen using linerlayout



Question:

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?


Solution:1

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


Solution:2

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


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »