6

I have an onClickListener that triggers a network call so I would like to have some way to show the user that communications are in progress. The problem I am running into is that I can't seem to throw up a ProgressDialog or change the UI in any way for that matter before the call is made inside the onClick Listener. All of the code works just fine, but the UI changes don't come into effect until after all the code in onClickListener runs.

I was wondering if my problem is simply that an anonymous inner class like an onclicklistener can only update the UI at the end of its run? Or maybe my code is just bad.

Thanks in advance

Below is the code for the onclick Listener :

        relayButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cPanel.throwProgress(NCDTCPRelayActivity.this);

                System.out.println(tvSocketConnection.getText().toString());

                if (relayStatusArray[relayNumber] == 0) 
                {
                    if (cPanel.TurnOnRelay(relayNumber, 1) == false)
                    {
                        changeTitleToRed();
                    }else{
                        changeTitleToGreen();
                    }
                }
                else {
                    if (cPanel.TurnOffRelay(relayNumber, 1) == false){
                        changeTitleToRed();
                    }else{
                        changeTitleToGreen();
                    }
                }
                cPanel.hideProgress(NCDTCPRelayActivity.this);
            }
        });

Here is the code for the throwProgress and hideProgress respectively (these are in a subclass of the activity):

public boolean throwProgress(Context mContext) {
    System.out.println("INSIDE THROWPROGRESS");
    try {
        tempDialog = ProgressDialog.show(mContext, "Connecting", "Connecting", true);
    }
    catch (RuntimeException e) {
        return false;
    }
    return true;
}
public boolean hideProgress(Context mContext) {
    System.out.println("OUTSIDE THROWPROGRESS");
    tempDialog.hide();
    return true;
}

**Edit Here is the new code for the onClickListener that I put the runnable in:

            public void onClick(View v) {
                cPanel.throwProgress(NCDTCPRelayActivity.this);
                System.out.println(tvSocketConnection.getText().toString());

                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        if (relayStatusArray[relayNumber] == 0) 
                        {
                            if (cPanel.TurnOnRelay(relayNumber, 1) == false)
                            {
                                changeTitleToRed();
                            }else{
                                changeTitleToGreen();
                            }
                        }
                        else {
                            if (cPanel.TurnOffRelay(relayNumber, 1) == false){
                                changeTitleToRed();
                            }else{
                                changeTitleToGreen();
                            }
                        }
                        cPanel.hideProgress(NCDTCPRelayActivity.this);
                        relayStatusArray = cPanel.getBankStatus(1);
                        updateButtonText();
                    }

                });
            }

2 Answers 2

3

Changing UI from your click handler should work just fine. The problem is likely that you're doing some heavy work on the UI thread and it's blocking it so that the dialog is not really updated until after all of that work is done. Try moving all the heavy lifting into an AsyncTask (read this doc if you're unfamiliar with it) hiding the dialog when the task is complete and see if that fixes it.

7
  • Well the network call is on an AsyncTask however I use the AsyncTask().execute().get() method so that the ui thread will wait for the response because the whole program is used for getting those network commands to the target and getting back aresponse. Its just a work around so that we don't get an exception of network on main thread. Wouldn't the code before the call be executed before the code after it though?
    – Poodimizer
    Feb 23, 2012 at 22:46
  • Don't call get() in the main thread. Override AsyncTask.onPostExecute() and handle the response there.
    – gngr44
    Feb 23, 2012 at 23:03
  • 1
    @Poodimizer Don't do that! You're effectively defeating the whole purpose of AsyncTask to begin with by turning it into a needlessly elaborate synchronous task!
    – Santa
    Feb 23, 2012 at 23:24
  • For this application it is preferable to have the UI lock up during network communications in case the call doesn't go through. Although I guess it could be an option users can select. I was just wondering about why this UI call is working like it is. Thanks for the idea though.
    – Poodimizer
    Feb 24, 2012 at 4:14
  • No, it's pretty much never preferable to have the UI lock up. You can prevent user interaction by making the progress dialog non-cancelable.
    – kabuko
    Feb 24, 2012 at 18:16
3

All the UI updates are delayed on Android, as well as on pretty much every GUI platform out there. Changes to the looks of a view are never rendered right away; instead, the GUI subsystem marks the view as "needs redraw", and calls draw() some time later on the message loop.

If you want something to take place after the screen has been updated, use Handler.post(). The post()'ed code will execute some time on the message loop, typically later than the queued draw code.

Aside node: Windows GUI is a happy exception to that rule; you can draw on a Windows window outside WM_PAINT. But on Android you can't.

1
  • I tried adding a runnable and putting the network call in it, but I still am not getting the throwProgressBar's ProgressDialog showing up. Did I do it correctly? I added a new section in the original post with the runnable in it.
    – Poodimizer
    Feb 23, 2012 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.