title: How do I set a custom duration for a custom Toast message?
body: <p>So I am using a xml layout with a Textview to create a custom toast message. Everything works fine except for the custom duration for it. I know that I can set it to either LENGTH_SHORT or LENGTH_LONG, but I want it display longer than LENGTH_LONG. I've been working on figuring this out for hours and I am not sure what I am doing wrong. Basically, I want to set it for how many seconds my custom toast appears on the screen and then it disappears until the toast is called again each time. Here is what I have in my Main Java class...</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  Button b;
  TextView tv;
  Toast myToast;
  
  Handler h;
  Runnable r;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      b = findViewById(R.id.my_toast_button);
  
  
      b.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              myCustomToast(getApplicationContext(), "Hello World!");
          }
      });
  }
  
  private void myCustomToast(final Context context, final String toastMessage) {
      LayoutInflater layoutInflater = getLayoutInflater();
      final View customToastLayout = layoutInflater.inflate(R.layout.my_custom_toast, (ViewGroup) findViewById(R.id.container));
  
      h = new Handler();
      r = new Runnable() {
          @Override
          public void run() {
              tv = customToastLayout.findViewById(R.id.my_custom_toast_tv);
              tv.setText(toastMessage);
              myToast = new Toast(context);
              myToast.setView(customToastLayout);
              myToast.setDuration(Toast.LENGTH_LONG);
              myToast.show();
  
              myCustomToast(context, toastMessage);
          }
      };
  
      h.postDelayed(r, 10000);
  
  }
  
  }
  </code></pre>
  
  <p>Thanks!</p>