简体   繁体   中英

How to make snackbar clickable?

As we know we can set action text to the snackbar .But I wanna make the whole snackbar clickable .

For that I tried to make the view of snackbar clickable but it was futile. Can anyone please help me to solve this?

Code:

Snackbar snackbar=Snackbar.make(home_btn, content, Snackbar.LENGTH_LONG);
    View sbView = snackbar.getView();
    sbView.setClickable(true);
    sbView.setFocusable(true);
    sbView.setBackgroundColor(Color.parseColor("#ffffff"));
    TextView tv = sbView.findViewById(android.support.design.R.id.snackbar_text);
    tv.setTextColor(Color.parseColor("#001919"));
    CoordinatorLayout.LayoutParams params=(CoordinatorLayout.LayoutParams)sbView.getLayoutParams();
    params.gravity = Gravity.TOP;
    sbView.setLayoutParams(params);
    snackbar.show();

    sbView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent();
            i.setAction(action);

            i.putExtras(b);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
        }
    });

Actually it's my fault I forgot to start intent . So, the final answer will be

Snackbar snackbar=Snackbar.make(home_btn, content, Snackbar.LENGTH_LONG);
    View sbView = snackbar.getView();
    sbView.setClickable(true);
    sbView.setFocusable(true);
    sbView.setBackgroundColor(Color.parseColor("#ffffff"));
    TextView tv = sbView.findViewById(android.support.design.R.id.snackbar_text);
    tv.setTextColor(Color.parseColor("#001919"));
    CoordinatorLayout.LayoutParams params=(CoordinatorLayout.LayoutParams)sbView.getLayoutParams();
    params.gravity = Gravity.TOP;
    sbView.setLayoutParams(params);
    snackbar.show();

    sbView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent();
            i.setAction(action);
            i.putExtras(b);
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
            startActivity(i);
        }
    });

I have an alternative using Kotlin's extensions.

Copy these 2 extensions to your code:

This extension is to make a linkable text view:

// Will set links on a text view
fun TextView.setLinks(vararg links: Pair<String, View.OnClickListener>) {
    val spannableString = SpannableString(this.text)
    for (link in links) {
        val clickableSpan = object : ClickableSpan() {

            override fun updateDrawState(textPaint: TextPaint) {
                // use this to change the link color
                textPaint.color = textPaint.linkColor
                // toggle below value to enable/disable
                // the underline shown below the clickable text
                textPaint.isUnderlineText = true
            }

            override fun onClick(view: View) {
                Selection.setSelection((view as TextView).text as Spannable, 0)
                view.invalidate()
                link.second.onClick(view)
            }
        }
        val startIndexOfLink = this.text.toString().indexOf(link.first)
        spannableString.setSpan(
            clickableSpan, startIndexOfLink, startIndexOfLink + link.first.length,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
    }
    this.movementMethod = LinkMovementMethod.getInstance() // without LinkMovementMethod, link can not click
    this.setText(spannableString, TextView.BufferType.SPANNABLE)
}

This extension is to pop the snack bar from an activity:

// Will pop a snack bar from an activity
fun Activity.popSnackBar(text: String, duration: Int = Snackbar.LENGTH_LONG, links: Array<Pair<String, View.OnClickListener>>? = null) {
    val sb = Snackbar.make(findViewById<View>(android.R.id.content).rootView, text, duration)
    if (links != null) {
        val tv = sb.view.findViewById<TextView>(com.google.android.material.R.id.snackbar_text)
        tv.setLinks(*links)
    }
    sb.show()
}

Finally, call this from your activity:

popSnackBar("Click here or exit", links = arrayOf(
                Pair("here", View.OnClickListener { println("clicked here") }),
                Pair("exit", View.OnClickListener { println("clicked exit") })
            ))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM