Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a custom toast fill width?

Tags:

android

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.

like image 873
Jez Fischer Avatar asked Jan 16 '11 21:01

Jez Fischer


People also ask

Can we set a custom layout for a toast?

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.

How do you set gravity on toast?

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.


2 Answers

This should work:

Toast t = Toast.makeText(this, "Hello", Toast.LENGTH_SHORT); t.setGravity(Gravity.FILL_HORIZONTAL, 0, 0); 
like image 51
C0deAttack Avatar answered Sep 28 '22 01:09

C0deAttack


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(); 
like image 25
Sumit Kumar Avatar answered Sep 28 '22 03:09

Sumit Kumar