9) Dialog Fragment

A Dialog box is like a prompt for user decisions/information for an app to continue a task (e.g. deleting a record in a database)

A Dialog Box is composed of 3 parts:

1) Optional title
2) Content Area (can contain sliders/lists/ checkboxes etc)
3) Action Buttons (e.g. Cancel & OK). Keep dismissive actions on the left, and affirmative action on the right.

Construct/create a DialogFragment

  • Define the appearance of the layout
  • Create a class that extends DialogFragment
  • Use onCreateView to link the appearance
  • Displaying a DialogFragment

  • MyDialog d= new Dialog();
  • d.show(manager,”test”);

    Note: the fragment manager object is managed in the show() method. We don’t call commit() after show either as that too is managed within the show() method.

    show Dialog must be the last call if we are carrying out fragment transactions

    e.g.
    // Add a
    // Replace a with b

    because commit() as mentioned above is WITHIN the show() method.

    MainActivity.java

    package com.jamesfroggatt.dialogsdemo.app;
    
    import android.app.Activity;
    import android.app.FragmentManager;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Toast;
    
    import static android.widget.Toast.LENGTH_SHORT;
    
    public class MainActivity extends Activity implements MyDialog.Communicator{
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        public void showDialog(View v){
            FragmentManager manager=getFragmentManager();
            MyDialog myDialog=new MyDialog();
            myDialog.show(manager,"MyDialog");
        }
    
        @Override
        public void onDialogMessage(String message) {
            Toast.makeText(this,message, LENGTH_SHORT).show();
        }
    }
    

    MyDialog.java

    package com.jamesfroggatt.dialogsdemo.app;
    
    import android.app.Activity;
    import android.app.DialogFragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.Button;
    
    public class MyDialog extends DialogFragment implements View.OnClickListener{
    
       Button yes,no;
    
        // create a reference to the communicator interface
        Communicator comm;
    
    
        // link the xml appearance with java
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
            View view=inflater.inflate(R.layout.my_dialog, null);
    
            // set the title of the dialog
            getDialog().setTitle("Hello");
    
            yes= (Button) view.findViewById(R.id.yes);
            no= (Button) view.findViewById(R.id.no);
    
            // set the onclick listeners
            yes.setOnClickListener(this);
            no.setOnClickListener(this);
    
            // stops user clicking off the Dialog and it dissapearing
            setCancelable(false);
    
            return view;
        }
    
    
    
        @Override
        public void onClick(View v) {
            if (v.getId()==R.id.yes){
    
                comm.onDialogMessage("Yes was clicked");
                // makes the dialog go away when button is selected
                dismiss();
                //Toast.makeText(getActivity(), "Yes button clicked", Toast.LENGTH_SHORT).show();
            }else{
    
                comm.onDialogMessage("No was clicked");
                // makes the dialog go away when button is selected
                dismiss();
    
                //Toast.makeText(getActivity(),"No button clicked",Toast.LENGTH_SHORT).show();
            }
        }
    
        interface Communicator
        {
            public void onDialogMessage(String message);
        }
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            comm= (Communicator) activity;
        }
    }
    

    my_dialog.xml

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="This is a dialog"
            android:id="@+id/textView" />
    
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="No"
            android:id="@+id/no"
            android:layout_below="@+id/textView" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Yes"
            android:id="@+id/yes"
            android:layout_below="@+id/textView"
            android:layout_toRightOf="@+id/no"/>
    
    </RelativeLayout>
    

    activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="${packageName}.${activityClass}">
    
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show dialog"
            android:onClick="showDialog"
             />
    </RelativeLayout>
    

    Tutorial notes from:

    An older approach to making a Dialog can be seen below

    Vid1:

    Vid2:

    Vid3:

  • Leave a Reply