Jeffrey Chou
Jeffrey Chou

Reputation: 331

AlertDialog won't wrap content no matter what I try

Here is my xml file for my dialog

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/main"
android:paddingTop="23dp"
android:background="@android:color/white">

<ImageView
    android:id="@+id/officer"
    android:layout_width="47dp"
    android:layout_height="47dp"
    android:layout_marginLeft="24dp"
    android:background="@drawable/officerman"/>
<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Are you sure you want\nto sign out?"
    android:textSize="15.5dp"
    android:textColor="#1C86FF"
    android:layout_marginTop="4dp"
    android:lineSpacingExtra="2dp"
    android:layout_marginLeft="84dp"/>

<LinearLayout
    android:layout_marginTop="20dp"
    android:layout_width="264dp"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="1"
    android:layout_below="@id/officer">

    <Button
        android:id="@+id/yes"
        android:layout_width="wrap_content"
        android:layout_weight="0.5"
        android:layout_height="42dp"
        android:background="#1C86FF"
        android:text="Yes"
        android:textAllCaps="false"
        android:textSize="15dp"
        android:textColor="@android:color/white"/>

    <Button
        android:id="@+id/no"
        android:layout_width="wrap_content"
        android:layout_weight="0.5"
        android:layout_height="42dp"
        android:background="#0078FF"
        android:textSize="15dp"
        android:textColor="@android:color/white"
        android:textAllCaps="false"
        android:text="No"/>

</LinearLayout>

</RelativeLayout>

Here is my java code

    AlertDialog.Builder build = new AlertDialog.Builder(Login.this);
    View lview = getLayoutInflater().inflate(R.layout.dialog_leave,null);
    build.setView(lview);
    AlertDialog dialog = build.create();
    dialog.show();

The alertdialog size isn't wrapping to my content no matter what I try. It ends up like this. It adds extended space to the right to get it to a certain size.

Click on this to see the image. It adds extended space to the right see to get it to the default alertdialog size

I have tried using getWindow.setLayout(width,height) in pixels, but that is too much of a pain, there must be a better way of doing it.

I tried getWindow.setLayout(Relative.LayoutParams.WRAP_CONTENT, ...) also, and the same thing still happens.

I have also tried using popupwindow. What is the easy fix to this issue?

Upvotes: 11

Views: 12900

Answers (7)

Eddie
Eddie

Reputation: 139

None of the above wrapped the content for me. Here's what worked for me.

I added this style in the styles.xml

<style name="WrapContentDialog" parent="Theme.AppCompat.Light.Dialog">
    <item name="windowMinWidthMajor">0%</item>
    <item name="windowMinWidthMinor">0%</item>
</style>

And then I set it to the dialog like this.

AlertDialog.Builder builder = new AlertDialog.Builder(getContext(), R.style.WrapContentDialog);

Hope it works for you too.

Upvotes: 12

saulmm
saulmm

Reputation: 2438

val dialog = AlertDialog.Builder(context)
    .setView(myView)
    .create()


dialog.setOnShowListener {
    dialog.window?.setLayout(
        myView.width,
        ViewGroup.LayoutParams.WRAP_CONTENT
    )
}

dialog.show()


Upvotes: 0

Cochi
Cochi

Reputation: 2209

I think you should defined the width major and minor in your xml:

windowFixedWidthMajor : A fixed width for the window along the major axis of the screen, that is, when in landscape.

windowFixedWidthMinor : A fixed width for the window along the minor axis of the screen, that is, when in portrait.

For example in your style.xml :

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        // your relevant item
        <item name="alertDialogTheme">@style/DialogTheme</item>
    </style>

    <style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
        <item name="windowFixedWidthMajor">90%</item> 
        <item name="windowFixedWidthMinor">90%</item>  // this should fix your issue
    </style> 

 // your other styles 
</resources>  

Hope this helps. Sorry for my english.

Upvotes: 3

Ajeet Singh
Ajeet Singh

Reputation: 2009

