Skip to content

Difference in setHeight() and resulting balloon height #808

@Equin

Description

@Equin

Please complete the following information:

  • Library Version [e.g. v1.6.12]
  • Affected Device(s) [emulator on API 33 and Xiaomi Mi9t Pro Api 31]

Describe the Bug:

When using WRAP height, it works with not big text but when its 6+ lines of text it crops in top or bottom.
Image

When using Balloon.Builder.setLayout(View) together with setHeight(int height), the height is either ignored completely and causes the popup to be small.
Image

```
val ratio = if (ScreenConfig.isPhone(activity)) 0.95f else 0.5f
val width = calculateWidth(ratio, 0)
val customView = inflateLayout()
val height = measureView(customView, width)
customView.layoutParams?.height = height

val b = getBitmapFromView(customView)

val builder = Balloon.Builder(activity)
.setLayout(customView)
.setWidthRatio(ratio)
.setHeight(height.pxToDp())

val balloon = builder.build()


private fun calculateWidth(ratio: Float, offset: Int): Int {
    val displayWidth = Resources.getSystem().displayMetrics.widthPixels - offset
    return (displayWidth * ratio).toInt()
}

private fun measureView(view: View, width: Int): Int {
    val widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY)
    val heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)

    view.measure(widthMeasureSpec, heightMeasureSpec)
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight())
    
    val height: Int = view.getMeasuredHeight()

    return height
}

private fun inflateLayout(): View {
    val inflater = activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    val view = inflater.inflate(R.layout.view_favorite_model, null)
    val txtTitle = view.findViewById<TextView>(R.id.txtTitle)
    val txtDescription = view.findViewById<TextView>(R.id.txtDescription)

    txtDescription?.maxLines = 5

    txtTitle.text = title
    txtDescription.text = message
    if (message.isNullOrEmpty()) {
        txtDescription.visibility = View.GONE
    } else {
        txtDescription.visibility = View.VISIBLE
    }

    txtDescription?.setOnClickListener {
        action?.invoke()
    }

    val lp = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT)
    view.layoutParams = lp

    return view
}

and here is XML 

<?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="wrap_content"
    android:paddingVertical="10dp"
    android:minHeight="60dp"
    android:background="@color/krLightIndigo"
    >

    <ImageView
        android:id="@+id/imgIcon"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginStart="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0"
        />

    <TextView
        android:id="@+id/txtTitle"
        style="@style/FontStyleSemiBold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constrainedWidth="true"
        android:layout_marginHorizontal="10dp"
        android:textColor="@color/krBlack"
        android:textSize="14sp"
        app:layout_constraintBottom_toTopOf="@+id/txtDescription"
        app:layout_constraintEnd_toStartOf="@+id/imgClose"
        app:layout_constraintStart_toEndOf="@+id/imgIcon"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed"
        app:layout_goneMarginStart="16dp"
        tools:text="Some ldsf ds gdfsg dfg dg sdgdfg dg dsg sdg sd gdsf gds gd fgdg d gdssd fgsd gdfg sd gsdfg fgong tedfg t " />

    <TextView
        android:id="@+id/txtDescription"
        style="@style/FontStyleRegular"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constrainedWidth="true"
        android:layout_marginHorizontal="10dp"
        app:layout_constraintHorizontal_bias="0"
        android:textColor="@color/krPrimary"
        android:textSize="14sp"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/imgClose"
        app:layout_constraintStart_toEndOf="@+id/imgIcon"
        app:layout_constraintTop_toBottomOf="@+id/txtTitle"
        app:layout_goneMarginStart="16dp"
        tools:text="Some losdf dfsdf sdf sdf sdf sd fsdf                  sdf sd fsdf sdfs dfsdfsd fsd fsng text " />


    <ImageView
        android:id="@+id/imgClose"
        android:layout_width="wrap_content"
        android:layout_height="24dp"
        android:layout_marginEnd="16dp"
        app:layout_constraintVertical_bias="0.5"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_close" />


</androidx.constraintlayout.widget.ConstraintLayout>


Expected Behavior:

I want to view measure correctly or Balloon stop ignoring setHeight.

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions