F3RN1
F3RN1

Reputation: 179

Display a message on Android

How I can display a message for a few seconds without using Toast on Android?

For example, if the user has logged in, then I want to show a message like "User logged in successfully" disappears in X seconds.

How I can do?

Upvotes: 1

Views: 4712

Answers (5)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

you can use PopupWindow to show massage just like Toast without blocking Current UI.but you will need to use a thread with runOnUiThread for dismiss PopupWindow after X Seconds.

See example-of-using-popupwindow

http://android-er.blogspot.in/2012/03/example-of-using-popupwindow.html

Upvotes: 0

Yevgeny Simkin
Yevgeny Simkin

Reputation: 28349

If you don't want an "OK" button to dismiss the message, you can put it up as a ProgressDialog and then dismiss it yourself after a couple of seconds like this...

ProgressDialog pd;

pd = ProgressDialog.show(context, "Title", "sub title");//context is probably `this`

Handler h= new Handler();

Runnable cancelDialog = new Runnable(){
   pd.dismiss();

};

h.postDelayed(cancelDialog, 3000);//this will be called in 3 seconds

You can distribute the various calls to whatever methods or button presses are relevant. I'd make the ProgressDialog, the Handler and the Runnable global to your Activity so that you can make those calls from wherever.

I would argue that using the ProgressDialog gives the user a feeling that this is going to go away on its own, otherwise, they're left staring at a prompt that is not "dismissable" and confused as to how to proceed.

Upvotes: 0

s.krueger
s.krueger

Reputation: 1043

final Handler handler = new Handler();
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("FooBar !");
final AlertDialog dialog = builder.create();
dialog.show();
handler.postDelayed(new Runnable() {
  public void run() {
    dialog.dismiss();    
  }
}, 3000); // Dismiss in 3 Seconds

Upvotes: 2

Goofy
Goofy

Reputation: 6128

try this...

while(needToDisplayData)
{
    displayData(); // display the data
    Thread.sleep(10000); // sleep for 10 seconds
}

Alternately you can use a Timer:

int delay = 1000; // delay for 1 sec. 
int period = 10000; // repeat every 10 sec. 
Timer timer = new Timer(); 
timer.scheduleAtFixedRate(new TimerTask() 
    { 
        public void run() 
        { 
            displayData();  // display the data
        } 
    }, delay, period); 

in the displayData(); method u can use a dialog.

Upvotes: 0

Related Questions