0

I have given a fixed android:layout_width="300dp", but despite that, the width changes depending on the content of the TextView. Why? How can I keep the width fixed? Instead, the height can changes.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="300dp"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tips"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:text="tips"
        android:textAlignment="center"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/store"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginRight="8dp"
        android:text="store"
        android:textAlignment="center"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tips" />

    <Button
        android:id="@+id/get"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:text="GET"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/store" />

    <Button
        android:id="@+id/go"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="16dp"
        android:text="GO"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/store" />


</androidx.constraintlayout.widget.ConstraintLayout>

enter image description here enter image description here

EDIT

MapsActivity.java

db.collection("Stores").addSnapshotListener((queryDocumentSnapshots, e) -> {
            if (queryDocumentSnapshots != null) {
                for (QueryDocumentSnapshot document : queryDocumentSnapshots) {
                    try{
                        MarkerOptions options = new MarkerOptions()
                                .position(new LatLng(document.getGeoPoint("geoPoint").getLatitude(), document.getGeoPoint("geoPoint").getLongitude()))
                                .title(String.valueOf(document.get("name")))
                                .snippet(String.valueOf(document.get("balanceTips")))
                                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN));
                        CustomInfoWindowGoogleMap customInfoWindow = new CustomInfoWindowGoogleMap(this);
                        mMap.setInfoWindowAdapter(customInfoWindow);
                        mMarker = mMap.addMarker(options);
                        Log.d(TAG, "addStoresOnMap: " + document.get("name") + " added");

                    }catch (NullPointerException eTry){
                        Log.e(TAG, "addStoresOnMap: NullPointerException: " + eTry.getMessage() );
                    }
                }
            }
        });

CustomInfoWindowGoogleMap.java

public class CustomInfoWindowGoogleMap implements GoogleMap.InfoWindowAdapter {

    private Context context;

    public CustomInfoWindowGoogleMap(Context ctx){
        context = ctx;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {
        View view = ((Activity)context).getLayoutInflater()
                .inflate(R.layout.maps_details, null);

        TextView name = view.findViewById(R.id.store);
        TextView balanceTips = view.findViewById(R.id.tips);

        name.setText(marker.getTitle());
        balanceTips.setText(marker.getSnippet());

        return view;
    }
}
Stefano
  • 209
  • 2
  • 10
  • 34
  • Is that a dialog that is changing width? – Cheticamp May 13 '20 at 22:16
  • your root layout width is static because of that it happens. Also, is it's dialog then sometimes in dialog constraint working weird – Shweta Chauhan May 14 '20 at 05:23
  • @Cheticamp yes it is. I have added more details of the code if they can be useful. – Stefano May 14 '20 at 07:48
  • @ShwetaChauhan so do you think it depends on the java code? I have added more details of the code if they can be useful. – Stefano May 14 '20 at 07:48
  • If I recall, dialogs want to wrap their contents and ignore what's specified for them. You can search for "change dialog width" on Stack Overflow to see some solutions. [Here](https://stackoverflow.com/questions/10242144/adjusting-size-of-custom-dialog-box-in-android) is one. – Cheticamp May 14 '20 at 10:18

0 Answers0