How to create a Toast from a background operation?

by Mariano Kamp » Sat, 02 May 2009 01:40:40 GMT

Sponsored Links
  what is a good way to signal an error from a thread that is not the UI
thread and you don't know which activity/handler is currently active? Can I
somehow get eleto the current UI thread?

  Can I somehow use the MainLooper from the application context?

  I use notification for serious events where the user needs to take action,
e.g. a login failed, but here I am looking for a transient notification with
a toast and it would be ok if the toast is not seen in some cases.



How to create a Toast from a background operation?

by Mark Murphy » Sat, 02 May 2009 01:44:40 GMT


Does it matter?

I have not tried to have a background thread use a Handler or
runOnUiThread() of an Activity that might not be active. It is entirely
possible "it just works", and so you can use whatever Activity you have

Mark Murphy (a Commons Guy)  | 

Android App Developer Books: 


Sponsored Links

How to create a Toast from a background operation?

by Mark Murphy » Sat, 02 May 2009 02:07:26 GMT


It looks like you are trying to manually instantiate a Handler, or
possibly an Activity, in the background thread. If so, that is not the
proper way to use a Handler. This is one of the reasons I dislike
Handler, preferring runOnUiThread() (and hopefully the new AsyncTask).

Doubtful. Otherwise, AsyncTask couldn't work.

Can you use AsyncTask for this? The result of your background work would
 be passed to onPostExecute(), which could then display an error on the
UI thread if something failed in the background thread.

Mark Murphy (a Commons Guy)  | 

Android App Developer Books: 


How to create a Toast from a background operation?

by Mark Murphy » Sun, 03 May 2009 06:08:37 GMT


Why not keep a log of failures and display it to the user when they next
launch your application? Or at least have that as an option.

I agree with Raphael -- popping up Toasts in the background would be
annoying to me as a user.

Mark Murphy (a Commons Guy)  | 

Warescription: Three Android Books, Plus Updates, $35/Year


Other Threads

1. GridView vs. horizontal scroll


currently I'm working on an activity which should display some data in
a GridView provided by a sqlite database. The table contains many
columns, so I'm trying to make the GridView horizontally scrollable,
so the user could easily navigate through the fields. I'he already
tried to use HorizontalScrollView, as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
        android:stretchMode="none"  />

However, the solution didn't work, on the emulator only one column
showed up, I couldn't figure it out why. Does GridView support
HorizontalScrollView anyway?

After that, I've tried to implement the horizontal scrolling
programmatically, using the GridView.scrollBy() method:

public class CustomGridView extends GridView {


        public boolean onKeyDown(int keyCode, KeyEvent event) {
                if(keyCode==KeyEvent.KEYCODE_DPAD_RIGHT) {
                        this.scrollBy(100, 0);
                else if(keyCode==KeyEvent.KEYCODE_DPAD_LEFT) {
                        this.scrollBy(-100, 0);

                return super.onKeyDown(keyCode, event);

In that case, I could navigate between the columns, but It's seems to
me, that the scrolling and the onItemClick method doesn't work
together properly: after scrolling, I've tried to click on a View in
the Grid, but the onItemClick method always received wrong parameters.
I suppose, the GridView doesn't refresh the value of the
mFirstPosition (the position of the first child displayed) variable.
Or have I done something wrong?

Any help on this topic would be very useful.



2. Waiting Dialog?

Hi Guys,

I got the following prob:
-I have to implement an interface
-in most methods its like "show dialog and get the selected result"
-afaik in android there is no way to wait for such a result

so... that's the base;

[syntax="java"]public int showDlg()
        int iSelectedChoice = -1;

        //show Dlg

        //wait for Dlg to be finished (ok or cancel is clicked)

        return iSelectedChoice;     //return selected choice

and my current implementation:

[syntax="java"]public int AskQuestion(String[] choices, String
strQuestion, int preSelect)
        Dialog oDlg = null;
        Builder oBuilder = new AlertDialog.Builder(this);
        oBuilder.setSingleChoiceItems(choices, 0, this);
        oBuilder.setPositiveButton("ok", this);
        oBuilder.setNegativeButton("cancel", this);

        oDlg = oBuilder.create();;

        while(m_fBool == false) //m_fBool is set true when a button is
clicked (in the onClicklistener)
            catch (InterruptedException e)

        return m_iResult;

In my implementation I can't see the Dialog anyway =(
Please help me, its urgent!!!


3. Root Access for Unlocked MyTouch 3G

4. RuntimeException -

5. Any updates on ADC-2?

6. ADC 2 - Can I post my app to the app store after August 1st?

7. sdk for windows from source code