0

I am having a little problem to implement a snackbar. I want my snackbar to take full width and stick to the bottom but it has margin from the bottom and its sides. How can I custom it?

What I tried (not working)

val params : ViewGroup.MarginLayoutParams = snackBar.view.layoutParams as ViewGroup.MarginLayoutParams
        params.setMargins(5, 0, 0, 20)

This is the entire code I wrote about snackbar

class OfflineFragment : Fragment() {

private lateinit var binding: FragmentOfflineBinding
private lateinit var viewModel: OfflineViewModel
private var isFabOpen = false
private var zoomIn: Animation? = null
private var zoomOut: Animation? = null
private var zoomInRotate : Animation? = null
private var zoomOutRotate : Animation? = null
private lateinit var mContext: MainActivity

override fun onAttach(context: Context) {
    super.onAttach(context)
    mContext = context as MainActivity
}


override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?,
): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_offline, container, false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    viewModel = ViewModelProvider(this).get(OfflineViewModel::class.java)
    binding.offlineViewModel = viewModel
    binding.lifecycleOwner = viewLifecycleOwner

    var llmain : ConstraintLayout = binding.layoutMain
    val sbTextView = com.google.android.material.R.id.snackbar_text as TextView
    sbTextView.textAlignment = View.TEXT_ALIGNMENT_CENTER
    val snackBar = Snackbar.make(llmain, "test", 3000)
            .setTextColor(getColor(mContext, R.color.white))
            .setBackgroundTint(getColor(mContext, R.color.colorFailed))
            .setAnimationMode(ANIMATION_MODE_SLIDE)

    snackBar.show()
}

}

My XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="100dp"
    android:text="Button"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />
  </androidx.constraintlayout.widget.ConstraintLayout>

current snackbar enter image description here

Lucy
  • 121
  • 12
  • Post xml layout and class code – Amir Jan 10 '22 at 09:19
  • @AmirAndroidDeveloper I posted them just now! – Lucy Jan 11 '22 at 00:13
  • remove val params : ViewGroup.MarginLayoutParams = snackBar.view.layoutParams as ViewGroup.MarginLayoutParams params.setMargins(5, 0, 0, 20) and check if it has margin right and left – Amir Jan 11 '22 at 07:12
  • deleted but not helpful... it's still the same. I think that's the default for `com.google.android.material.R.id.snackbar_text` ? – Lucy Jan 11 '22 at 08:50
  • 1
    @AmirAndroidDeveloper If it's the default then never mind... I will just use it in that way! Thanks for helping tho! – Lucy Jan 12 '22 at 01:03

0 Answers0