11

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?

Ajeet Choudhary
  • 1,969
  • 1
  • 17
  • 41
Jeffrey Chou
  • 331
  • 2
  • 4
  • 15

7 Answers7

10

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.

Eddie
  • 119
  • 1
  • 5
6

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();
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
3

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.

Community
  • 1
  • 1
Cochi
  • 2,199
  • 2
  • 12
  • 15
  • 1
    I tried it using ContextThemeWrapper and it didn't work. Am I applying the style wrong? How should I do it – Jeffrey Chou Jun 23 '17 at 10:36
  • You doesn't need any line of code , just edit your style.xml like above by adding the item `alertDialogTheme` and by adding the style "DialogTheme". Don't forget to remove your "hacking space". – Cochi Jun 23 '17 at 12:36
1

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.

Ajeet Choudhary
  • 1,969
  • 1
  • 17
  • 41
  • Thanks, it does work, but the appearance animation is less smooth. It sort of just pops up instead of slightly fading in. Do you have any idea how to make it appear with an animation similar to an alert dialog? – Jeffrey Chou Jun 23 '17 at 10:02
  • you can create custom animation for dialog. Ref:https://stackoverflow.com/questions/4817014/animate-a-custom-dialog – Ajeet Choudhary Jun 23 '17 at 10:08
  • create your animation xmls and set into dialog as dialog.getWindow().getAttributes().windowAnimations = R.style.PauseDialogAnimation; – Ajeet Choudhary Jun 23 '17 at 10:10
  • this seems like a really clunky workaround, I'm looking for a simpler solution. I feel like there is one. – Jeffrey Chou Jun 23 '17 at 10:35
  • once you create the style, you can use it anywhere in your project – Ajeet Choudhary Jun 23 '17 at 10:43
0

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);
}

}

Ashwin S Ashok
  • 3,623
  • 2
  • 29
  • 36
0

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

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


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

dialog.show()


saulmm
  • 2,398
  • 15
  • 21