Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve #1 Support wrap content & padding instead of height/width dp #20

Merged
merged 5 commits into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions app/src/main/java/com/skydoves/balloondemo/CustomActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.skydoves.balloon.balloon
import com.skydoves.balloondemo.factory.CustomListBalloonFactory
Expand Down Expand Up @@ -58,13 +57,12 @@ class CustomActivity : AppCompatActivity(),
tabLayout.addTab(tabLayout.newTab().setText("Contents"))

recyclerView.adapter = adapter
recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
this.adapter.addItems(ItemUtils.getSamples(this))
adapter.addItems(ItemUtils.getSamples(this))

// gets customListBalloon's recyclerView.
val listRecycler: RecyclerView =
customListBalloon.getContentView().findViewById(R.id.list_recyclerView)
listRecycler.adapter = customAdapter
listRecycler.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)
this.customAdapter.addCustomItem(ItemUtils.getCustomSamples(this))

toolbar_list.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class ProfileBalloonFactory : Balloon.Factory() {
setArrowSize(10)
setArrowOrientation(ArrowOrientation.TOP)
setArrowPosition(0.5f)
setWidthRatio(0.55f)
setHeight(250)
isRtlSupport(BalloonUtils.isRtlLayout())
setCornerRadius(4f)
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/layout/activity_custom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="334"
android:text="1024"
android:textColor="@color/white_93"
android:textSize="16sp"
android:textStyle="bold" />
Expand Down Expand Up @@ -166,6 +166,8 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_sample" />
</LinearLayout>

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="334"
android:text="1024"
android:textColor="@color/white_93"
android:textSize="16sp"
android:textStyle="bold" />
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/item_custom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<LinearLayout 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_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:background="?attr/selectableItemBackground"
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/res/layout/layout_custom_list.xml
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="8dp">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list_recyclerView"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/item_custom" />
</LinearLayout>
4 changes: 3 additions & 1 deletion app/src/main/res/layout/layout_custom_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
<LinearLayout 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_width="240dp"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="12dp">

Expand Down Expand Up @@ -33,6 +34,7 @@
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="Love coffee, music, magic tricks and writing poems."
android:textColor="@color/background800"
android:textSize="14sp"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/layout_custom_tag.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
android:padding="4dp">

<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/circleImageView"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_gravity="center_vertical"
android:scaleType="centerCrop"
android:src="@drawable/ic_assignment"
android:tint="@color/colorPrimaryDark" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="12"
android:textColor="@android:color/black"
android:textSize="16sp"
Expand Down
107 changes: 81 additions & 26 deletions balloon/src/main/java/com/skydoves/balloon/Balloon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import android.os.Handler
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.RelativeLayout
import androidx.annotation.ColorInt
Expand All @@ -48,7 +49,6 @@ import androidx.lifecycle.OnLifecycleEvent
import com.skydoves.balloon.annotations.Dp
import com.skydoves.balloon.annotations.Sp
import kotlinx.android.synthetic.main.layout_balloon.view.balloon_arrow
import kotlinx.android.synthetic.main.layout_balloon.view.balloon_background
import kotlinx.android.synthetic.main.layout_balloon.view.balloon_content
import kotlinx.android.synthetic.main.layout_balloon.view.balloon_detail
import kotlinx.android.synthetic.main.layout_balloon.view.balloon_icon
Expand Down Expand Up @@ -83,10 +83,19 @@ class Balloon(
init {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
this.bodyView = inflater.inflate(R.layout.layout_balloon, null)
val width = getMeasureWidth()
val params = RelativeLayout.LayoutParams(width, builder.height)
this.bodyView.layoutParams = params
this.bodyWindow = PopupWindow(bodyView, width, builder.height)
this.bodyWindow = PopupWindow(bodyView, RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT)

if (builder.widthRatio != NO_Float_VALUE || builder.width != NO_INT_VALUE) {
this.bodyWindow.width = getMeasureWidth()
this.bodyView.balloon_detail.layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT
}

if (builder.height != NO_INT_VALUE) {
this.bodyWindow.height = getMeasureHeight()
this.bodyView.balloon_detail.layoutParams.height = LinearLayout.LayoutParams.MATCH_PARENT
}

createByBuilder()
}

Expand Down Expand Up @@ -127,13 +136,13 @@ class Balloon(
rotation = 90f
}
}
when (builder.arrowOrientation) {
ArrowOrientation.BOTTOM, ArrowOrientation.TOP -> {
x =
supportRtlLayoutFactor * (bodyWindow.width * builder.arrowPosition - (builder.arrowSize / 2))
bodyView.post {
when (builder.arrowOrientation) {
ArrowOrientation.BOTTOM, ArrowOrientation.TOP ->
x = bodyView.width * builder.arrowPosition - (builder.arrowSize / 2)
ArrowOrientation.LEFT, ArrowOrientation.RIGHT ->
y = bodyView.height * builder.arrowPosition - (builder.arrowSize / 2)
}
ArrowOrientation.LEFT, ArrowOrientation.RIGHT ->
y = bodyWindow.height * builder.arrowPosition - (builder.arrowSize / 2)
}
layoutParams = params
alpha = builder.alpha
Expand All @@ -143,8 +152,14 @@ class Balloon(
}

private fun initializeBackground() {
with(bodyView.balloon_background) {
with(bodyView.balloon_detail) {
alpha = builder.alpha
if (builder.padding != NO_INT_VALUE) {
setPadding(builder.padding, builder.padding, builder.padding, builder.padding)
} else {
setPadding(builder.paddingLeft, builder.paddingTop,
builder.paddingRight, builder.paddingBottom)
}
if (builder.backgroundDrawable == null) {
background = GradientDrawable().apply {
setColor(builder.backgroundColor)
Expand Down Expand Up @@ -226,6 +241,7 @@ class Balloon(
bodyView.balloon_detail.removeAllViews()
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
inflater.inflate(builder.layout, bodyView.balloon_detail)
bodyView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
}

private fun applyBalloonAnimation() {
Expand Down Expand Up @@ -282,7 +298,7 @@ class Balloon(
fun show(anchor: View) {
show(anchor) {
bodyWindow.showAsDropDown(anchor, -(anchor.measuredWidth / 2),
-builder.height - (anchor.measuredHeight / 2))
-getMeasureHeight() - (anchor.measuredHeight / 2))
}
}

Expand Down Expand Up @@ -340,7 +356,7 @@ class Balloon(
show(anchor) {
bodyWindow.showAsDropDown(anchor,
supportRtlLayoutFactor * ((anchor.measuredWidth / 2) - (getMeasureWidth() / 2)),
-builder.height - anchor.measuredHeight)
-getMeasureHeight() - anchor.measuredHeight)
}
}

Expand All @@ -358,7 +374,7 @@ class Balloon(
show(anchor) {
bodyWindow.showAsDropDown(anchor,
supportRtlLayoutFactor * ((anchor.measuredWidth / 2) - (getMeasureWidth() / 2) + xOff),
-builder.height - anchor.measuredHeight + yOff)
-getMeasureHeight() - anchor.measuredHeight + yOff)
}
}

Expand Down Expand Up @@ -411,7 +427,7 @@ class Balloon(
fun showAlignRight(anchor: View) {
show(anchor) {
bodyWindow.showAsDropDown(anchor, anchor.measuredWidth,
-(builder.height / 2) - (anchor.measuredHeight / 2))
-(getMeasureHeight() / 2) - (anchor.measuredHeight / 2))
}
}

Expand All @@ -428,7 +444,7 @@ class Balloon(
fun showAlignRight(anchor: View, xOff: Int, yOff: Int) {
show(anchor) {
bodyWindow.showAsDropDown(anchor, anchor.measuredWidth + xOff,
-(builder.height / 2) - (anchor.measuredHeight / 2) + yOff)
-(getMeasureHeight() / 2) - (anchor.measuredHeight / 2) + yOff)
}
}

Expand All @@ -445,7 +461,7 @@ class Balloon(
fun showAlignLeft(anchor: View) {
show(anchor) {
bodyWindow.showAsDropDown(anchor, -(getMeasureWidth()),
-(builder.height / 2) - (anchor.measuredHeight / 2))
-(getMeasureHeight() / 2) - (anchor.measuredHeight / 2))
}
}

Expand All @@ -462,7 +478,7 @@ class Balloon(
fun showAlignLeft(anchor: View, xOff: Int, yOff: Int) {
show(anchor) {
bodyWindow.showAsDropDown(anchor, -(getMeasureWidth()) + xOff,
-(builder.height / 2) - (anchor.measuredHeight / 2) + yOff)
-(getMeasureHeight() / 2) - (anchor.measuredHeight / 2) + yOff)
}
}

Expand All @@ -482,7 +498,7 @@ class Balloon(

val dismissWindow: () -> Unit = { this.bodyWindow.dismiss() }
if (this.builder.balloonAnimation == BalloonAnimation.CIRCULAR) {
this.bodyWindow.contentView.circularUnRevealed() {
this.bodyWindow.contentView.circularUnRevealed {
dismissWindow()
}
} else {
Expand Down Expand Up @@ -526,12 +542,22 @@ class Balloon(
}
}

/** gets measured width size of the balloon. */
/** gets measured width size of the balloon popup. */
fun getMeasureWidth(): Int {
if (builder.widthRatio != 0f) {
if (builder.widthRatio != NO_Float_VALUE) {
return (context.displaySize().x * builder.widthRatio - builder.space).toInt()
} else if (builder.width != NO_INT_VALUE) {
return builder.width
}
return this.bodyView.measuredWidth
}

/** gets measured height size of the balloon popup. */
fun getMeasureHeight(): Int {
if (builder.height != NO_INT_VALUE) {
return builder.height
}
return builder.width - builder.space
return this.bodyView.measuredHeight
}

/** gets a content view of the balloon popup window. */
Expand All @@ -549,11 +575,21 @@ class Balloon(
@BalloonDsl
class Builder(private val context: Context) {
@JvmField @Dp
var width: Int = context.displaySize().x
var width: Int = NO_INT_VALUE
@JvmField @FloatRange(from = 0.0, to = 1.0)
var widthRatio: Float = 0f
var widthRatio: Float = NO_Float_VALUE
@JvmField @Dp
var height: Int = context.dp2Px(60)
var height: Int = NO_INT_VALUE
@JvmField @Dp
var padding: Int = NO_INT_VALUE
@JvmField @Dp
var paddingLeft: Int = 0
@JvmField @Dp
var paddingTop: Int = 0
@JvmField @Dp
var paddingRight: Int = 0
@JvmField @Dp
var paddingBottom: Int = 0
@JvmField @Dp
var space: Int = 0
@JvmField
Expand Down Expand Up @@ -636,6 +672,25 @@ class Balloon(
/** sets the height size. */
fun setHeight(@Dp value: Int): Builder = apply { this.height = context.dp2Px(value) }

/** sets the padding on all directions. */
fun setPadding(@Dp value: Int): Builder = apply { this.padding = context.dp2Px(value) }

/** sets the left padding on all directions. */
fun setPaddingLeft(@Dp value: Int): Builder = apply { this.paddingLeft = context.dp2Px(value) }

/** sets the top padding on all directions. */
fun setPaddingTop(@Dp value: Int): Builder = apply { this.paddingTop = context.dp2Px(value) }

/** sets the right padding on all directions. */
fun setPaddingRight(@Dp value: Int): Builder = apply {
this.paddingRight = context.dp2Px(value)
}

/** sets the bottom padding on all directions. */
fun setPaddingBottom(@Dp value: Int): Builder = apply {
this.paddingBottom = context.dp2Px(value)
}

/** sets the side space between popup and display. */
fun setSpace(@Dp value: Int): Builder = apply { this.space = context.dp2Px(value) }

Expand Down
3 changes: 3 additions & 0 deletions balloon/src/main/java/com/skydoves/balloon/Definition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package com.skydoves.balloon
/** definition of the non-value of Int type. */
const val NO_INT_VALUE: Int = -1

/** definition of the non-value of Float type. */
const val NO_Float_VALUE: Float = 0f

/** definition of the non-value of Long type. */
const val NO_LONG_VALUE: Long = -1L

Expand Down
Loading