5

I'm developing an Android application and I have a question about custom dialogs.

I do this to open a custom dialog:

protected void showSetFriendEmailDialog()
{
    // Create the dialog.
    final Dialog emailDialog = 
            new Dialog(FriendHomeActivity.this, android.R.style.Theme_DeviceDefault);
    emailDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

    emailDialog.setCancelable(true);
    emailDialog.setContentView(R.layout.dialog_add_friend_email);

    // Get dialog widgets references.
    final EditText editFriendsEmail = (EditText)emailDialog.findViewById(R.id.editEmailAddFriendEmail);
    Button btnAccept = (Button)emailDialog.findViewById(R.id.btnAddFriendEmail);

    // Set on click lister for accept button
    btnAccept.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            // Get selected values
            String friendEmail = 
                    editFriendsEmail.getText().toString();

            // Close dialog.
            emailDialog.dismiss();

            // TODO: Call api to send email to web service using friendsEmail var.
            Log.v(TAG, "Friend email: " + friendEmail);
        }
    });

    //now that the dialog is set up, it's time to show it    
    emailDialog.show();
}

And this is its layout:

<?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:orientation="vertical" >

    <TextView
        android:id="@+id/textAddFriendEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/layout_set_friend_email"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editEmailAddFriendEmail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textEmailAddress" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btnAddFriendEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/ok" />

</LinearLayout>

But it opens full screen.

What do I have to do to open it as an AlertDialog?

VansFannel
  • 45,055
  • 107
  • 359
  • 626

3 Answers3

7

you have to use android.R.style.Theme.Dialog instenad of android.R.style.Theme_DeviceDefault

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
2

Use R.style.Theme_AppCompat_Dialog in order to get the proper dialog.

Tom Aranda
  • 5,919
  • 11
  • 35
  • 51
0

Just make a custom style and apply it to dialog...

<style name="full_screen_dialog" parent="@android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
    <item name="android:windowBackground">@android:color/transparent</item>
</style>
Shani Goriwal
  • 2,111
  • 1
  • 18
  • 30