Skip to content

Instantly share code, notes, and snippets.

@afzafri
Last active September 16, 2018 04:22
Show Gist options
  • Save afzafri/52f1c5c043e68b0a62691fb4548a94f1 to your computer and use it in GitHub Desktop.
Save afzafri/52f1c5c043e68b0a62691fb4548a94f1 to your computer and use it in GitHub Desktop.
Show simple progress bar (circular spinner) for Android App activity

XML codes for Layout file

  • First, include these codes to your activity layout XML file.
  • FrameLayout is used as it can be on top (overlay) other layout components.
  • The background of the FrameLayout is set to Android default background color. You can change it to any color you want, or even transparent.
  • Progress bar (circular spinner) is for showing the animation. Layout gravity set to center to make it center on the page.
<FrameLayout
    android:id="@+id/loadingFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:attr/colorBackground"
    android:visibility="gone">

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="106dp"
        android:layout_height="107dp"
        android:layout_gravity="center" />
</FrameLayout>

Java codes

  • Then, in your activity, define the FrameLayout component.
final FrameLayout loadingFrame = (FrameLayout) findViewById(R.id.loadingFrame);
  • Finally, use setVisibility() to set component to VISIBLE to display the progress bar, or GONE to hide it.
// show
loadingFrame.setVisibility(View.VISIBLE);

// hide
loadingFrame.setVisibility(View.GONE);

Created by

  • Afif Zafri
  • Used for my fyp project, save for future references and share with others
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment