Vincent
Vincent

Reputation: 6188

Android Layout priority

I'm trying to divide a screen into 2 sections. The above section is a map widget. Below is an other layout filled with widgets. The problem is that the map widget fills the whole screen. How can I make the layout below the map push te map up?

I'm guessing it could be solved by giving the map widget a height, but that's not very flexible.

My attemp:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">



    <com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:clickable="true" 
        android:apiKey="my api key" />





    <RelativeLayout
        android:id="@+id/map_menu" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_below="@id/mapview"
        android:background="@drawable/locatie_background" >


        <Button android:id="@+id/firstButtonId" android:layout_width="wrap_content" 
            android:layout_height="wrap_content" android:text="@string/action_ready" 
            />

    </RelativeLayout >

</RelativeLayout>

Upvotes: 5

Views: 10269

Answers (3)

Octavian Helm
Octavian Helm

Reputation: 39603

Go with a LinearLayout, set the layout_height of both children to wrap_content and set the layout_weight of both to 1.

This would divide the screens height equally:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:clickable="true"
        android:apiKey="my api key" />
    <RelativeLayout
        android:id="@+id/map_menu" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_below="@id/mapview"
        android:background="@drawable/locatie_background">
        <Button android:id="@+id/firstButtonId"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:text="@string/action_ready" />
    </RelativeLayout >
</LinearLayout>

You can work with layout_weight as if they would be percent values like here:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <com.google.android.maps.MapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="90"
        android:clickable="true"
        android:apiKey="my api key" />
    <RelativeLayout
        android:id="@+id/map_menu" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:layout_below="@id/mapview"
        android:background="@drawable/locatie_background">
        <Button android:id="@+id/firstButtonId"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:text="@string/action_ready" />
    </RelativeLayout >
</LinearLayout>

Upvotes: 4

Kartik Domadiya
Kartik Domadiya

Reputation: 29968

Instead of RelativeLayout use LinearLayout like this :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:orientation="vertical">


<LinearLayout
    android:id="@+id/map_menu1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:layout_weight="1"
    >

    <com.google.android.maps.MapView
    android:id="@+id/mapview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:clickable="true"
    android:apiKey="my api key" />
</LinearLayout >

<LinearLayout
    android:id="@+id/map_menu" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:layout_weight="1"
    >

    <Button android:id="@+id/firstButtonId" android:layout_width="fill_parent" 
        android:layout_height="wrap_content" android:text="action_ready" 
        />

</LinearLayout >

</LinearLayout>

Upvotes: 1

Uday
Uday

Reputation: 6023

replace your code with this :

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:orientation="vertical">



<com.google.android.maps.MapView
    android:id="@+id/mapview"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:clickable="true" 
    android:apiKey="my api key" />





<RelativeLayout
    android:id="@+id/map_menu" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_below="@id/mapview"
    android:background="@drawable/locatie_background" >


    <Button android:id="@+id/firstButtonId" android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:text="@string/action_ready" 
        />

</RelativeLayout >

</LinearLayout>

Upvotes: 1

Related Questions