Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 735
Jeffrey Chou Avatar asked Jun 23 '17 09:06

Jeffrey Chou


People also ask

What is the difference between dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do I use AlertDialog?

Alert Dialog code has three methods:setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message. setIcon() method is used to set the icon on the Alert dialog box.


2 Answers

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();
like image 78
AskNilesh Avatar answered Sep 29 '22 19:09

AskNilesh


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.

like image 41
Eddie Avatar answered Sep 29 '22 18:09

Eddie