Skip to content

Instantly share code, notes, and snippets.

@cuber5566
Created January 11, 2019 07:37
Show Gist options
  • Save cuber5566/0a332798d4da10294e2e26418a650609 to your computer and use it in GitHub Desktop.
Save cuber5566/0a332798d4da10294e2e26418a650609 to your computer and use it in GitHub Desktop.
<?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:paddingStart="16dp"
android:paddingEnd="16dp">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/arrowUp"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginStart="8dp"
android:background="@drawable/triangle_down"
android:rotation="180"
app:backgroundTint="@color/black_alpha75"
app:layout_constraintEnd_toEndOf="@+id/info"
app:layout_constraintStart_toStartOf="@+id/info"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="@drawable/corner_black_alpha75"
android:paddingStart="16dp"
android:paddingTop="8dp"
android:paddingEnd="16dp"
android:paddingBottom="8dp"
android:textColor="@color/white_text"
android:textSize="@dimen/text_size_caption"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/arrowUp"
tools:text="infsfdgsdfgsdfgo" />
<android.support.v7.widget.AppCompatImageView
android:id="@+id/dropdown"
android:layout_width="16dp"
android:layout_height="16dp"
android:layout_marginStart="8dp"
android:background="@drawable/triangle_down"
app:backgroundTint="@color/black_alpha75"
app:layout_constraintEnd_toEndOf="@+id/info"
app:layout_constraintStart_toStartOf="@+id/info"
app:layout_constraintTop_toBottomOf="@+id/info" />
</android.support.constraint.ConstraintLayout>
@SuppressLint("InflateParams")
fun View.showPopupInfo(text: String) {
val view = LayoutInflater.from(context).inflate(R.layout.popup_info, null, false)
val popupWindow = PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true)
popupWindow.isTouchable = true
popupWindow.setTouchInterceptor { _, _ -> false }
val windowWidth = Resources.getSystem().displayMetrics.widthPixels
popupWindow.showAsDropDown(this, windowWidth, -height * 2)
view.info.text = text
view.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
val rootLocation = IntArray(2)
getLocationOnScreen(rootLocation)
val offsetX = -(view.width / 2) + width / 2
val offsetY = if (rootLocation[1] - view.height <= 32.toDp()) {
view.arrowUp.visible()
view.dropdown.gone()
0
} else {
view.arrowUp.gone()
view.dropdown.visible()
-height - view.height + view.arrowUp.height
}
popupWindow.dismiss()
popupWindow.showAsDropDown(this@showPopupInfo, offsetX, offsetY)
view.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
val popupLocation = IntArray(2)
view.getLocationOnScreen(popupLocation)
val arrowX = (rootLocation[0] - popupLocation[0] + width / 2 - view.dropdown.width / 2).toFloat()
view.arrowUp.x = arrowX
view.dropdown.x = arrowX
}
})
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment