title: Place Toast with custom layout at the bottom of the screen
body: <p>I have a Toast with a custom layout I want to place at the bottom of the screen, and at the same time have it fill the width of the screen. I cannot get it to work properly.</p>
  
  <p>My Toast layout toast.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;TextView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/text"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="#ee222222"
      android:textColor="#ffffff" /&gt;
  </code></pre>
  
  <p>The code to display the Toast, MyToast.java:</p>
  
  <pre><code>View view = inflater.inflate(R.layout.toast, null);
  TextView textView = phoneToastView.findViewById(R.id.text);
  textView.setText("1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18");
  
  Toast toast = new Toast(getContext());
  toast.setGravity(Gravity.BOTTOM | Gravity.FILL_HORIZONTAL, 0, 0);
  toast.setView(view);
  toast.setDuration(Toast.LENGTH_LONG);
  toast.show();
  </code></pre>
  
  <p><strong>Here is my problem:</strong>    </p>
  
  <p>If I set gravity to BOTTOM, I get the Toast on the bottom of the screen. If I set gravity to FILL_HORIZONTAL, it will fill the screen horizontally. If I use both, it fills the screen horizontally, but there is a gap between the Toast and the bottom of the screen.</p>
  
  <p>I think what happens, is that Android creates the view, moves it to the bottom, and <em>then</em> stretches it out. Now the text suddenly has more space, which for some screens and some texts means that it changes from 2 lines of text to just 1 line of text.</p>
  
  <p>Now the view suddenly requires less vertical space, and the view is shrunk by moving the bottom edge up.</p>
  
  <p>Is there a way around this? Can I force Android to do the horizontal fill <em>first</em> and <em>then</em> do the move to the bottom? Can I get the view to shrink vertically from the top instead of from the bottom?</p>
  
  <p><strong>Answer to some of the comments:</strong></p>
  
  <ul>
  <li>I can not use Snackbar, since this Toast will be displayed when my app is in the background</li>
  <li>The text I'm displaying in my Toast varies in length, so I cannot predict how many lines of text it will be</li>
  <li>The code above is the absolute simplest where I have been able to reproduce the error</li>
  </ul>
  
  <p><strong>Update:</strong></p>
  
  <p>To be able to reproduce the issue, use the code above and run the app on a virtual device Nexus 5.</p>
  
  <p>Below are links to screenshots with three different length texts. The shortest and longest texts are displayed at the bottom, the middle text gets a gap below the Toast:</p>
  
  <ul>
  <li><a href="https://i.stack.imgur.com/QyewU.png" rel="nofollow noreferrer">Toast with short text</a></li>
  <li><a href="https://i.stack.imgur.com/if6hl.png" rel="nofollow noreferrer">Toast with medium text</a></li>
  <li><a href="https://i.stack.imgur.com/48O5A.png" rel="nofollow noreferrer">Toast with long text</a></li>
  </ul>
  
  <p><strong>Update 2: My solution</strong></p>
  
  <p>My new toast layout toast.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="#ee222222"
      android:orientation="vertical"&gt;
  
      &lt;TextView
          android:id="@+id/text1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:ellipsize="end"
          android:maxLines="1"
          android:textColor="#ffffff" /&gt;
  
      &lt;TextView
          android:id="@+id/text2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="top"
          android:ellipsize="end"
          android:maxLines="1"
          android:textColor="#ffffff" /&gt;
  
      &lt;TextView
          android:id="@+id/text3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:maxLines="3"
          android:ellipsize="end"
          android:textColor="#ffffff" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The new code to display the Toast, MyToast.java:</p>
  
  <pre><code>View view = inflater.inflate(R.layout.toast, null);
  
  TextView textView1 = phoneToastView.findViewById(R.id.text1);
  textView1.setText("Some text on one line");
  TextView textView2 = phoneToastView.findViewById(R.id.text2);
  textView2.setText("Some more text on one line");
  TextView textView3 = phoneToastView.findViewById(R.id.text3);
  textView3.setText("Even more text, and this text is much longer, not fitting on one line at all.");
  
  // If I don't need the unpredictable third line of text, hide the view
  textView3.setVisibility(View.GONE);
  
  Toast toast = new Toast(getContext());
  toast.setGravity(Gravity.BOTTOM | Gravity.FILL_HORIZONTAL, 0, 0);
  toast.setView(view);
  toast.setDuration(Toast.LENGTH_LONG);
  toast.show();
  </code></pre>
  
  <p>For my needs, I will mostly use textviews 1 and 2. Then the toast will be at the bottom of the screen (since they are fixed to have one line of text). In some situations, I cannot predict the length of the text, and then I use textview 3. Then the toast will not be at the bottom, but this is quite rare.</p>