Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show "Loading" bar between two activities in Android

I'm new to Android app programming. I want to show a "loading" status or bar/circle when clicking a button in my FIRST activity to go to the SECOND activity after fully loaded.

At the moment, I have my two activities but it take a while to load the second one and I want that bar/circle to help the user to understand that there is something loading and that the application hasn't just crashed.

I've searched and found many topics about retrieving external data like url and media from web with asynctask but I think this is not my case: I just need a very simple message when clicking my button just before going to the second activity.

like image 976
Olileo Avatar asked Jan 22 '15 03:01

Olileo


2 Answers

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleLarge"
    android:layout_gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:visibility="gone"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_height="wrap_content"
    android:text="Click Me" />

</FrameLayout>

then in your onclick listener of your button

ProgressBar p = (ProgressBar)findViewById(R.id.progressBar1);
if(p.getVisibility() != 0){ // check if it is visible
   p.setVisibility(0); // if not set it to visible
   arg0.setVisibility(1 or 2); // use 1 or 2 as parameters.. arg0 is the view(your button) from the onclick listener
}

for your second problem.. it happens because you have set the visibility of the button to gone or invisible, if you finished the activity, then when you call it again the os will re-create it, but if you didn't specify to close it then the os will store it in something called backstack, also an activity has lifecycle , more just copy and paste it

@Override
public void onResume() { // this is called by your activity before it gets visible to the user, when you leave this activity 2 and come back to 
     //activity 1, because activity 1 wasn't killed it is resumed from the backstack and this function is called
    // TODO Auto-generated method stub
    super.onResume();
    // so you will check here, if your button is visible or not like the way you did in the onclick for the progressbar
     ProgressBar p = (ProgressBar)findViewById(R.id.progressBar1); 
     Button b = (Button) findViewById(R.id.button1); // if you have already instantiated your button then nevermind
     if(b.getVisibility() != View.VISIBLE){ // check if it is visible
     //View.VISIBLE is an int variable which is 0
     b.setVisibility(View.VISIBLE); // if not set it to visible
     p.setVisibility(View.INVISIBLE or View.GONE); //View.INVISIBLE is 1, and 2 is View.GONE
     // you can either use the ints or the static variables.. gone stands for when the view is invisible and is not accounted for with respect to space on the screen
     // so if you use relativeLayout and you align a view in respect to another if you use gone your viewlayout will be squashed around,
     // invisible is the when its invisible and is being accounted for
}
like image 143
Elltz Avatar answered Sep 27 '22 23:09

Elltz


Show a Toast before starting the second Activity. A toast will work between activities:

In Activity1

 public static Toast transitionToast;

  ..........
  transitionToast.Toast.makeText(this, R.string.loading, Toast.LENGTH_LONG);
  transitionToast.show();
  startActivity(new Intent(this, Activity2.class));

In Activity2:

  public void onStart() {
       super.onStart();
       Activity1.transitionToast.cancel();   // Dismiss the toast     
  }

A bit nicer would be putting transitionToast declaration in a separate file.

like image 22
cyanide Avatar answered Sep 27 '22 22:09

cyanide