0

So I have added a snackbar to notify user on success or failure... but the text and icon are not properly aligned and I can't figure out how to properly do it

fun notifyUserMessage(context: Context, contextView: View, message: String, 
  notificationType: NotificationTypes = NotificationTypes.ERROR){
    val builderTextLeft = SpannableStringBuilder()
    builderTextLeft.append("    $message")
    builderTextLeft.setSpan(ImageSpan(context, R.drawable.ic_alertcircle), 0, 1, 0)
    builderTextLeft.setSpan(
        AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), 0,
        builderTextLeft.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
    )
    val snackbar = Snackbar.make(contextView, builderTextLeft, Snackbar.LENGTH_LONG)
        .setBackgroundTint(context.getColor(setBackground(notificationType)))
        .setTextColor(context.getColor(R.color.gainsboro_00))

    val params = snackbar.view.layoutParams as FrameLayout.LayoutParams
    params.gravity = Gravity.TOP
    params.height = 250

    snackbar.animationMode = BaseTransientBottomBar.ANIMATION_MODE_FADE
    snackbar.show()
}

but the result looks like :

enter image description here

I would like the text properly aligned with the icon but also make sure that the "text+icon" are both centered in the snackbar.

Any idea ?

Seb
  • 2,659
  • 3
  • 23
  • 52

1 Answers1

0

Try this Solution :

 public static SpannableStringBuilder decorateProTagInString(String name, Activity activity) {
        
            SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
            spannableStringBuilder.append(name);
            if (spannableStringBuilder.length() > 0) {
                try {

                    spannableStringBuilder.setSpan(new RelativeSizeSpan(0.8f), spannableStringBuilder.length() - 7, spannableStringBuilder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    spannableStringBuilder.setSpan(new RoundedBackgroundSpan(activity), spannableStringBuilder.length() - 7, spannableStringBuilder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                } catch (Exception exp) {
                    exp.printStackTrace();
                }

            }
            return spannableStringBuilder;
        
        return null;
    }

Function Call:

AppUtils.decorateProTagInString(image + "message", activity);

Otherwise follow this : Answer Link

Thanks and appreciate.

S_i_l_e_n_t C_o_d_e_r
  • 2,179
  • 2
  • 7
  • 21