简体   繁体   中英

Why Android AlertDialog is not taking full height of screen

I'm learning Android Studio and facing problem with Alertbox. Why alertbox is not transparent full height in background? I have searched about it but all tricks fail. I'm new to android studio so your some explanation will be helpful for me.

Here is my code. Picture at the end is showing the problem in the graphic.

// get prompts.xml view
LayoutInflater li = LayoutInflater.from(this);
View promptsView = li.inflate(R.layout.add_group_popup, null);

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
        this);

// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);

final EditText userInput = (EditText) promptsView
        .findViewById(R.id.editTextDialogUserInput);

// set dialog message
alertDialogBuilder
        .setCancelable(false)
        .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // get user input and set it to result
                        // edit text
                     //   result.setText(userInput.getText());
                    }
                })
        .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        dialog.cancel();
                    }
                });

// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();

add_group_popup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter the Group Name : "
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editTextDialogUserInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <requestFocus />

    </EditText>

</LinearLayout>

在此输入图像描述

I had exactly same problem with this, finally i can solve this by replacing AlertDialog to Dialog .

so the solution is like this:

Dialog dialog = 
    new Dialog(
        this,
        android.R.style.Theme_DeviceDefault_Light_NoActionBar_Fullscreen
    );
dialog.setContentView(R.layout.yourCustomLayoutDialog);

Hope this help..

You can explicitly set the height and width of your dialog to match the screen in onStart

public void onStart() {
  super.onStart();
  DisplayMetrics metrics = getResources().getDisplayMetrics();
  int width = metrics.widthPixels;
  int height = metrics.heightPixels;
  getDialog().getWindow().setLayout(width, height);
}

I saw by accident when I wrote method like that in activity and use it from fragment, it was in full screen.

public AlertDialog showAlert(String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppTheme);
        builder.setMessage(message);
        builder.setPositiveButton("OK", null);
        return builder.show();
}

Set dialog size programmatically using Window manager.

 WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(rate.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
    alertdialog.show();
    alertdialog.getWindow().setAttributes(lp);

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