How can you make a custom toast notification fill the width of the screen?
I have tried multiple things and can't get it to work.
Thanks.
If a simple text message isn't enough, you can create a customized layout for your toast notification. To create a custom layout, define a View layout, in XML or in your application code, and pass the root View object to the setView (View) method.
Positioning your Toast A standard toast notification appears near the bottom of the screen, centered horizontally. You can change this position with the setGravity(int, int, int) method. This accepts three parameters: a Gravity constant, an x-position offset, and a y-position offset.
This should work:
Toast t = Toast.makeText(this, "Hello", Toast.LENGTH_SHORT); t.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
You can create toast with custom layout and fill_horizontal. Below code is written in Adapter class and it is working fine.
LayoutInflater inflater =(LayoutInflater)mContext .getSystemService(mContext.LAYOUT_INFLATER_SERVICE); View layout =inflater.inflate(R.layout.custom_toast_layout,null); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Hello! This is a custom toast!"); Toast toast = new Toast(mContext); //use both property in single function toast.setGravity(Gravity.BOTTOM|Gravity.FILL_HORIZONTAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With