简体   繁体   中英

progress dialog is not showing the spinner in android version 8.0 but it still works

In my app, the progress dialog should show out when the button is clicked. Now the progress dialog is shown but without spinner or the loading bar(sorry I'm not sure what it's call)

This is how my progress dialog looks like in android 8.0 real device

这是我的进度对话框在android 8.0真实设备中的样子

This is how my progress dialog looks like in android 5.0

在此处输入图片说明

  private ProgressDialog progressDialog;

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            progressDialog = new ProgressDialog(Login.this);
    }

         public void loginUser(View v) {
            final String Email = emailText.getText().toString();
            final String password = passwordText.getText().toString();

            progressDialog.setMessage("Logging in....");
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.show();

            if (TextUtils.isEmpty(Email)) {
                emailText.setError("Email is required");
            }
            if (TextUtils.isEmpty(password)) {
                passwordText.setError("password is required");
            }
            if ((!TextUtils.isEmpty(Email)) && (!TextUtils.isEmpty(password))) {
                mAuth.signInWithEmailAndPassword(Email, password)
                        .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                            @Override
                            public void onComplete(@NonNull Task<AuthResult> task) {
                                if (!task.isSuccessful()) {
                                    Toasty.error(getApplicationContext(), "Login Failed",
                                            Toast.LENGTH_SHORT, true).show();
                                    progressDialog.dismiss();
                                } else {
      Toasty.success(getApplicationContext(), "Login Success",
                                            Toast.LENGTH_SHORT, true).show();
                                    Intent intent = new Intent(Login.this, Admins.class);
                                    intent.putExtra("Email", emailText.getText().toString());
                                    startActivity(intent);
                  }
           }
     }

Can anyone help me to solve this?

ProgressDialog

is deprecated from SDK 26. You should use

ProgressBar

Usage:

In your layout add the view:

<ProgressBar
   android:id="@+id/progressBar"
   style="?android:attr/progressBarStyle"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   app:layout_constraintTop_toTopOf="@+id/parent"
   app:layout_constraintBottom_toBottomOf="@+id/parent"
   app:layout_constraintLeft_toLeftOf="parent"
   app:layout_constraintRight_toRightOf="parent"
   android:layout_marginLeft="16dp"
   android:layout_marginRight="16dp"
   android:indeterminate="true"
   android:max="100"
   android:visibility="gone"/>

Declare your progressBar in your activity

ProgressBar progressBar;

In onCreate get the view

progressBar = findViewById(R.id.progressBar);

When you need to show the progressBar:

progressBar.setVisibility(View.VISIBLE);

When you need it to be gone:

progressBar.setVisibility(View.GONE);

For more details take a look into ProgressBar docs

It could be helpful to disable user interaction while ProgressBar is up.

EDIT

Disabling interaction

getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
            WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

Reenabling interaction:

getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

EDIT 2

You don't need to copy ProgressDialog visual as per Material Design patterns ( Progress Indicators ).

EDIT 3

Here is an example for what I said to you in comments. It is a layout for a ProgressBar inside a View

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_margin="16dp"
        android:visibility="visible"
        android:background="#EFEFEF">

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Things to do"
            android:textSize="30sp"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"/>

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:indeterminate="true"
            android:max="100"
            android:visibility="visible"
            android:layout_below="@+id/title"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Please wait we are doing things..."
            android:layout_below="@+id/title"
            android:layout_toRightOf="@+id/progressBar"
            android:layout_toEndOf="@+id/progressBar"/>
    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

It'll produce something like this: 看起来如何

You can set elevation for API 21 or higher:

android:elevation="10dp"

As you asked in comments, this is just an example, not a recommendation . It's not the most beautiful layout you'll see in life, but it will give you a north.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM