3

I'm developing an Android application where I have Dialogs with custom layout. At first I decided to use AlertDialog without title and without buttons, since I have my own Buttons in my custom layout. This works fine in Android 4.4, but not in Android 2.3.

This is how it's seen in Android 4.4:

enter image description here

This is how it's seen in Android 2.3:

enter image description here

Please notice the weird gray background behind my Dialog.

In SO I saw that some people solved it using Dialog instead of AlertDialog. I tried it and it really fixed it, but now my Dialog fills the whole screen:

enter image description here

And I don't want that, I want the Dialog to have some margin like the AlertDialog has.

This is my layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/dialog_background"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/adTitleTextView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginLeft="1dp"
        android:layout_marginRight="1dp"
        android:layout_marginTop="1dp"
        android:background="@drawable/background_green"
        android:gravity="center"
        android:textColor="#FFFFFFFF"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#FFFFFFFF" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:paddingBottom="15dp"
        android:paddingTop="10dp" 
        android:textColor="#FFFFFFFF" />

    <LinearLayout
        android:id="@+id/adButtonBar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/adNegativeButton"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/selector_button_alert_dialog"
            android:textColor="#FFFFFFFF"
            android:textSize="14sp" />

        <Button
            android:id="@+id/adNeutralButton"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/selector_button_alert_dialog"
            android:textColor="#FFFFFFFF"
            android:textSize="14sp" />

        <Button
            android:id="@+id/adPositiveButton"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/selector_button_alert_dialog"
            android:textColor="#FFFFFFFF"
            android:textSize="14sp" />

    </LinearLayout>

</LinearLayout>

This is how I create my Dialog:

    Dialog mDialog = new Dialog( this, android.R.style.Theme_Dialog );
    mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    View content = getLayoutInflater().inflate( R.layout.alert_dialog_layout, null);
    ((TextView)content.findViewById(R.id.adTitleTextView)).setText( title );
    ((TextView) content.findViewById( R.id.adBodyTextView )).setText( message );    

    mDialog.setContentView( content );

    Button b = (Button) content.findViewById(R.id.adPositiveButton);
    b.setText( posText );
    b.setOnClickListener( this );

    b = (Button) content.findViewById(R.id.adNegaveButton);
    b.setText( negText );
    b.setOnClickListener( this );

    mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    mDialog.show();

And this is how I create the AlertDialog:

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

    alertDialogBuilder.setView( content );

    final AlertDialog mDialog = alertDialogBuilder.create();
    mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    ...

Anybody knows how can I remove that weird background from AlertDialog in Android 2.3 OR how to make Dialog not to fill the whole screen?

Thank you!

PX Developer
  • 8,065
  • 7
  • 42
  • 66
  • it's seems like your both images are same then where is the issue? – M D Feb 28 '14 at 10:17
  • Like I explain in my post, if I use AlertDialog (images 1 and 2) I see a weird gray background behind the Dialog in Android 2.3 (please look at image 2). If I use Dialog (image 3), the Dialog fills the screen horizontally (please look at image 3), and I want it to be like Image 1, with some margin. – PX Developer Feb 28 '14 at 10:23
  • tell me first you added `Theme_Dialog` for all the `Android Version` separately into values `styles.xml`. – M D Feb 28 '14 at 10:27
  • I'm using `Android`'s default `Theme_Dialog` style, not a custom one, so I guess `Android` already has it for all versions, right? I added it because in the following post they say it fixes the full screen `Dialog` issue, but it doesn't work for me: http://stackoverflow.com/questions/17484863/custom-dialog-opens-full-screen – PX Developer Feb 28 '14 at 10:33
  • No no you go wrong over here.ok fine post your `Theme_Dialog` over here – M D Feb 28 '14 at 10:33
  • I don't have a Theme_Dialog, I'm using android.R.style.Theme_Dialog: http://developer.android.com/reference/android/R.style.html#Theme_Dialog – PX Developer Feb 28 '14 at 10:37
  • The backgrounds are set in the custom layout, like you can see in my post: android:background="@drawable/dialog_background". – PX Developer Feb 28 '14 at 10:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48674/discussion-between-m-d-and-px-developer) – M D Feb 28 '14 at 10:38

2 Answers2

1

I finally found a solution for this, although I don't like it much. I'm solving the Dialog not full screen issue.

Basically, I set the width programmatically and, if the height exceeds a certain value, I set it too:

    content.getLayoutParams().width = (int) (screenWidth * 0.95);

    content.post(new Runnable() {

        @Override
        public void run() {             

            int newMaxHeight = content.findViewById(R.id.adTitleTextView).getHeight() + 
                    content.findViewById(R.id.adBodyContainer).getHeight() + 
                    content.findViewById(R.id.adButtonBar).getHeight();             


            if( newMaxHeight > screenHeight * 0.92 ){

                content.getLayoutParams().height = (int) (screenHeight * 0.92);

                content.findViewById(R.id.adBodyContainer).getLayoutParams().height = 
                        (int) (screenHeight * 0.92) - content.findViewById(R.id.adTitleTextView).getHeight()
                        - content.findViewById(R.id.adButtonBar).getHeight();

            }

        }
    });

With this code I can limit the maximum width and height of the Dialog. If anybody finds a better solution, please post it here.

PX Developer
  • 8,065
  • 7
  • 42
  • 66
0

Try this :

dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
Sagar Shah
  • 4,272
  • 2
  • 25
  • 36