1

I'm developing an app. In this app there's an alertdialog that opens when you click a point. In this alert dialog there are two edittext, a gridview and a button. The gridview has associated a BaseAdapter that convert Uris to IconView (a class that extends ImageView) using Glide.

I want to set a maximum height of 350dp, but if there is just one row (there'll be always one image) I want the gridview to adapt.

I tried this way: How to have a GridView that adapts its height when items are added

But it doesn't work. So what I've tried is to count the number of elements and if there's is more than 2, set 350dp of height if there is less than 2, set 175dp. But it doesnt work.

The alert dialog layout:

<?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="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <EditText
        android:id="@+id/puntoName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:fontFamily="sans-serif"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/puntoDescripcion"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:maxHeight="100dp"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/puntoName" />


    <GridView
        android:id="@+id/photo_grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:gravity="center"
        android:horizontalSpacing="6dp"
        android:numColumns="2"
        android:verticalSpacing="5dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/puntoDescripcion"></GridView>


    <Button
        android:id="@+id/cerrar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cerrar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/photo_grid" />

</androidx.constraintlayout.widget.ConstraintLayout>

This is the code I'm using in order to change GridView height:

fun checkSize() {
    val popUp: View = fragmentCaller.layoutInflater.inflate(R.layout.popup,null)
    var photogrid: GridView = popUp.findViewById(R.id.photo_grid)
    var params: ViewGroup.LayoutParams = photogrid.layoutParams
    var tam: Int = 175
    if (adapter.getDataSource().size>2) {
        tam = DpToPixels(350)
    }
    photogrid.layoutParams.height = tam
    photogrid.requestLayout()
}

private fun DpToPixels(dp: Int) : Int {
    val escala: Float = fragmentCaller.requireContext().resources.displayMetrics.density;
    var tam: Int = (dp * escala + 0.5f).toInt()
    return tam
}

The size of the gridView is always 0. I don't know why. I just want to set android:layout_height programmatically. I call checkSize() method after I call Dialog.show() and everytime I do changes in the elements of adapter.

Thanks.

ahmad bajwa
  • 966
  • 2
  • 10
  • 30

0 Answers0