Lesson 110. Android 3. Fragments. DialogFragment - dialogue

Lesson 110. Android 3. Fragments. DialogFragment – dialogue


In this lesson:

– we work with DialogFragment

We continue to consider the heirs of Fragment. DialogFragment – differs from the usual snippet in that it is displayed as a dialog and has appropriate methods.

There are two ways to create a dialog: using your layout file and using AlertDialog.Builder. We are drawing an application that will trigger two dialogues built in different ways.

Let’s create a project:

Project name: P1101_DialogFragment
Build Target: Android 4.1
Application name: DialogFragment
Package name: ru.startandroid.develop.p1101dialogfragment
Create Activity: MainActivity

Add rows to strings.xml:

Dialog 1
Dialog 2
Text of your message
Yes
No
Maybe

We will create two dialogues, so we will need two snippets.

Let’s create a layout file for the first snippet.

dialog1.xml:



	
	
	
		
		
		
	

This is what our dialogue will look like – the message text and the three buttons.

we create a class Dialog1.java:

package ru.startandroid.develop.p1101dialogfragment;

import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

public class Dialog1 extends DialogFragment implements OnClickListener {

  final String LOG_TAG = "myLogs";

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    getDialog().setTitle("Title!");
    View v = inflater.inflate(R.layout.dialog1, null);
    v.findViewById(R.id.btnYes).setOnClickListener(this);
    v.findViewById(R.id.btnNo).setOnClickListener(this);
    v.findViewById(R.id.btnMaybe).setOnClickListener(this);
    return v;
  }

  public void onClick(View v) {
    Log.d(LOG_TAG, "Dialog 1: " + ((Button) v).getText());
    dismiss();
  }

  public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    Log.d(LOG_TAG, "Dialog 1: onDismiss");
  }

  public void onCancel(DialogInterface dialog) {
    super.onCancel(dialog);
    Log.d(LOG_TAG, "Dialog 1: onCancel");
  }
}

IN onCreateView we get the Dialog object using the getDialog method and set the dialog header. Next, we create a layout view, find the buttons in it, and place the current snippet as the handler.

IN onClick output the text by holding down the buttons and explicitly close the dialog by dismiss.

The onDismiss method works when the dialog closes. We write about it in a log.

The onCancel method works when the dialog is canceled by the Back button. We write about it in a log.

Let’s create the second fragment. This is where we will build a dialog with the help of the builder, so there is no need for a layout file. We only create a class Dialog2.java:

package ru.startandroid.develop.p1101dialogfragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;

public class Dialog2 extends DialogFragment implements OnClickListener {

  final String LOG_TAG = "myLogs";

  public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder adb = new AlertDialog.Builder(getActivity())
        .setTitle("Title!").setPositiveButton(R.string.yes, this)
        .setNegativeButton(R.string.no, this)
        .setNeutralButton(R.string.maybe, this)
        .setMessage(R.string.message_text);
    return adb.create();
  }

  public void onClick(DialogInterface dialog, int which) {
    int i = 0;
    switch (which) {
    case Dialog.BUTTON_POSITIVE:
      i = R.string.yes;
      break;
    case Dialog.BUTTON_NEGATIVE:
      i = R.string.no;
      break;
    case Dialog.BUTTON_NEUTRAL:
      i = R.string.maybe;
      break;
    }
    if (i > 0)
      Log.d(LOG_TAG, "Dialog 2: " + getResources().getString(i));
  }

  public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);
    Log.d(LOG_TAG, "Dialog 2: onDismiss");
  }

  public void onCancel(DialogInterface dialog) {
    super.onCancel(dialog);
    Log.d(LOG_TAG, "Dialog 2: onCancel");
  }
}

We usually used the onCreateView method to fill the snippet with content. To create a dialog using the Bilder, you use onCreateDialog. We create a dialog with a header, a message and three buttons. Assign the current snippet to the button handler.

IN onClick determine which button was pressed and display the corresponding text in the log. If you create a dialog through the builder, the dialog itself closes after clicking the button, the dismiss method is not needed here.

methods onDismiss and onCancel is the closure and cancellation of a dialogue, similar to the first fragment.

We change the layout file for MainActivity – main.xml:



	
	

Here are just two buttons.

Kodyma MainActivity.java:

package ru.startandroid.develop.p1101dialogfragment;

import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

  DialogFragment dlg1;
  DialogFragment dlg2;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    dlg1 = new Dialog1();
    dlg2 = new Dialog2();
  }

  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btnDlg1:
      dlg1.show(getFragmentManager(), "dlg1");
      break;
    case R.id.btnDlg2:
      dlg2.show(getFragmentManager(), "dlg2");
      break;
    default:
      break;
    }

  }
}

We create dialogs and run them using the show method, which requires FragmentManager and a string tag to enter. The transaction and who takes place within this method, we do not need to think about it.

We all save and launch the application.

press Dialog1

Our simple dialogue was reflected.

Press a button, for example, Yes – the dialogue closed. We look at the logs:

Dialogue 1: Yes
Dialogue 1: onDismiss

Everything is correct.

Let’s start the first dialog again and press the key back (Back). We look at the log:

Dialogue 1: onCancel
Dialogue 1: onDismiss

OnCancel – dialog was canceled and onDismiss – dialog closed.

If we rotate the screen, every time it will work onDismiss, but the dialogue will be displayed again after the rotation.

Let’s start the second dialog – click the button Dialogue 2.

The standard dialog we have designed is displayed. For example, No – the dialogue closed. In the logs:

Dialogue 2: No
Dialogue 2: onDismiss

Let’s start the second dialogue again and click back. In the logs:

Dialogue 2: onCancel
Dialogue 2: onDismiss

All the same as in the first case.

A few more words on the topic.

If you do not want your dialog to be closed with a button, use the setCancelable method with the false parameter for your snippet.

There is another way of calling the dialogue. This is a show method, but it already accepts not FragmentManager but FragmentTransaction on the input. In this case, the system itself will call commit inside the show, but we can pre-populate any transaction we created or send it to BackStack.

You can use dialog snippets as regular snippets and display them on Activity rather than as a dialog. But be careful with use getDialog. I understand that it returns null in this case.

if AlertDialog.Builder unfamiliar to you, then take a look at Lesson 60 and the following. There is enough detail on how to create different dialogs.

In the next lesson:

– we work with PreferenceFragment
– we use Headers




Discuss in the forum [88 replies]

Leave a Comment