Search code examples
javaandroidgoogle-maps-android-api-2google-maps-api-2

Resizing 'Google maps V2' to take half of the screen instead of whole screen


I'm working on an Android project right now: what I want to do is having the top half of the screen for the map and the other half for informations that I'd write later on.

My problem is that the map takes the whole screen, I tried different layouts (Relative, Grid) and inflating another layout but the map always takes the whole screen whatever I used.

here's my code:

public class Map extends SherlockMapFragment implements IPage, View.OnClickListener {

private GoogleMap    googleMap;
private Marker       marker;
ViewGroup            view;


public Map() {
    super();
}
    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    view = (ViewGroup) inflater.inflate(R.layout.map_page, null);

    googleMap = getMap();
    googleMap.setMyLocationEnabled(true);
    marker = googleMap.addMarker(new MarkerOptions().title("Event").position(new LatLng(0, 0)));

    final LatLng latLng = new LatLng(48.8146, 2.39525);      

    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16.0f));
    marker.setPosition(latLng);

    return view;
}

and the xml:

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

 <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_weight="1.0"
   android:orientation="vertical" > 

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

 <LinearLayout
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:layout_weight="1.0"
   android:orientation="vertical" >

   <TextView 
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:id="@+id/infotext">          
   </TextView>

 </LinearLayout>
</LinearLayout>

Once again, the map works without any issue but it takes the whole screen, I really cannot understand why. Also, I'm using the lib SherlockBar (which cause me some issues to implement the map).

I'd greatly appreciate any kind of help, I'm still kind of new with developing on Java/eclipse so mistakes often happen. Thanks!


Solution

  • If you want to have your screen split into two equals parts, you just have to use the following layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <fragment
            android:id="@+id/fragment_map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="50" />
    
        <TextView 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="50"
            android:id="@+id/infotext" />          
    
    </LinearLayout>
    

    As you only have two views, you don't have to wrap them into others LinearLayouts.

    Setting the same weight for the Fragment and the TextView is the key. It will give half of the screen (in height as the orientation of the LinearLayout is vertical) for each View.