7

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.

Alex K
  • 8,269
  • 9
  • 39
  • 57
Olileo
  • 83
  • 1
  • 3
  • 1
    create a framelayout in your xml file and add a progressbar and your button that you click.. then in your onclicklistener toggle the visibility of your progressbar and button, by` setVisibility(View.Visible)` for your progressbar and invisible for you button, thats all – Elltz Jan 22 '15 at 03:15
  • 1
    @Elltz, that sounds like an answer. – Nathan Tuggy Jan 22 '15 at 03:29
  • Thanks a lot for your answer, is it possible to have a code example ? Sorry but i'm very new to this – Olileo Jan 22 '15 at 04:11

2 Answers2

3
<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
}
Elltz
  • 10,730
  • 4
  • 31
  • 59
  • Thanks a LOT Eltz for your time and answer. In the future, I will do more research and learn more to hopefully help a beginner :) – Olileo Jan 22 '15 at 04:29
  • okay, Sir,but check it, run it, first, if it works then you accept it.@Olileo – Elltz Jan 22 '15 at 04:34
  • Thanks, it WORKS, but the progress bar freeze and I don't know why ... Furthermore, when i'm into Activity2 if I get back to Activity1 with the "previous button", it shows my progress bar moving infinitly. I don't know if I made an error ? Thanks again – Olileo Jan 22 '15 at 05:39
  • @Olileo Sir, are you using asynctask? if not you might maybe want to try or try threads they are very easy, the reason of the lag or freeze might be you are doing some hard work which tends to like freeze the progressBar, you know your app runs on a thread called UI thread or main thread, broadcast receiver anything the os does to help your app work is done there, so if you want to do some more work, you create a diff or work path and bring back to the ui thread when you are done, to prove what im saying when you click the button it flows then freeze but when you come back does it freeze? – Elltz Jan 22 '15 at 12:19
  • @Eltz Sorry, I didn't see the last comment. When the ProgressBar shows first, it freeze, then slightly move... And I have my Activity2. When I Come back to Activity1, ProgressBar move perfectly. How can I use asynctask as you say ? – Olileo Jan 22 '15 at 17:17
  • yea,Sir, try [1](http://developer.android.com/reference/android/os/AsyncTask.html) , [2](http://stackoverflow.com/questions/9671546/asynctask-android-example) , [3](http://www.compiletimeerror.com/2013/01/why-and-how-to-use-asynctask.html), [google search](https://www.google.com/search?q=android+asnyctask&ie=utf-8&oe=utf-8#q=android+how+to+use+asnyctask) @Olileo – Elltz Jan 22 '15 at 17:38
  • Thanks Again Eltz to help a Beginner like me. I'll try and if it Work, i'll post the final solution here. Bye – Olileo Jan 22 '15 at 17:56
1

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.

cyanide
  • 3,885
  • 3
  • 25
  • 33
  • Thanks a lot. I add public static Toast and import toast widget, where have I to put the code after .......... as you said ? Thanks – Olileo Jan 22 '15 at 04:04