Best Way to Show a Loading/Progress Indicator

Best way to show a loading/progress indicator?

ProgressDialog is deprecated from Android Oreo. Use ProgressBar instead

ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Loading");
progress.setMessage("Wait while loading...");
progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
progress.show();
// To dismiss the dialog
progress.dismiss();

OR

ProgressDialog.show(this, "Loading", "Wait while loading...");

Read more here.

By the way, Spinner has a different meaning in Android. (It's like the select dropdown in HTML)

Most efficient way to show a loading spinner - Android

By default a ProgressBar is a circular, indeterminate one (i.e. it doesn't show an amount of progress, it just spins to show activity). Just stick one in your layout with a width and height and that's all you need! You can see some options for customising it (including with your own drawable) on the docs page.

If you like, the Material Components library has its own progress spinners - for those you do need to set indeterminate to true, they're determinate by default.

what is best ways to set loading/progress in full screen in flutter

you can try the following

Have a utils class

    class Utils {
late BuildContext context;

Utils(this.context);

// this is where you would do your fullscreen loading
Future<void> startLoading() async {
return await showDialog<void>(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return const SimpleDialog(
elevation: 0.0,
backgroundColor: Colors.transparent, // can change this to your prefered color
children: <Widget>[
Center(
child: CircularProgressIndicator(),
)
],
);
},
);
}

Future<void> stopLoading() async {
Navigator.of(context).pop();
}
Future<void> showError(Object? error) async {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
action: SnackBarAction(
label: 'Dismiss',
onPressed: () {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
},
),
backgroundColor: Colors.red,
content: Text(handleError(error)),
),
);
}
}

Then use it where you need loading

ElevatedButton(
onPressed: () async {

FocusScope.of(context).unfocus();
if (widget.formkey!.currentState!.validate()) {
Utils(context).startLoading();
widget.formkey!.currentState!.save();
widget.authProvider
.signInWithEmailAndPassword(
widget.textController.text.trim(),
widget.passwordController.text.trim())
.then((user) async {
// do something with user
Utils(context).stopLoading();
}).onError(
(error, stackTrace) {
Utils(context).showError(error);
Utils(context).stopLoading();
},
);
}
},
child: const Text(
AppConstants.kBtnLogin,
style: TextStyle(color: Colors.white),
),
)

How to show a progress indicator animation overlayed on current screen in flutter?

Please make a common loader widget

class LoaderTransparent extends StatelessWidget {
double height;
double width;
Color colorValue;
LoaderTransparent({this.colorValue});

@override
Widget build(BuildContext context) {
height = MediaQuery.of(context).size.height;
width = MediaQuery.of(context).size.width;
return Container(
height: height,
width: width,
color: Colors.transparent,
child: Center(
child: SizedBox(
height: 60.0,
width: 60.0,
child:
//Image.asset('assets/images/loader.gif',fit: BoxFit.fill,) // use you custom loader or default loader
CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation(
Colors.blue),
strokeWidth: 5.0))));
}
}

In your screen use like this

Scaffold(
body:
Stack(children: [
Container(
width: width,
child: Column(
children: <Widget>[ // user screen ui

],)
),
true ? LoaderTransparent() : Container() // true or false conditions according loader show or hide
])
);

Show a progress bar when an Activity is loading

If you have long operations you should not be doing them in onCreate in any case as this will freeze the UI (whether or not the activity is displayed). The UI set by onCreate will not appear and the UI will be unresponsive until after the onCreate call finishes.

It seems you can start your second activity and display a progress bar (or requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);), then start an ASyncTask which will be responsible for updating your UI once data has been retrieved.

How to show loading progress or spinner in the middle of the screen with React Native?

So in your case you can do several things

  1. You can use React Native Activity Indicator -> View
  2. You can use Overlay Library -> react-native-loading-spinner-overlay -> View GitHub
  3. If you like to make loading like facebook / instagram -> then use react-native-easy-content-loader -> View GitHub

Assume that you are using React Native Activity Indicator :

import { ActivityIndicator } from "react-native";

export default class HomeScreen extends Component {
constructor(props) {
super(props);
this.state = {
isLoading: true
};
}

//Get Home Screen Data API Action
componentDidMount() {
this.loadAPI(); // Call home screen get data API function
}

//Login API Function
loadAPI = () => {
this.setState({ isLoading: true }); // Once You Call the API Action loading will be true
fetch(API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json"
}
})
.then(response => response.json())
.then(responseText => {
// You can do anything accroding to your API response
this.setState({ isLoading: false }); // After getting response make loading to false
})
.catch(error => {});
};

render() {
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
{this.state.isLoading && <ActivityIndicator color={"#fff"} />}
</View>
);
}
}
  • If you want to hide all the view until loading finish like images, so you can use custom library instead of Activity Indicator.

How to show a progress Dialog before data loading in flutter?

Use a FutureBuilder to control the rendering during the load time;

  final categories = Webservice().load(Category.allCategory);

Widget build(BuildContext context) {
return FutureBuilder(
future: categories,
builder: (ctx, snapshot) {
var value = (snapshot.connectionState == ConnectionState.done) ? '${_category.catToDo}' : '0';

return Text(
value,
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold
),
);
}
);
}

Or if you want to display a loading animation :

  final categories = Webservice().load(Category.allCategory);

Widget build(BuildContext context) {
return FutureBuilder(
future: categories,
builder: (ctx, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Text(
'${_category.catToDo}',
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold
),
);
}
else {
return CircularProgressIndicator();
}
}
);
}

Display progress bar while loading

The mistake you are doing here is you are dumping specific time into your code
You never know how much it will take to get response.
You should follow following approach

Step 1 Show progress dialog on screen

Step 2 Let download take its own time.But it should be done in new thread

Step 3 Once download is complete it will raise message that task is done,now remove that
progress dialog and proceed.

I am pasting sample code here.Hope it will help you.

package com.android.myApps;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class MainScr extends Activity
{
private Handler handler;
private ProgressDialog progress;
private Context context;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = AncActivity.this;
progress = new ProgressDialog(this);
progress.setTitle("Please Wait!!");
progress.setMessage("Wait!!");
progress.setCancelable(false);
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);

handler = new Handler()
{

@Override
public void handleMessage(Message msg)
{
progress.dismiss();
Intent mainIntent = new Intent(context, Category.class);
startActivity(mainIntent);
super.handleMessage(msg);
}

};
progress.show();
new Thread()
{
public void run()
{
// Write Your Downloading logic here
// at the end write this.
handler.sendEmptyMessage(0);
}

}.start();

}

}


Related Topics



Leave a reply



Submit