Search code examples
javaandroidandroid-studioandroid-alertdialog

custom alert dialog background color


i need a custom alert dialog for show progress. when i run it the alert dialog background color not change to transparent... below i put my code in xml layout and alert dialog code

xml layout :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="@android:color/transparent"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent">

<androidx.cardview.widget.CardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:cardElevation="10dp"
    app:cardCornerRadius="100dp">

        <ProgressBar
            android:id="@+id/progressBar"
            style="@style/Widget.AppCompat.ProgressBar"
            android:layout_width="30dp"
            android:layout_margin="5dp"
            android:layout_height="30dp" />

</androidx.cardview.widget.CardView>

and this is my code in fragment :

                View progress = getLayoutInflater().inflate(R.layout.progress_alert, null);
            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setCancelable(false);
            builder.setView(progress);
            alertDialog = builder.create();
            alertDialog.show();

Solution

  • try to set dialog theme like this in styles.xml

    <style name="dialogTheme" parent="android:Theme.Holo.Dialog">
        <item name="android:windowMinWidthMajor">90%</item>
        <item name="android:windowMinWidthMinor">90%</item>
    </style>
    

    java code

    Dialog dialog = new Dialog(activity, R.style.dialogTheme);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.your_dialog_layout);
    activity.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));