Zynastor
Zynastor

Reputation: 91

How to make SnackBar in Android Fragment?

How to make SnackBar in Android Fragment?

What context should i call for it?

 override fun onTaskLongClick(task: Task) {        
        Snackbar.make(view!!.rootView, "Long Click removed...", Snackbar.LENGTH_LONG).show()
    }

this code is working anyway, but it covers the soft key.

Upvotes: 0

Views: 497

Answers (1)

Zynastor
Zynastor

Reputation: 91

Wrong way 1.

 override fun onTaskLongClick(task: Task) {        
        Snackbar.make(view!!.rootView, "Long Click removed...", Snackbar.LENGTH_LONG).show()
    }

This code will cover the software button of phone. Totally not good at all. enter image description here

Wrong way 2.

override fun onTaskLongClick(task: Task) {     
Snackbar.make(activity!!.findViewById(android.R.id.content), "Long Click removed..."
, Snackbar.LENGTH_LONG).show()
    }

This way seems to be working properly, but it covers the floating button and cannot be removed by user. This means user has to wait until SnackBar disappear. It seems to be android.R.id.content is in different layout compared to activity_main. enter image description here

Right way!

override fun onTaskLongClick(task: Task) {       
        Snackbar.make(activity!!.findViewById(R.id.activity_main), "Long Click removed...", Snackbar.LENGTH_LONG).show()
    }

Adding id to activity_main.xml layout and call layout by findviewbyid. This way works correctly like the way we always thought. enter image description here

Upvotes: 1

Related Questions