Note that there are some explanatory texts on larger screens.

plurals
  1. PODoes the onClickListener not update the UI until all of the code inside it has run in Android?
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>Thanks in advance</p> <p>Below is the code for the onclick Listener :</p> <pre><code> 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); } }); </code></pre> <p>Here is the code for the throwProgress and hideProgress respectively (these are in a subclass of the activity):</p> <pre><code>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; } </code></pre> <p>**Edit Here is the new code for the onClickListener that I put the runnable in:</p> <pre><code> 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(); } }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload