Search code examples
javaandroidcheckboxandroid-dialogfragmentdialogfragment

Do not show again checkbox on a DialogFragment


So the title explains it all. I have a dialogfragment that currently pops up and I want to add a do not show checkbox to it and then obviously implement that check and not show if it was checked. I know there is a .setSingleChoiceItems, but I am not entirely sure on what would be going in there as it isn't really an item I would add somewhere. But then again I could probably be wrong as I am just getting into Android development.

Dialogfragment java

public class WifivsDataDialog extends DialogFragment {


     @
     Override
     public Dialog onCreateDialog(Bundle savedInstanceState) {
         // Use the Builder class for convenient dialog construction
         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
         builder.setMessage(R.string.dialog_box)
             .setPositiveButton(R.string.WiFi, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                     // FIRE ZE MISSILES!

                 }
             })
             .setNegativeButton(R.string.Cell_Data, new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                     // User cancelled the dialog
                 }
             });
         // Create the AlertDialog object and return it
         return builder.create();
     }
 }

Here is the code calling it in my MainActivity.java

 WifivsDataDialog myDiag = new WifivsDataDialog();
    myDiag.show(getFragmentManager(), "dialog_layout");
    myDiag.setCancelable(false);

Solution

  • The DialogFragment class

        public class WifivsDataDialog extends DialogFragment implements View.OnClickListener {
        private CheckBox checkBox;
        private Button button1;
        private Button button2;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View mainView = inflater.inflate(R.layout.wifi_dialog, container);
            checkBox = (CheckBox) mainView.findViewById(R.id.checkBox);
            button1 = (Button) mainView.findViewById(R.id.button1);
            button2 = (Button) mainView.findViewById(R.id.button2);
    
            button1.setOnClickListener(this);
            button2.setOnClickListener(this);
    
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                    // Store the isChecked to Preference here
                    SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPref.edit();
                    editor.putBoolean("DONT_SHOW_DIALOG", isChecked);
                    editor.commit();
    
                }
            });
            return mainView;
        }
    
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.button1:
                    // Do wifi stuff here
                    break;
                case R.id.button2:
                    // Do cellular stuff here
                    break;
            }
        }
    }
    

    The Layout xml wifi_dialog.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:padding="20dp"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent">
    
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Some information to be displayed to human"
                android:id="@+id/textView" android:textSize="30dp"/>
        <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Do now show this dialog again."
                android:id="@+id/checkBox"/>
        <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:gravity="center">
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="WiFi"
                    android:id="@+id/button1" android:layout_gravity="center_horizontal"/>
            <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="CELL"
                    android:id="@+id/button2"/>
        </LinearLayout>
    </LinearLayout>
    

    And on your Activity or where ever you display the dialog, check the preference set by user before displaying the dialog. Something like below.

    SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
            boolean dontShowDialog = sharedPref.getBoolean("DONT_SHOW_DIALOG", false);
            if (!dontShowDialog) {
                new WifivsDataDialog().show(getFragmentManager(), "WiFi");
            }