+ Reply to Thread
Results 1 to 2 of 2

Thread: Problems setting visibility in Android application

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    14

    Problems setting visibility in Android application

    I'm writing an Android application which requires the user to login. I have a "username" and "password" EditText fields and also a "Submit" button.

    When testing in the emulator (and on a phone), the app appears to 'lock up' when the user hits "Submit". It hasn't actually crashed or anything like that, it's just locked whilst the connection to the server is made (and then it advances the user to the next view if their login is correct).

    This isn't very cosmetically pleasing as some users may think the app has locked up if their connection is slow and the login doesn't process for a few seconds..

    To avoid this I want to have a ProgressBar which will appear just to let the user know something is happening and the app hasn't locked up!

    Here's my code:
    Code:
    private OnClickListener listenerOne = new OnClickListener() {
        public void onClick(View v) {
            Button submitButton = (Button)findViewById(R.id.submit);
            submitButton.setVisibility(8); //Make submit button invisible
    
            ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressbar);
            progressBar.setVisibility(0); //Make progress bar visible. It's in the same position as the submit button
    
            login(); // do the login server stuff
                    // the problem is that thenew visibility doesn't happen until the login() is called...
        }
    };
    I've ordered the code so that it makes the submit invisible, then makes the progress bar visible, then does the login (so the progress bar will be there twirling around whilst the app connects to the login server). However, it's not working out how I intended - it's seemingly skipping over the setVisibility code and just calling the login method. It must be setting the visibility of the submit and progress bar items but it doesn't happen before the login method does its stuff as it just locks up like usual and the items never actually get hidden/shown until the login method has completed!!

    Any help would be much appreciated. Sorry for the essay!

  2. #2
    Junior Member
    Join Date
    Jul 2012
    Posts
    12
    The login part is still taking over the UI thread despite being "after" your UI elements.

    You should try running it in a separate thread or AsyncTask. Try this for starters (replace your login() call with it), then make it prettier if it works:
    Code:
        new Thread(new Runnable() {
            public void run() {
                login();
            }
        }).start();
    Any particular reason you are using the actual ints as opposed to the flags available for setting visibility? Seems like it would be easier to read like this:
    Code:
    progressBar.setVisibility(ProgressBar.VISIBLE);

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts