-1

I want to have a MapView taking all the space above a persistent bottom sheet (NestedScrollView) as I already have and you see on the image below.

Example of a NestedScrollView with BottomSheetBehavior combined with map

For the MapView I'm using android:layout_height="238dp". I have tried to replace it for android:layout_height="match_parent" but then the height of the MapView was stretched taking up the entire screen. Here's is the code, as you see it on the image above.

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:mapbox="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="beforeDescendants"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".SpotFormActivity"
    tools:showIn="@layout/spot_form_master_layout">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/save_spot_form_basic"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <com.mapbox.mapboxsdk.maps.MapView
                android:id="@+id/mapview2"
                android:layout_width="match_parent"
                **android:layout_height="238dp"** />

        </androidx.coordinatorlayout.widget.CoordinatorLayout>

        <androidx.core.widget.NestedScrollView
            android:id="@+id/spot_form_scrollview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FFF"
            android:paddingBottom="60dp"
     app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
           ....
        </androidx.core.widget.NestedScrollView>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

I don't want to use android:layout_height="238dp" because in devices with bigger screen the map height doesn't get automatically adjusted. Does anyone has a suggestion of a better way to do this?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
leoneboaventura
  • 415
  • 1
  • 3
  • 13

1 Answers1

1

this what i use most of the times for changing items height and it suites to different devices
first get the height of the device :

public static int getDaviceHeight(Activity activity){
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        height=displayMetrics.heightPixels;
        return height;

    }

then change the items height:

MapView mapView = (MapView) v.findViewById(R.id.mapView2);
        mapView.getLayoutParams().height=(getDaviceHeight(this))/3;
        //ofcourse you can change the number and devide the total height as much as you want