Android Development

How to use Loading Spinner in Android Studio

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:

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

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.

naimishgupta

Recent Posts

How to Create Custom Toast Android with Example

In custom toast Android, You are able to design a toast message according to your… Read More

2 months ago

How to use Toast on Android

Toast is a method, With the help of toast you can display a message that… Read More

2 months ago

How To Update Drivers On Windows 10

Update Drivers On Windows 10 - For programs or hardware accessories to work properly with… Read More

3 months ago

Windows 11 release date official, specs, features, and all you need to know

Windows 11 release date- Windows 10 has been released for quite a long time now,… Read More

4 months ago

NVIDIA & Windows 10 (How I Solved the Code 43 Error)

The display of the error code 43 is often found in the Device status box… Read More

5 months ago

Forza Horizon 5 on Xbox Series and PC: Release Date, Location, & More

Forza Horizon is the outstanding companion to the mythical Xbox Forza racing franchise. Though the… Read More

6 months ago

This website uses cookies.