Adrian-Costin Țundrea
Adrian-Costin Țundrea

Reputation: 325

Android Dialog without transparent margins (fullscreen)

I have an alert dialog created using the alertdialog builder. I want to remove the space in the left and right of the dialog... basically extend it from side to side. I know I could you an activity instead of a dialog, but I want to keep the button style and implementing that button style in an activity requires making a layout for different SDKs, which is not convenient in the long run.

Why I need it full width? Because I need to display AdMob ads and if they are not full width the ads will not load.

Any help is appreciated as I have tried all kinds of theme properties...

Thanks, Adrian

PS: Here is my current code for creating the dialog:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.DialogTheme));
    alertDialogBuilder.setInverseBackgroundForced(true);
    LayoutInflater inflater = this.getLayoutInflater();
    View view = inflater.inflate(R.layout.dial_dialog, null);

    AdView adView = (AdView) view.findViewById(R.id.adView);
    if (!application.getLicense().isValid()) {
        adView.loadAd(new AdRequestWrapper(this));
    }

    alertDialogBuilder.setView(view);
    alertDialogBuilder.setTitle(R.string.dial_dialog_title).setCancelable(false);
    alertDialogBuilder.setPositiveButton(R.string.dial_dialog_message_positive_text, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });
    alertDialogBuilder.setNeutralButton(R.string.dial_dialog_message_neutral_text, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });
    alertDialogBuilder.setNegativeButton(R.string.dial_dialog_message_negative_text, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
        }
    });
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
    alertDialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

PS2: Here is an image with what I need... http://www.tiikoni.com/tis/view/?id=f3aed38 I need to loose all the space with red. I don't have any problems with the space marked in yellow. (it can be kept or it can be removed)

Upvotes: 2

Views: 2159

Answers (2)

Adrian-Costin Țundrea
Adrian-Costin Țundrea

Reputation: 325

I know a little bit late to come back with an answer, but following the recipes from here I managed to solved almost everything. Basically I have the following Dialog theme:

<style name="DialogTheme" parent="android:Theme.Holo.Light.Dialog">
    <item name="android:windowBackground">@drawable/screen_background_selector_light</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowMinWidthMajor">100%</item>
    <item name="android:windowMinWidthMinor">100%</item>
</style>

Note the reference to screen_background_selector_light which means I also had to add screen_background_selector_light.xml and (its dependency background_holo_light.xml).

Other that that I only had to specify the activity will use this new theme:

<activity android:name=".zom.xyz.app.activity.MyActivity_" android:label="@string/activity_title" android:theme="@style/DialogTheme"/>

And of course create it as any other activity.

As for the other need I had... easy buttons I did it this way. Here is the layout of the activity

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">

    INSERT_DIALOG_CONTENT_HERE

</RelativeLayout>

<LinearLayout
        style="?android:attr/buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <Button
            android:id="@+id/cancelButton"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="fill_vertical"
            android:layout_weight="1"
            android:text="@string/activity_message_negative_text"/>

    <Button
            android:id="@+id/skipButton"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="fill_vertical"
            android:layout_weight="1"
            android:text="@string/activity_message_neutral_text"/>

    <Button
            android:id="@+id/correctButton"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="fill_vertical"
            android:layout_weight="1"
            android:text="@string/activity_message_positive_text"/>
</LinearLayout>

Using this layout and theme will give you the exact feel of a dialog while giving it full width.

Upvotes: 1

1 )Create your own dialog extended from Dialog and in constructor set style, something like this:

public MyDialog(Context c, Event e) {
        super(c, android.R.style.Theme_Light);
} 

or

2) when creating instanse, set style:

Dialog dialog = new Dialog(this, R.style.MyDialogTheme);

example here

Upvotes: 0

Related Questions