How to let SnackBar take full-width of the screen in android

43 Views Asked by At

I have changed the width to MATCH_PARENT even if I use custom layout,it still doesn't work. And I want it's bottom gap is 0, so I set layout.bottom = 0, it also doesn't work. how to resolve it? My device is an AndroidPad.

snackbar in MainActivity

val view: View = findViewById(android.R.id.content)
var inflater = LayoutInflater.from(this)
val customView: View = inflater.inflate(R.layout.custom_snackbar, null)
snackbar = Snackbar.make(view, "", Snackbar.LENGTH_LONG)
snackbar?.setBackgroundTint(ContextCompat.getColor(this, R.color.primary))
val layout = snackbar?.view as Snackbar.SnackbarLayout
val textView: TextView = customView.findViewById(R.id.snackbar_text)
val layoutParams = layout.layoutParams
layout.bottom = 0
textView.text = message
layoutParams.width = CoordinatorLayout.LayoutParams.MATCH_PARENT//change to LinearLayout or FrameLayout get the same effect
layout.removeAllViews()
layout.addView(customView)
snackbar?.show()

custom_snackbar.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_snackbar">

    <TextView
        android:id="@+id/snackbar_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"/>
</LinearLayout>

actual rendering effect enter image description here

the effect i want enter image description here

1

There are 1 best solutions below

1
Javad Sheikhsagha On

Please add below code to your layout :

layout.setPadding(0, 0, 0, 0)

for further more information please check : link