Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the onClickListener not update the UI until all of the code inside it has run in Android?

Tags:

java

android

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

                });
            }
like image 297
Poodimizer Avatar asked Feb 23 '12 22:02

Poodimizer


People also ask

Which method is used to set the updates of UI?

Android Thread Updating the UI from a Background Thread The solution is to use the runOnUiThread() method, as it allows you to initiate code execution on the UI thread from a background Thread.

What is the purpose of using OnClickListener?

In Android, the OnClickListener() interface has an onClick(View v) method that is called when the view (component) is clicked. The code for a component's functionality is written inside this method, and the listener is set using the setOnClickListener() method.

Which are the ways to set OnClickListener for button in Android?

To implement View. OnClickListener in your Activity or Fragment, you have to override onClick method on your class. Firstly, link the button in xml layout to java by calling findViewById() method.

Can we set OnClickListener on TextView?

In Android, TextView is a child class of View, and hence can use the method setOnClickListener() on the object of TextView. In this tutorial, we will learn how to set OnClickListener for TextView in Kotlin file, with the help of an example.


2 Answers

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.

like image 142
kabuko Avatar answered Oct 23 '22 05:10

kabuko


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.

like image 24
Seva Alekseyev Avatar answered Oct 23 '22 05:10

Seva Alekseyev