Skip to content Skip to sidebar Skip to footer

How To I Create A 100% Custom Dialogfragment

I want to have an Android dialog using Fragments that is fully customized: none of the platform dialog theme pieces are included. For example, something like this: How do I do thi

Solution 1:

Code below will help you to display full screen dialog and it also set transparent color

Dialogdialog=newDialog(this);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// layout to display
dialog.setContentView(R.layout.about_program_dialog_layout);

// set color transpartent
dialog.getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));

dialog.show();

about_program_dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:background="#55000000" ><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true"android:layout_marginBottom="227dp"android:text="Dismiss" /><TextViewandroid:id="@+id/autoCompleteTextView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/button1"android:layout_alignParentRight="true"android:layout_marginBottom="16dp"android:layout_marginRight="63dp"android:ems="10"android:text="Hello There World" /></RelativeLayout>

Solution 2:

The following Mono for Android C# code does the trick (but should be easy to port to Java). I tested on Android 2.2 (Galaxy S) and Android 4.1 (Nexus 7). The only thing you would need to change are the layout IDs used for the parent view and dialog view.

[Activity (MainLauncher = true)]            
publicclassTestCustomDialogActivity : FragmentActivity
{
    publicclassMyDialogFragment : Android.Support.V4.App.DialogFragment
    {
        publicoverride Android.Views.View OnCreateView(Android.Views.LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Android 3.x+ still wants to show title: disable
            Dialog.Window.RequestFeature(WindowFeatures.NoTitle);

            // CHANGE TO YOUR DIALOG LAYOUT or VIEW CREATION CODEreturn inflater.Inflate(Resource.Layout.MyLayout, container, true);
        }

        publicoverridevoidOnResume()
        {
            // Auto size the dialog based on it's contents
            Dialog.Window.SetLayout(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);

            // Make sure there is no background behind our view
            Dialog.Window.SetBackgroundDrawable(new ColorDrawable(Color.Transparent));

            // Disable standard dialog styling/frame/theme: our custom view should create full UI
            SetStyle(Android.Support.V4.App.DialogFragment.StyleNoFrame, Android.Resource.Style.Theme);

            base.OnResume();
        }
    }

    protectedoverridevoidOnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // CHANGE TO YOUR MAIN SCREEN
        SetContentView(Resource.Layout.MyDialog);

        var dialog = new MyDialogFragment();
        dialog.Show(SupportFragmentManager, "dialog");
    }        
}

I uploaded a full Mono for Android sample to https://github.com/t9mike/CustomDialogFragmentSample.

Post a Comment for "How To I Create A 100% Custom Dialogfragment"