• 12
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

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...

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);

}

}

Thanks!

      • 2
    • There is no documented API to display a Toast for any length other than long or short. There may be bugs/hacks that work on specific versions of Android, but nothing is sure to work generally.

You can achieve it like this

private Toast mToastToShow;
public void showToast(View view) {
   // Set the toast and duration
   int toastDurationInMilliSeconds = 10000;
   mToastToShow = Toast.makeText(this, "Hello world, I am a toast.", Toast.LENGTH_LONG);

   // Set the countdown to display the toast
   CountDownTimer toastCountDown;
   toastCountDown = new CountDownTimer(toastDurationInMilliSeconds, 1000 /*Tick duration*/) {
      public void onTick(long millisUntilFinished) {
         mToastToShow.show();
      }
      public void onFinish() {
         mToastToShow.cancel();
         }
   };

   // Show the toast and starts the countdown
   mToastToShow.show();
   toastCountDown.start();
}

You can set it to your custom view

  • 1
Reply Report

Instead of using Toast.LENGTH_LONG or Toast.LENGTH_SHORT, just enter an int, it's looking for an int. I believe it's in milliseconds, so if you use 1000, it would be 1000 milliseconds

  • 0
Reply Report
      • 2
    • This may work on some versions of the OS, but its undocumented behavior. The documented behavior of setDuration is that it takes LONG or SHORT, not a number of ms
    • Thanks for your response CCT. I tried myToast.setDuration(1000); like you said. However, I got a red line under 1000 and here is the message it gave me."Must be one of: Toast.LENGTH_SHORT, Toast.LENGTH_LONG less... (Ctrl+F1) Inspection info:Ensures that when parameter in a method only allows a specific set of constants, calls obey those rules. Issue id: WrongConstant"