Search code examples
javaandroidxmlandroid-studioandroid-layout

How to make AlertDialog full screen in android


I know this question has been asked before but I really can't get it, I want my alert dialog to take full screen but instead it just keeps showing my app in the background, I have tried many things before posting here, so please if you know how can this be done share your answer.

Loading.java

        class LoadingDialog {
    
       private Activity activity;
       private AlertDialog dialog;
    
    
          LoadingDialog(MainActivity myActivity){
             activity = myActivity;
    
    
        }
    
        void hello(){
    
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            LayoutInflater inflater = activity.getLayoutInflater();
            builder.setView(inflater.inflate(R.layout.custom_dialog, null));
            builder.setCancelable(false);
    
            dialog = builder.create();
            dialog.show();
    
    
        }
    void DismissDialog(){
        dialog.dismiss();
    }
    void dismiss() { dialog.dismiss(); }

}

custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white"

    >

    <!--<view-->
    <!-- android:layout_width="match_parent"-->
    <!-- android:layout_height="match_parent" >-->
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/white"
        android:orientation="vertical">
        <com.airbnb.lottie.LottieAnimationView
            android:id="@+id/animationView"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_marginTop="100dp"
            android:padding="40dp"

            app:lottie_autoPlay="true"
            app:lottie_loop="true"
            app:lottie_rawRes="@raw/loading1" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:fontFamily="Arial"
            android:textAlignment="center"
            android:layout_marginRight="30dp"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="20dp"

            android:text="Loading..."
            android:textColor="#196bbf"
            android:textSize="18sp"
            android:textStyle="bold"
            />
    </LinearLayout>
</RelativeLayout>

How I call it in Main :

final LoadingDialog loadingDialog = new LoadingDialog(MainActivity.this);

please can you tell me what am I missing ? please help me in this.


Solution

  • To make Alert Dialog fit screen width and height but Actionbar is visible use below code :

        void hello(){
    
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        LayoutInflater inflater = activity.getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.custom_dialog_, null));
        builder.setCancelable(false);
        dialog = builder.show();
    
        dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT);
    
    }
    

    And if you want to hide the Actionbar also, then first put below code in your styles.xml file

        <style name="myFullscreenAlertDialogStyle" parent="Theme.AppCompat.Light.NoActionBar">      //no actionbar, but status bar exists
        <item name="android:windowFullscreen">true</item>                       //remove status bar
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>        //smooth animation
        <item name="colorAccent">@color/colorAccent</item>                      //change button text color
        </style>
    

    And use this style as below:

        void hello(){
    
        AlertDialog.Builder builder = new AlertDialog.Builder(activity,R.style.myFullscreenAlertDialogStyle);
        LayoutInflater inflater = activity.getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.custom_dialog_, null));
        builder.setCancelable(false);
        dialog = builder.show();
    
        //dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT);
    
       }