2

I'm trying to stop the progress bar after getting response from server. Below is the xml code that I've used

activity.xml

  <ProgressBar
        android:layout_weight="1"
        android:id="@+id/progressBar"
        android:indeterminate="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="40dp"
        android:minWidth="40dp"
        tools:ignore="MissingConstraints" />

activity.kt

 var progressBar =  findViewById(R.id.progressBar) as ProgressBar
 doAsync {
            try {
                    progressBar.setVisibility(View.VISIBLE);
                    appList = getAppList(requestBody, LAUNCHER_BASE_URL)
                    Log.d("App list =", appList.toString())
                    appInfoList = populateList()
                    progressBar.setVisibility(View.GONE);
            } catch (e: Exception) {
                progressBar.setVisibility(View.GONE);
                Log.e("Exception-launcher", e.toString())
            }

From the above code,progress bar is not stopping even after getting the response. Any help would be appreciated

I've tried that but the page becomes blank after the progress bar stops

Christina Varghese
  • 179
  • 1
  • 2
  • 13
  • Because your progress bar is gone while it throw some exception not that time when you get success response. – Piyush Apr 03 '19 at 07:02
  • so you wanted to show the response to user after progressbar stop ? and Your display becomes blank after progress bar gone. It means you didn't attached your response to your design. – Ashish Apr 03 '19 at 07:26
  • 1
    Possible duplicate of [Android - Can't hide progress bar](https://stackoverflow.com/questions/20565250/android-cant-hide-progress-bar) – Manoj Perumarath Apr 03 '19 at 07:30

4 Answers4

3

Add progressBar.setVisibility(View.INVISIBLE); in your try clause too, because in its current state, it will only remove the loading for when the API fails:

 var progressBar =  findViewById(R.id.progressBar) as ProgressBar
 doAsync {
            try {
                    progressBar.setVisibility(View.VISIBLE);
                    appList = getAppList(requestBody, LAUNCHER_BASE_URL)
                    Log.d("App list =", appList.toString())
                    appInfoList = populateList()
                progressBar.setVisibility(View.INVISIBLE);
            } catch (e: Exception) {
                progressBar.setVisibility(View.INVISIBLE);
                Log.e("Exception-launcher", e.toString())
            }
Adib Faramarzi
  • 3,798
  • 3
  • 29
  • 44
1

try below code

doAsync {
    try {
        progressBar.visibility = View.VISIBLE
        appList = getAppList(requestBody, LAUNCHER_BASE_URL)
        Log.d("App list =", appList.toString())
        appInfoList = populateList()
    } catch (e: Exception) {
        progressBar.visibility = View.GONE
        Log.e("Exception-launcher", e.toString())
    } finally {
        progressBar.visibility = View.GONE
    }
0

You are hiding the progressbar only if the code goes in Exception (catch block).

Move your code after the catch block, as follows:

    try {
         progressBar.setVisibility(View.VISIBLE);
         appList = getAppList(requestBody, LAUNCHER_BASE_URL)
         Log.d("App list =", appList.toString())
         appInfoList = populateList()
    } catch (e: Exception) {
         Log.e("Exception-launcher", e.toString())
    }
    progressBar.setVisibility(View.GONE);

Let me know if that helps

Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66
0

Not really clear how can you handle View inside doAsync Normally this should represents in this way

 val progressBar =  findViewById<ProgressBar>(R.id.progressBar)
 progressBar.visibility = View.VISIBLE
 doAsync {
      try {
         appList = getAppList(requestBody, LAUNCHER_BASE_URL)
         Log.d("App list =", appList.toString())
         appInfoList = populateList()              
      } catch (e: Exception) {
          e.printStackTrace()
      }

      uiThread {
         progressBar.visibility = View.GONE
      }
 }
Stanislav Bondar
  • 6,056
  • 2
  • 34
  • 46