Wednesday, February 6, 2013

DialogFragment Mono for Android.

Wanted to make a Dialog fragment, for uploading a picture from either the photo gallery in Android or from the Camera.  I wanted to really start doing a lot of dialogfragments.  I like that I can use them as fragments in activities or make them float above as dialogs.. Gives me twice the functionality as just making a normal fragment.   Of course, it does require at least 2.2 on the API side.

The Code:

public class CameraDialogFragment : DialogFragment
    {
        ImageButton camera;
        ImageButton gallery;
        
        public override Android.Views.View OnCreateView(Android.Views.LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {

            Dialog.Window.RequestFeature(WindowFeatures.NoTitle);

            var view = inflater.Inflate(Resource.Layout.CameraDialog, container, true);

            gallery = view.FindViewById<ImageButton>(Resource.Id.imageButton2);
            gallery.Click += delegate {

            };

            camera = view.FindViewById<ImageButton>(Resource.Id.imageButton1);
            camera.Click += delegate {

            };
            
            return view;
        }
        
        public override void OnResume()
        {
        
            Dialog.Window.SetLayout(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent);
            
        
            Dialog.Window.SetBackgroundDrawable(new ColorDrawable(Color.Transparent));
            
        
            SetStyle(DialogFragmentStyle.NoFrame, Resource.Style.YouthImpactTheme);
            
            base.OnResume();
        }
        

        
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            

        }
        
    }

The XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <ImageButton
        android:src="@drawable/Camera3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton1" />
    <ImageButton
        android:src="@drawable/Gallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageButton2" />
</LinearLayout>


The result:




1 comment: