Reputation: 33
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!
Upvotes: 2
Views: 7975
Reputation: 330
I know it's kind of late, but since I passed by to seek answer for my question, here are my thoughts:
It looks like your linear layout that holds MapFragment is taking up whole screen. Instead of having match_parent, change android:layout_height to fill_parent.
The difference here is that match_parent makes the height of the view to take ALL HEIGHT of the parent. In your case parent is the linear layout that is of the size of the screen.
fill_parent actually fills free space of the parent.
For your example the following XML should work:
<?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="fill_parent"
android:layout_weight="1" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1.0"
android:orientation="vertical"
android:weight="7">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/infotext">
</TextView>
</LinearLayout>
</LinearLayout>
Having wrap_content with weights allows you to do relative layouting. On my devices, following weights look like that (And you can play with those on your to achieve desired sizes)
|----------------|
| |
| |
| |
| |
| Map |
| |
| |
| |
| |
|----------------|
| Text |
|----------------|
Upvotes: 1
Reputation: 790
Use this one:-
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.model.LatLng;
import android.annotation.SuppressLint;
//import android.app.Fragment;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class Map_Check extends Fragment {
public Map_Check(){}
MapView mapView;
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.map_view, container, false);
// Gets the MapView from the XML layout and creates it
mapView = (MapView) v.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);
MapsInitializer.initialize(this.getActivity());
// Updates the location and zoom of the MapView
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(48.8146, 2.39525), 10);
map.animateCamera(cameraUpdate);
return v;
}
@Override
public void onResume() {
mapView.onResume();
super.onResume();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}
And map_view.xml:-
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="150dp">
<com.google.android.gms.maps.MapView
android:id="@+id/mapview"
android:layout_width="100dp"
android:layout_height="100dp"/>
</LinearLayout>
Upvotes: 0
Reputation: 1557
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 LinearLayout
s.
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
.
Upvotes: 9