How to alight drawable left near to TextView Android (Or custom snackbar android)

I'm using a custom SnackBar for android, I have created SnackBar with an icon and a message. Icon drawable isn't working as expected. The margin text is further than expected. So, this is what I did: SnackBar-1

The expected custom snackbar is: SnackBar-2 How can I get my expected output? Thank you very much.

Here is my code:

fun showSuccessFullMsg(message: String, anchorView: View) {
    val snackBarView = Snackbar.make(binding.root, message, Snackbar.LENGTH_LONG)
    val view = snackBarView.view
    val params = view.layoutParams as FrameLayout.LayoutParams
    snackBarView.setBackgroundTint(resources.getColor(android.R.color.white))
    params.gravity = Gravity.TOP
    params.gravity = Gravity.CENTER_HORIZONTAL
    val actionBarHeight = anchorView.height

    params.setMargins(32, actionBarHeight.plus(36), 32, 0)
    view.layoutParams = params

    val textView = view.findViewById<TextView>(com.google.android.material.R.id.snackbar_text)
    textView.textAlignment = View.TEXT_ALIGNMENT_CENTER
    val drawable = resources.getDrawable(
        R.drawable.ic_double_check,
        resources.newTheme()
    )
    (textView.layoutParams as LinearLayout.LayoutParams).gravity = Gravity.CENTER_HORIZONTAL
    drawable.bounds = Rect(0, 0, 32, 24)
    textView.setCompoundDrawablesRelativeWithIntrinsicBounds(drawable, null, null, null)
    textView.isSingleLine = true
    textView.ellipsize = TextUtils.TruncateAt.END
    view.background =
        ContextCompat.getDrawable(
            this,
            com.proxglobal.ringtone.R.drawable.bg_snackbar
        ) // for custom background
    snackBarView.animationMode = BaseTransientBottomBar.ANIMATION_MODE_FADE
    snackBarView.setTextColor(Color.parseColor("#23C16B"))
    snackBarView.show()
}

Upvotes: 0

Views: 138

Answers (2)

Thank for Answer of @Cheticamp. Finally, Here is my code for this:

  @SuppressLint("PrivateResource", "UseCompatLoadingForDrawables")
fun showSuccessFullMsg(message: String, anchorView: View) {
    val drawable = resources.getDrawable(
        R.drawable.ic_double_check,
        resources.newTheme()
    )
    drawable.bounds = Rect(0, 0, 64, 48)
    val imageSpan = ImageSpan(drawable, ImageSpan.ALIGN_BASELINE)
    val sp = SpannableString("  $message")
    sp.setSpan(imageSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
    val snackBarView = Snackbar.make(binding.root, sp, Snackbar.LENGTH_LONG)
    val view = snackBarView.view
    val params = view.layoutParams as FrameLayout.LayoutParams
    snackBarView.setBackgroundTint(resources.getColor(android.R.color.white))
    params.gravity = Gravity.TOP
    params.gravity = Gravity.CENTER_HORIZONTAL
    val actionBarHeight = anchorView.height

    params.setMargins(32, actionBarHeight.plus(36), 32, 0)
    view.layoutParams = params

    val textView = view.findViewById<TextView>(com.google.android.material.R.id.snackbar_text)
    textView.textAlignment = View.TEXT_ALIGNMENT_CENTER

    (textView.layoutParams as LinearLayout.LayoutParams).gravity = Gravity.CENTER_HORIZONTAL
    textView.isSingleLine = true
    textView.ellipsize = TextUtils.TruncateAt.END
    view.background =
        ContextCompat.getDrawable(
            this,
            R.drawable.bg_snackbar
        ) // for custom background
    snackBarView.animationMode = BaseTransientBottomBar.ANIMATION_MODE_FADE
    snackBarView.setTextColor(Color.parseColor("#23C16B"))
    snackBarView.show()
}

Upvotes: 0

Cheticamp
Cheticamp

Reputation: 62841

Remove the line

textView.textAlignment = View.TEXT_ALIGNMENT_CENTER

and programmatically add padding to the start of the TextView. You will have to calculate from the sizes of the TextView, drawable and text to determine how much padding to apply.

A second way is to add the drawable into the text itself using a ImageSpan.

Upvotes: 0

Related Questions