Search code examples
androidxamarin.androidviewflipperandroid-dialogfragment

ViewFlipper not showing in custom DialogFragment


I created a custom DialogFragment that loads a custom layout called student_info_main_container. This layout contains a ViewFlipper which I load with two additional layouts. The problem is that the ViewFlipper doesn't show in my DialogFragment but the rest of the student_info_main_container layout does show. Most of my work is done in OnCreateView. Any help would be appreciated.

public class StudentInfoUIViewController : DialogFragment, IJoinClassView
    {
        private ViewFlipper studentInfoContentFlipper;

        public static StudentInfoUIViewController NewInstance() 
        {
            StudentInfoUIViewController frag = new StudentInfoUIViewController();     
            frag.SetStyle(DialogFragmentStyle.NoTitle, 0);

            return frag;
        }

        public override void OnAttach(Activity activity)
        {
            base.OnAttach(activity);
            Log.Debug("StudentInfoUIViewController", "OnAttach Called.");
        }

        //This is called to create the view and initialize the UI.  The UI isn't made visible until OnStart is called.
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        {
            Dialog.Window.SetGravity(GravityFlags.Top);

            //Inflate and set the main layout for this DialogFragment
            View mainView = inflater.Inflate(Resource.Layout.student_info_main_container, container, true);

            //Find the ViewFlipper in the mainView
            studentInfoContentFlipper = Activity.FindViewById<ViewFlipper>(Resource.Id.studentInfoMainContainer_viewFlipper_studentInfoContent);

            //Inflate the layouts that are going to be added to the studentInfoContentFlipper ViewFlipper
            View joinSelectionView = inflater.Inflate(Resource.Layout.student_info_join_selection, null);
            View test = inflater.Inflate(Resource.Layout.test, null);

            //Add the above views to the flipper.
            studentInfoContentFlipper.AddView(joinSelectionView, 0);
            studentInfoContentFlipper.AddView(test, 1);


            Log.Debug("StudentInfoUIViewController", "OnCreateView Called.");

            return mainView;
        }

        public override void OnStart()
        {
            base.OnStart();
            //UI is visible
            Log.Debug("StudentInfoUIViewController", "OnStart Called.");

        }               

    }

Solution

  • Don't look for the ViewFlipper in the Activity, simply look for it in the inflated view(mainView) which will become the view for the Fragment. Also don't use the Inflate() method with the boolean parameter set to true as that will add the inflated view to the container which will be done again, automatically, by the system after onCreateView returns.