Skip to content Skip to sidebar Skip to footer

Create Buttons Below Google Map

I want to add two aligned buttons below google map.I have tried adding buttons below but its not showing and whenever i try to change size of map,interface gets different for diffe

Solution 1:

If I guessed right your problem is that any Buttons below the map fragment (inside the main LinearLayout) are not visible. This can be solved by setting the map fragment's layout_height to 0dp and giving it a layout_weight of 1. The UI elements above and below the map fragment should have the layout_height set to wrap_content just like you have done.

The idea is that the other UI elements use just the amount of the screen height that they need and the map fragment will then use all the remaining space. A similar approach can commonly be used in all kinds of layouts that have some elements with fixed height/width and one element that is supposed to scale to an optimal height/width.

As XML:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"
    ><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"><EditTextandroid:layout_width="200dp"android:layout_height="wrap_content"android:id="@+id/etSearch"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="search"android:id="@+id/btnSearch"android:layout_gravity="end"style="?android:attr/buttonStyleSmall"android:onClick="onSearch" /></LinearLayout><fragmentxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:id="@+id/map"tools:context=".MapsActivity"android:name="com.google.android.gms.maps.SupportMapFragment" /><!--The Buttons below the map fragment--><LinearLayoutandroid:orientation="horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_horizontal"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="New Button"android:id="@+id/button" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="New Button"android:id="@+id/button2" /></LinearLayout></LinearLayout>

Solution 2:

You can do it by specifying layout_weight attribute for map fragment and linear layout that holds editText and a button.

For example:

<LinearLayoutandroid:layout_width="wrap_content"android:layout_weight="1"android:layout_height="wrap_content"><EditTextandroid:layout_width="200dp"android:layout_height="wrap_content"android:id="@+id/etSearch"/><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="search"android:id="@+id/btnSearch"android:layout_gravity="end"style="?android:attr/buttonStyleSmall"android:onClick="onSearch" /></LinearLayout><fragmentxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/map"android:layout_weight="1"tools:context=".MapsActivity"android:name="com.google.android.gms.maps.SupportMapFragment" />

This will split the space equally for map fragment and linear layout, but feel free to try different numbers to achieve what you want

Post a Comment for "Create Buttons Below Google Map"