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);
}
}