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

Textview in the custom layout without decoration. #151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,29 @@ class MainActivity : AppCompatActivity() {

tooltip?.dismiss()

// tooltip = Tooltip.Builder(this)
// .anchor(button, 0, 0, false)
// .text(text)
// .styleId(style)
// .typeface(typeface)
// .maxWidth(metrics.widthPixels / 2)
// .arrow(arrow)
// .floatingAnimation(animation)
// .closePolicy(closePolicy)
// .showDuration(showDuration)
// .overlay(overlay)
// .create()


tooltip = Tooltip.Builder(this)
.anchor(button, 0, 0, false)
.text(text)
.styleId(style)
.typeface(typeface)
.maxWidth(metrics.widthPixels / 2)
.arrow(arrow)
.customViewNoDecor(R.layout.custom_tooltip, R.id.tooltip_text)
.floatingAnimation(animation)
.text(text)
.closePolicy(closePolicy)
.showDuration(showDuration)
.overlay(overlay)
.create()


tooltip
?.doOnHidden {
tooltip = null
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/res/layout/custom_tooltip.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
android:padding="0dp">

<androidx.appcompat.widget.AppCompatTextView
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:id="@+id/tooltip_text"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark"
android:autoLink="all"
android:clipToPadding="false"
android:gravity="center"
android:padding="16dp"
android:text="Textview with no decoration. you don't need to care of decoration by code"
android:textAppearance="?android:attr/textAppearanceSmallInverse" />
</FrameLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class Tooltip private constructor(private val context: Context, builder: Builder
private var mDrawable: TooltipTextDrawable? = null
private var mAnchorView: WeakReference<View>? = null
private lateinit var mContentView: View
private var isCustomViewNoDecor = false
private lateinit var mTextView: TextView

private val hideRunnable = Runnable { hide() }
Expand Down Expand Up @@ -124,7 +125,7 @@ class Tooltip private constructor(private val context: Context, builder: Builder
offsetBy(
(mNewLocation[0] - mOldLocation!![0]).toFloat(),
(mNewLocation[1] - mOldLocation!![1]).toFloat()
)
)
}

mOldLocation!![0] = mNewLocation[0]
Expand All @@ -137,12 +138,12 @@ class Tooltip private constructor(private val context: Context, builder: Builder

init {
val theme = context.theme
.obtainStyledAttributes(
null,
R.styleable.TooltipLayout,
builder.defStyleAttr,
builder.defStyleRes
)
.obtainStyledAttributes(
null,
R.styleable.TooltipLayout,
builder.defStyleAttr,
builder.defStyleRes
)
this.mPadding = theme.getDimensionPixelSize(R.styleable.TooltipLayout_ttlm_padding, 30)
mOverlayStyle =
theme.getResourceId(
Expand Down Expand Up @@ -185,6 +186,7 @@ class Tooltip private constructor(private val context: Context, builder: Builder
}

builder.layoutId?.let {
isCustomViewNoDecor = builder.customViewNoDecor
mTextViewIdRes = builder.textId!!
mTooltipLayoutIdRes = builder.layoutId!!
mIsCustomView = true
Expand Down Expand Up @@ -229,12 +231,14 @@ class Tooltip private constructor(private val context: Context, builder: Builder
}

fun update(text: CharSequence?) {
mText = text
if (isShowing && null != mPopupView) {
mTextView.text = if (text is Spannable) {
text
} else {
HtmlCompat.fromHtml(text as String, HtmlCompat.FROM_HTML_MODE_COMPACT)
text?.let {
mText = text
if (isShowing && null != mPopupView) {
mTextView.text = if (text is Spannable) {
text
} else {
HtmlCompat.fromHtml(text as String, HtmlCompat.FROM_HTML_MODE_COMPACT)
}
}
}
}
Expand Down Expand Up @@ -312,25 +316,26 @@ class Tooltip private constructor(private val context: Context, builder: Builder
}

mFloatingAnimation?.let { contentView.setPadding(it.radius) }

mTextView = contentView.findViewById(mTextViewIdRes)

with(mTextView) {
mDrawable?.let { background = it }
if(!isCustomViewNoDecor){
with(mTextView) {
mDrawable?.let { background = it }

if (mShowArrow)
setPadding(mPadding, mPadding, mPadding, mPadding)
else
setPadding(mPadding / 2, mPadding / 2, mPadding / 2, mPadding / 2)
if (mShowArrow)
setPadding(mPadding, mPadding, mPadding, mPadding)
else
setPadding(mPadding / 2, mPadding / 2, mPadding / 2, mPadding / 2)

text = if (mText is Spannable) {
mText
} else {
HtmlCompat.fromHtml([email protected] as String, HtmlCompat.FROM_HTML_MODE_COMPACT)
}
text = if (mText is Spannable) {
mText
} else {
HtmlCompat.fromHtml([email protected] as String, HtmlCompat.FROM_HTML_MODE_COMPACT)
}

mMaxWidth?.let { maxWidth = it }
mTypeface?.let { typeface = it }
mMaxWidth?.let { maxWidth = it }
mTypeface?.let { typeface = it }
}
}

if (null != mViewOverlay) {
Expand Down Expand Up @@ -488,7 +493,7 @@ class Tooltip private constructor(private val context: Context, builder: Builder
contentPosition.y,
contentPosition.x + w,
contentPosition.y + h
)
)
if (!displayFrame.rectContainsWithTolerance(finalRect, mSizeTolerance.toInt())) {
Timber.e("content won't fit! $displayFrame, $finalRect")
return findPosition(parent, anchor, offset, gravities, params, fitToScreen)
Expand Down Expand Up @@ -636,7 +641,7 @@ class Tooltip private constructor(private val context: Context, builder: Builder
gravities,
params,
fitToScreen)
)
)
}

fun hide() {
Expand Down Expand Up @@ -843,6 +848,7 @@ class Tooltip private constructor(private val context: Context, builder: Builder
internal var activateDelay = 0L
internal var followAnchor = false
internal var animationStyle: Int? = null
internal var customViewNoDecor: Boolean = false

@LayoutRes
internal var layoutId: Int? = null
Expand All @@ -865,6 +871,13 @@ class Tooltip private constructor(private val context: Context, builder: Builder
return this
}

fun customViewNoDecor(@LayoutRes layoutId: Int, @IdRes textId: Int): Builder {
this.layoutId = layoutId
this.textId = textId
customViewNoDecor = true
return this
}

fun customView(@LayoutRes layoutId: Int, @IdRes textId: Int): Builder {
this.layoutId = layoutId
this.textId = textId
Expand Down