How to display loading message during the execution of my function?

Louis Chabert

Some functions take a long time to execute. I want to make the user wait with a progress bar. Unfortunately, when I run this one, it runs but only at the end of my function and not when I would like. Let me explain, I launch my progress bar with the "sendloading()" method but during my tests this bar is only displayed after the processing of the other functions "copyAssets" or "createOnClicBtnInsert()". Can someone explain to me why?

Here is my code :

 @Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    view.findViewById(R.id.btnInsertDataset2).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(!hasPermissions(getContext(),Permissions)){
                lay_dataset2 = view.findViewById(R.id.lay_dataset2); 
                messagePerm();
            }else {
                sendLoading(); //MY PROGRESS BAR
                btnInsert = view.findViewById(R.id.btnInsertDataset2);
                /** if perm ok -> insert*/
                copyAssets();
                createOnClicBtnInsert();
                view.findViewById(R.id.btnDeleteDataset2).setEnabled(true);
                Toast.makeText(getContext(), "Insert Success", Toast.LENGTH_SHORT).show();
            }


        }
    });
}

Here is my code for the progress bar :

    public void sendLoading() {
    int llPadding = 30;
    LinearLayout ll = new LinearLayout(getContext());
    ll.setOrientation(LinearLayout.HORIZONTAL);
    ll.setPadding(llPadding, llPadding, llPadding, llPadding);
    ll.setGravity(Gravity.CENTER);
    LinearLayout.LayoutParams llParam = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    llParam.gravity = Gravity.CENTER;
    ll.setLayoutParams(llParam);

    ProgressBar progressBar = new ProgressBar(getContext());
    progressBar.setIndeterminate(true);
    progressBar.setPadding(0, 0, llPadding, 0);
    progressBar.setLayoutParams(llParam);

    llParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
    llParam.gravity = Gravity.CENTER;
    TextView tvText = new TextView(getContext());
    tvText.setText("Loading ...");
    tvText.setTextColor(Color.parseColor("#000000"));
    tvText.setTextSize(20);
    tvText.setLayoutParams(llParam);

    ll.addView(progressBar);
    ll.addView(tvText);

    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setCancelable(true);
    builder.setView(ll);

    AlertDialog dialog = builder.create();
    dialog.show();
    Window window = dialog.getWindow();
    if (window != null) {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.copyFrom(dialog.getWindow().getAttributes());
        layoutParams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
        layoutParams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
        dialog.getWindow().setAttributes(layoutParams);
    }

}
Gabe Sechan

Android is an event driven OS that runs on a single main thread. Drawing is an event. That means in order to draw the main thread needs to return to the event loop. Your code does not return to the event loop until after you do all your processing. Thus it won't draw until after the processing is done.

The way to solve this is to use either a Thread or a Kotlin coroutine to do the processing, and allow the main thread to return to the event loop. BTW, if you have long lasting processing you should not be doing it on the main thread anyway- it freezes your UI and makes your app appear non responsive, for the same reason. The main thread should do short quick calculations and respond to OS events only.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Display message during loading

Loading spinner with *ngIf not showing during execution of function

How to display loading message when an iFrame is loading?

How to stop a function during its execution - JavaScript

Problems with loading resources during execution

Screen is not updating during function, won't display loading gif

Asynchronous javascript: How to run a function during the execution of another function

Display a ‘loading’ message while a time consuming function is executed in Flask

Display a ‘loading’ message while a time consuming function is executed in Flask

"loading" message doesn't display while processing a copy function

how display any content in browser during page loading

How to solve Graph execution error during my model training?

why doesn't my javascript function display my message?

How to display message to all users my application?

How to display notice or success message during the import by Convert Adapter?

Check loading time in WebDriver test during execution

Function redefinitions during execution in lisp

How can i display loading/processing message inside DataTable?

How to optimize my loading JQuery function?

How to display message in HTML Tag with PHP in a function

Replies with immediate execution during message handling in ReBus

Display the JVM's ThreadStackSize during the execution of an application

Have .bat display info during execution (of WinSCP)

How to display an animated icon during python function processing in django?

My C array gets corrupted during execution

Loading Message While Function Processes

How to display a loading progress bar when jQuery function is executing?

How to show loading component during lazy loading

PL\SQL How can I improve the execution speed of my function?