How to use Loading Spinner in Android Studio

180

How to use Loading Spinner in Android Studio: If you want to learn to create a loading spinner in Android Studio is very simple, Just follow the simple steps given below:

Step: 1

First of all, you need to write a progress bar code in XML. it just looks like given below:

 <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"/>

Step: 2

Create a variable of the progress bar in the MainActivity.java file.

ProgressBar progressBar;

Step: 3

After that find the progress bar using findviewbyId. and also you can use visibility to turn on or of the progress bar. Where you want you can define the visibility of the progress bar.

progressBar = findViewById(R.id.progressbar);
//using visibilty gone, the progress bar turn of
progressBar.setVisibility(View.GONE);
//using visibilty visible , the progress bar turn on
progressBar.setVisibility(View.VISIBLE);

Example: How to use Loading Spinner in Android Studio

Write design code in activity_main.xml, and here write the progress bar code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
<TextView
    android:id="@+id/AndroidFreeks"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Android Freeks"
android:textStyle="bold"
    android:fontFamily="monospace"
    android:gravity="center_horizontal"
    android:textSize="28sp"
    android:textColor="#4CAF50"/>
    <TextView
        android:layout_width="match_parent"
        android:id="@+id/title"
        android:layout_height="wrap_content"
        android:text="How to Loader in Android Studio"
        android:layout_below="@id/AndroidFreeks"
        android:layout_margin="5dp"
        android:textSize="20sp"
        android:gravity="center_horizontal"/>
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:backgroundTint="#4CAF50"
        android:text="Start Loading"
        android:textSize="14dp"/>
    <ProgressBar
        android:id="@+id/progressbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true"/>
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:backgroundTint="#4CAF50"
        android:layout_below="@id/progressbar"
        android:text="Stop Loading"
        android:textSize="14dp"/>


</RelativeLayout>

Here we create a variable of progress bar and button and also find this using the findviewbyId. After that by default, the visibility of the progress bar is gone, and set up onClickListner on Button. When you click on the button progress bar is visible and press the second button progress bar visibility is gone.

package com.example.retrofit2example;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {
    Button button, button1;
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.button);
        button1 = findViewById(R.id.button1);
        progressBar = findViewById(R.id.progressbar);
        progressBar.setVisibility(View.GONE);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                progressBar.setVisibility(View.VISIBLE);
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progressBar.setVisibility(View.GONE);
            }
        });

    }
}

Below we define the content of AndroidMainfests.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.retrofit2example">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.LoadingSpinnerExample">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

After following the above code the output looks like that:

How to use Loading Spinner in Android Studio

When clicking on the Star Loading button it looks like, given below:

How to use Loading Spinner in Android Studio

I hope you like this article, How to use Loading Spinner in Android Studio. If you have any queries related to android development you connect with me through comment and I will reply to you as soon as possible.

LEAVE A REPLY

Please enter your comment!
Please enter your name here