Search code examples
androidxmlandroid-layoutviewinfowindow

Set view to match_parent in a layout in android


I have a tiny problem with a InfoWindow layout. I am trying to set a view in the layout to match the parent in width, but I get some small gaps in the left and right margins. I tried with LinearLayout, RelativeLayout and ConstraintLayout as root view, I get the same results.

Here is my XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                         xmlns:app="http://schemas.android.com/apk/res-auto"
                                         xmlns:tools="http://schemas.android.com/tools"
                                         android:layout_width="wrap_content"
                                         android:layout_height="wrap_content"
                                         android:layout_margin="0dp"
                                         android:orientation="vertical"
                                         android:padding="0dp">

<View
    android:id="@+id/view"
    android:layout_width="0dp"
    android:layout_height="30dp"
    android:layout_marginTop="0dp"
    android:background="@color/login_button_color"
    android:orientation="horizontal"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent">

</View>

<de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/profile_image_info_view"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="80dp"
    android:layout_marginRight="80dp"
    android:layout_marginTop="45dp"
    android:layout_weight="2"
    android:src="@drawable/com_facebook_profile_picture_blank_portrait"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    tools:ignore="InefficientWeight"
    android:layout_marginStart="80dp"
    android:layout_marginEnd="80dp"/>

<TextView
    android:id="@+id/owner_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_margin="10dp"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:layout_weight="1"
    android:gravity="center_horizontal"
    android:text="@string/placeholder"
    android:textColor="@android:color/black"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/profile_image_info_view"
    app:layout_constraintVertical_bias="0.17000002"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"/>

<TextView
    android:id="@+id/event_title_info_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginBottom="40dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_weight="1"
    android:foregroundGravity="center_horizontal"
    android:gravity="center_horizontal"
    android:text="@string/placeholder"
    android:textColor="@color/com_facebook_button_background_color"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/owner_name"
    app:layout_constraintVertical_bias="0.92"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp"/>

</android.support.constraint.ConstraintLayout>

And a screenshot:

enter image description here

This is how I inflate it:

mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter()
                    {
                        @Override
                        public View getInfoWindow(Marker marker)
                        {
                            return null;
                        }

                        @Override
                        public View getInfoContents(Marker marker)
                        {
                            View view = 
                    getLayoutInflater().inflate(R.layout.fragment_info_view, 
                    null);
                            profileImageInfoView = (CircleImageView) 
                    view.findViewById(R.id.profile_image_info_view);
                            ownerName = 
                    (TextView)view.findViewById(R.id.owner_name);
                            eventTitle = (TextView) 
                    view.findViewById(R.id.event_title_info_view);
                            return view;
                        }
                    });

Thank you in advance!


Solution

  • The code you have written in getInfoContents() method move it to the getInfoWindow() method.

    If you use getInfoContents() method then it will add a white background with some padding but getInfoWindow() method does not do that.

    Updated code will be:

    mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
        @Override
        public View getInfoWindow(Marker marker) {
            View view = getLayoutInflater().inflate(R.layout.fragment_info_view, null);
            profileImageInfoView = (CircleImageView) 
            view.findViewById(R.id.profile_image_info_view);
            ownerName = (TextView)view.findViewById(R.id.owner_name);
            eventTitle = (TextView)view.findViewById(R.id.event_title_info_view);
            return view;
        }
    
        @Override
        public View getInfoContents(Marker marker) {
            return null;
        }
    });