Question

In my android application I have a MainActivity. It has an EditText and a Button. User enter his twitter handle and press button. An IntentService is launched which retrieves user's tweets and then return the first tweet to a BroadcastReceiver.

Since loading tweets takes time I want to show a loader until loading tweets is done.

I'm using following code in button click listener to show the loading dialog

ProgressDialog progress = new ProgressDialog(MainActivity.this);
progress.setTitle("Loading");
progress.setMessage("Wait while loading...");
progress.show();

But I don't know how to hide this once the loading is done.

Below are the code of the MainActivity , IntentService and BroadcastReceiver

MainActivity

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    analyze = (Button)findViewById(R.id.analyze);
    twitter_username = (EditText)findViewById(R.id.twitter_username);
    analyze.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            ProgressDialog progress = new ProgressDialog(MainActivity.this);
            progress.setTitle("Loading");
            progress.setMessage("Wait while loading...");
            progress.show();
            Intent i = new Intent(MainActivity.this , TwitterChecker.class);
            i.putExtra("username", twitter_username.getText().toString());
            startService(i);

        }});


}

IntentService

public class TwitterChecker extends IntentService {
    public TwitterChecker(){
        super("TwitterChecker");
    }

@Override
protected void onHandleIntent(Intent intent) {
    String username = intent.getStringExtra("username");
    TwitterAuthenticator authenticator = TwitterAuthenticator.getInstance();
    String accessToken = null;
    try {
        accessToken = authenticator.authenticate();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (AuthenticationException e) {
        e.printStackTrace();
    }
    Twitter tweets = FetchTweets.fetch(accessToken , username);
    Log.i("Info" , "IntentService started");
    Intent tweet = new Intent("com.kaysush.action.TWEET");
    tweet.putExtra("tweet", tweets.get(0).getText());
    sendBroadcast(tweet); // Once loaded the tweet is sent to the Receiver

}

}

BraodcastReceiver public class TweetsReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.i("TWEET RECEIVED" , intent.getStringExtra("tweet"));
             //Once loading is done a toast is shown
     Toast.makeText(context, intent.getStringExtra("tweet"), Toast.LENGTH_LONG).show();
}

}

Was it helpful?

Solution

Hide a ProgressDialog once data is loaded

You are able to do it but you need to use not static but dynamic BroadcastReceiver. So here is solution:

At first, register in your Activity BroadcastReceiver dynamically:

private void registerReceiver() {
   receiver = new BroadcastReceiver() {

      @Override
      public void onReceive(Context context, Intent intent) {
         if (intent.getAction().equals(Const.LOADING_COMPLETE_ACTION)) {
            if (dlg != null) {
               dlg.dismiss();
            }
         }
      }
   }

   IntentFilter intentFilter = new IntentFilter();
   intentFilter.addAction(Const.LOADING_COMPLETE_ACTION);

   registerReceiver(receiver, intentFilter);
}

And then in your IntentService, all what you need is to send Broadcast:

sendBroadcast(new Intent(Const.LOADING_COMPLETE_ACTION));

Note: Also define your ProgressDialog variable on Activity scope to have access to it from onReceive() method.

OTHER TIPS

ProgressDialog progress;  // Make global

Assign onClick listener

progress = new ProgressDialog(MainActivity.this);

Then in call back method onReceive()

progress.cancel();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top