1

I have this app, that I created a custom dialog for. I must of goofed something up cause while the .show call on the dialog does indeed bring it up, it looks like a whole new fragment and it is not floating but instead replacing the ui with its contents. I did see in their help for DialogFragment:

http://hi-android.info/docs/reference/android/app/DialogFragment.html#Lifecycle

that one can embed a dialog as a regular fragment or not. Though I am not doing anything to do this so I cannot figure out why its acting like an embedded fragment and not floating. After thinking on it, is it the way I defined my XML definition? The dialogfragment example above didn't really give a definition for the xml layout, so maybe that is where my issue is? (Even added the gravity to the xml file, still no dice)

My xml definition for this Dialog is here:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:gravity="center_horizontal"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
   <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal"
 android:layout_width="match_parent"
 android:layout_height="wrap_content">       
    <TextView               
            android:textSize="20sp"
            android:text = "Location:" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"/>
         <Spinner 
            android:id="@+id/location_spinner"
            android:layout_width = "450sp"
            android:layout_height="wrap_content"/>
            <!-- fill out the data on the package total cost etc -->    
    </LinearLayout>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">    
               <Button android:id="@+id/location_dlg_ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Okay"/>
                   <Button android:id="@+id/location_dlg_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cancel"/>
                   <Button android:id="@+id/location_dlg_new"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Create new..."/>
    </LinearLayout>
</LinearLayout>

Like I said displays just fine, the code for the fragment:

        package com.viciousbytes.studiotab.subactivities.dialogfragments;

        import ... ...

        public class LocationPicker extends DialogFragment {

            ArrayList<Location> mLocations;


            public static LocationPicker newInstance()
            {
                LocationPicker loc = new LocationPicker();
                                    return loc;

            }
            private void setLocations(ArrayList<Location> loc)
            {
                mLocations=loc;     
            }

              @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    // Pick a style based on the num.
                    int style = DialogFragment.STYLE_NORMAL, theme = android.R.style.Theme;         
                    setStyle(style, theme);
                }

              @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                    View v = inflater.inflate(R.layout.location_dialog, container, false);


                    Spinner spinner = (Spinner)v.findViewById(R.id.location_spinner);
                    ArrayAdapter<Location> adapter = new ArrayAdapter<Location>(v.getContext(), android.R.layout.simple_spinner_item, mLocations);                  
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                    if(mLocations==null)
                        spinner.setPrompt("No Locations");
                    else
                        spinner.setAdapter(adapter);

                    spinner.setOnItemSelectedListener(new LocationSelectedListener());

                    // Watch for button clicks.
                    Button newBtn = (Button)v.findViewById(R.id.location_dlg_new);
                    newBtn.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            // When button is clicked, call up to owning activity.
                            //create new start that activity...

                        }
                    });

                    // Cancel do nothing dismissthis
                    Button cancelBtn = (Button)v.findViewById(R.id.location_dlg_cancel);
                    cancelBtn.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            // When button is clicked, call up to owning activity.
                            //create new start that activity...

                        }
                    });

                    // okay button means set listener with the selected location.
                    Button okBtn = (Button)v.findViewById(R.id.location_dlg_ok);
                    okBtn.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            // When button is clicked, call up to owning activity.
                            //create new start that activity...

                        }
                    });


                    return v;
                }

        }

It is called from a fragment itself? though does that matter? because I am calling a TimePIckerDialog and a DatePickerDialog and those work fine, but my calling code from my other fragment is:

void showLocationDialog() {     


                 FragmentTransaction ft = getFragmentManager().beginTransaction();
                 Fragment prev = getFragmentManager().findFragmentByTag("locpicker");
                if (prev != null) {
                   ft.remove(prev);
                  }
                ft.addToBackStack(null);

                // Create and show the dialog.
                DialogFragment newFragment = LocationPicker.newInstance();
                newFragment.show(ft, "locpicker");                  
}
Codejoy
  • 3,722
  • 13
  • 59
  • 99

1 Answers1

1

Your constructors are wrong. Try to have just one static method newInstance to instantiate the fragment for all cases and use a Bundle to store the arguments that you want to use in the fragment. Refer to Basic Dialog section here and extend it to your case.

500865
  • 6,920
  • 7
  • 44
  • 87
  • I used that example. Copied it near verbatim and still having issues. Same thing, i am perplexed. In act in their example their newInstance was not declared public so its usage in another activity didn't work unless it was declared public (for me). – Codejoy Jul 22 '11 at 02:30
  • actually I got it, the Theme was wrong, I needed a theme 0 which I assumed was android.R.style.Theme but must not of been, setting theme =0 did the trick. – Codejoy Jul 22 '11 at 15:04
  • Your new constructors look fine!! – 500865 Jul 22 '11 at 21:49