Search code examples
javaandroidandroid-layoutprogressdialogandroid-styles

removing background from progress dialog


I use a custom progress dialog in my app. It's shown in the photo below.I want to edit two things.First, remove that black background that appears behind the circle.Second, increase the volume of the circle and change its color.

enter image description here

This is the style

<style name="CustomDialog" parent="@android:style/Theme.Dialog">
        <item name="android:textColor">#FF4081</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:windowFrame">@android:color/transparent</item>
    </style>

This is my code

dialog = new ProgressDialog(activity.this,R.style.CustomDialog);
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            dialog.show();

Solution

  • ProgressDialog is deprecated. You can create your custom progress dialog using alert dialog.

    public static AlertDialog showProgressBar(Context context){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.layout_custom_progress_bar, null);
        builder.setView(view);
        AlertDialog dialog = builder.create();
        dialog.show();
        return dialog;
    }
    

    You can then instantiate the dialog so you can manually dismiss it. Use it like so.

    AlertDialog dialog = showProgressBa(this); // this will automatically show the custom progress bar
    // to dismiss
    dialog.dismiss();
    

    Here is your custom layout for the progress dialog.

    //layout_custom_progress_bar.xml
    <?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">
    
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    
        <ProgressBar
            android:id="@+id/progress_bar"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginStart="16dp"
            android:indeterminateDrawable="@drawable/progress_bar"
            android:max="100"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
    </android.support.constraint.ConstraintLayout>
    

    Your progress bar drawable. You can change the color by changing the values of android:centerColor android:endColor android:startColor

    // progress_bar
    <?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="200"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="1080" >
    
    <shape
    android:innerRadius="18dp"
    android:shape="ring"
    android:thickness="4dp"
    android:useLevel="false" >
    
    <size
        android:height="48dp"
        android:width="48dp" />
    
    <gradient
        android:centerColor="@color/colorPrimary"
        android:centerY="0.5"
        android:endColor="@color/colorWhite"
        android:startColor="@color/colorPrimary"
        android:type="sweep"
        android:useLevel="false" />
    
    </shape>
    </rotate>