How to make a loading bar on Android

Asked 2 years ago, Updated 2 years ago, 24 views

I'm making an app, but I think it'll stop if the screen stays still while I'm loading the image I'd like to mark that the image is being loaded, what should I do?

android

2022-09-22 12:56

1 Answers

On Android, the loading bar is called the progress bar. Define the style for the custom progress bar.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="custom_loading">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowTitleStyle">@null</item>
    <item name="android:windowAnimationStyle">@+android:style/Animation.Dialog</item>
    <item name="android:windowBackground">@+android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
</style>
</resources>

Define the class to create the loading bar.

import android.app.Dialog;
import android.content.Context;
import android.view.ViewGroup.LayoutParams;
import android.widget.ProgressBar;

/**
 * Custom loading class
 * * @author YT
 */
public class CLoading{
    private static Dialog m_loadingDialog = null;

    public static void showLoading(Context context) {

        if (m_loadingDialog == null) {    
            If there is no dialog, make it visible
            m_loadingDialog = new Dialog(context, R.style.custom_loading);
            //Let's create a progress
            ProgressBar pb = new ProgressBar(context);
            LayoutParams params = new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            //Let's include the program in the dialog
            m_loadingDialog.setContentView(pb, params);
            m_loadingDialog.setCancelable(false);
            m_loadingDialog.show();    
        } } else if(!m_loadingDialog.isShowing()){    
            //If you have a dialog and you're in HIDE state, make it visible            
            m_loadingDialog = null;
            m_loadingDialog = new Dialog(context, R.style.custom_loading);
            ProgressBar pb = new ProgressBar(context);
            LayoutParams params = new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            m_loadingDialog.setContentView(pb, params);
            m_loadingDialog.setCancelable(false);
            m_loadingDialog.show();
        }

    }
    public static void hideLoading() {
        if (m_loadingDialog != null) {
            if(m_loadingDialog.isShowing()){
                Don't let it be visible if there's a dialog    
                m_loadingDialog.dismiss();
                m_loadingDialog = null;
            }    
        }
    }
}

Go to the activity to float the progress bar

Context context;

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

    context = this;

    CLoading.showLoading(context);    //show
    //CLoading.hideLoading();        //hide

}

Call CLoading's showLoading method in this way.


2022-09-22 12:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.