Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7537907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:58:31+00:00 2026-05-30T06:58:31+00:00

I have an onClickListener that triggers a network call so I would like to

  • 0

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

                });
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:58:33+00:00Added an answer on May 30, 2026 at 6:58 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an onClickListener that should call a class that shows and image from
I have read about problems that onClickListener will consume onItemClickListener Events like here or
I have a problem concerning setting a listener that would execute different action based
Lets say that I have this code moveDirectory.setOnClickListener(new OnClickListener(){ public void onClick(View v) {
In my view, I have a search EditText and I would like to trigger
I have a listView and a onClickListener that onClick do som things and it
i have questions that i am really need some help on it: i develop
I have a private onClickListener class with a switch statement for everybutton Some buttons,
I would like to have an EditText with one modification: on the right but
I have an EditText that shows time. After user clicks the EditText I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.