You are doing everything right,just change you xml layout to:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#33000000"
    android:gravity="center">

<RelativeLayout
    android:id="@+id/main"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:paddingTop="23dp">

    <ImageView
        android:id="@+id/officer"
        android:layout_width="47dp"
        android:layout_height="47dp"
        android:layout_marginLeft="24dp"
    android:background="@drawable/officerman"/>
    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="84dp"
        android:layout_marginTop="4dp"
        android:lineSpacingExtra="2dp"
        android:text="Are you sure you want\nto sign out?"
        android:textColor="#1C86FF"
        android:textSize="15.5dp" />

    <LinearLayout
        android:layout_width="264dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/officer"
        android:layout_marginTop="20dp"
        android:orientation="horizontal"
        android:weightSum="1">

        <Button
            android:id="@+id/yes"
            android:layout_width="wrap_content"
            android:layout_height="42dp"
            android:layout_weight="0.5"
            android:background="#1C86FF"
            android:text="Yes"
            android:textAllCaps="false"
            android:textColor="@android:color/white"
            android:textSize="15dp" />

        <Button
            android:id="@+id/no"
            android:layout_width="wrap_content"
            android:layout_height="42dp"
            android:layout_weight="0.5"
            android:background="#0078FF"
            android:text="No"
            android:textAllCaps="false"
            android:textColor="@android:color/white"
            android:textSize="15dp" />

    </LinearLayout>

</RelativeLayout>
</LinearLayout>

And Java code to:

final Dialog dialog = new Dialog(Login.this, android.R.style.Theme_Translucent_NoTitleBar);
   //dialog.setCancelable(false);
   View view = LayoutInflater.from(Login.this).inflate(R.layout.dialog_leave, null);
   dialog.setContentView(view);
   dialog.show();

It will works.

Upvotes: 1

AskNilesh
AskNilesh

Reputation: 69689

try this my friend

 AlertDialog.Builder build = new AlertDialog.Builder(Login.this);
    LayoutInflater mlLayoutInflater=LayoutInflater.from(MainActivity.this);
    final  View dialView=mlLayoutInflater.inflate(R.layout.R.layout.dialog_leave,null);
    AlertDialog dialog_card = build.create();
    Window window = dialog_card.getWindow();
    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
    window.setGravity(Gravity.CENTER);
    build.show();

Upvotes: 5

Abhishek Tyagi
Abhishek Tyagi

Reputation: 142

Try setting width of buttons to 0dp. Also, try using tools -> android -> layout inspector on your activity. It might give you better insight on what's happening

Upvotes: 0

Ashwin S Ashok
Ashwin S Ashok

Reputation: 3663

Try Using DialogFragment

public class DialogChangePasswordFragment extends DialogFragment {
private Listener dialogButtonClick;
private EditText editOldPassword;
private EditText editNewPassword;
private EditText editConfirmPassword;

public static DialogChangePasswordFragment newInstance() {
    DialogChangePasswordFragment fragment = new DialogChangePasswordFragment();
    return fragment;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try {
        dialogButtonClick = (Listener) context;
    } catch (Exception e) {
        dialogButtonClick = (Listener) getTargetFragment();
        e.printStackTrace();
    }

}

@Override
public void onResume() {
    super.onResume();
    getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}

@Override
public void onDetach() {
    super.onDetach();
    dialogButtonClick = null;
}

@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);
    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    return dialog;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.dialog_password_confirm, container, false);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    TextView dialogOk = (TextView) view.findViewById(R.id.dialogOk);
    TextView dialogCancel = (TextView) view.findViewById(R.id.dialogCancel);
    dialogOk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (validateFields()) {
                dialogButtonClick.onConfirmChangePassword(editOldPassword.getText().toString().trim(), editConfirmPassword.getText().toString().trim());
                dismiss();
            }
        }
    });
    setCancelable(false);
    dialogCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    });
}



public interface Listener {
    void onConfirmChangePassword(String oldPassword, String newPassword);
}

}

Upvotes: 0

Related Questions