Fragment Dialog : Dialog « UI « Android

Fragment Dialog


 * Copyright (C) 2010 The Android Open Source Project
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.



import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class FragmentDialog extends Activity {
    int mStackLevel = 0;

    protected void onCreate(Bundle savedInstanceState) {

        View tv = findViewById(;
        ((TextView)tv).setText("Example of displaying dialogs with a DialogFragment.  "
                + "Press the show button below to see the first dialog; pressing "
                + "successive show buttons will display other dialog styles as a "
                + "stack, with dismissing or back going to the previous dialog.");

        // Watch for button clicks.
        Button button = (Button)findViewById(;
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

        if (savedInstanceState != null) {
            mStackLevel = savedInstanceState.getInt("level");

    public void onSaveInstanceState(Bundle outState) {
        outState.putInt("level", mStackLevel);

    void showDialog() {

        // will take care of adding the fragment
        // in a transaction.  We also want to remove any currently showing
        // dialog, so make our own transaction and take care of that here.
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        Fragment prev = getFragmentManager().findFragmentByTag("dialog");
        if (prev != null) {

        // Create and show the dialog.
        DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);, "dialog");

    static String getNameForNum(int num) {
        switch ((num-1)%6) {
            case 1: return "STYLE_NO_TITLE";
            case 2: return "STYLE_NO_FRAME";
            case 3: return "STYLE_NO_INPUT (this window can't receive input, so "
                    + "you will need to press the bottom show button)";
            case 4: return "STYLE_NORMAL with dark fullscreen theme";
            case 5: return "STYLE_NORMAL with light theme";
            case 6: return "STYLE_NO_TITLE with light theme";
            case 7: return "STYLE_NO_FRAME with light theme";
            case 8: return "STYLE_NORMAL with light fullscreen theme";
        return "STYLE_NORMAL";

    public static class MyDialogFragment extends DialogFragment {
        int mNum;

         * Create a new instance of MyDialogFragment, providing "num"
         * as an argument.
        static MyDialogFragment newInstance(int num) {
            MyDialogFragment f = new MyDialogFragment();

            // Supply num input as an argument.
            Bundle args = new Bundle();
            args.putInt("num", num);

            return f;
        public void onCreate(Bundle savedInstanceState) {
            mNum = getArguments().getInt("num");

            // Pick a style based on the num.
            int style = DialogFragment.STYLE_NORMAL, theme = 0;
            switch ((mNum-1)%6) {
                case 1: style = DialogFragment.STYLE_NO_TITLE; break;
                case 2: style = DialogFragment.STYLE_NO_FRAME; break;
                case 3: style = DialogFragment.STYLE_NO_INPUT; break;
                case 4: style = DialogFragment.STYLE_NORMAL; break;
                case 5: style = DialogFragment.STYLE_NORMAL; break;
                case 6: style = DialogFragment.STYLE_NO_TITLE; break;
                case 7: style = DialogFragment.STYLE_NO_FRAME; break;
                case 8: style = DialogFragment.STYLE_NORMAL; break;
            switch ((mNum-1)%6) {
                case 4: theme =; break;
                case 5: theme =; break;
                case 6: theme =; break;
                case 7: theme =; break;
                case 8: theme =; break;
            setStyle(style, theme);

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.fragment_dialog, container, false);
            View tv = v.findViewById(;
            ((TextView)tv).setText("Dialog #" + mNum + ": using style "
                    + getNameForNum(mNum));

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

            return v;



Related examples in the same category

1.Dialog Helper
2.Use AlertDialog to inform exception
3.Add option item to Alert dialog and get user selection result
4.Add some padding to keep the dialog borders away
5.Dialog activity and TextView
6.Preference dialog
7.Show dialog
8.Dialog layout
9.Layout dialog with xml layout file
10.extends DialogFragment
11.Color Picker Dialog
12.Dialog Yes No Message
13.Dialog Yes No Old School Message
14.Dialog Yes No Holo Light Message
15.Dialog Yes No Long Message
16.Dialog Yes No Ultra Long Message
17.Dialog with List of value
18.Dialog with Progress
19.Dialog with Single Choice
20.Dialog Multiple Choice
21.Dialog Multiple Choice Cursor
22.Dialog with Text Entry
23.Dialog with Xml layout
24.Dialog Activity
25.Demonstrates how to show an AlertDialog that is managed by a Fragment.
26.Demonstrates the use of progress dialogs.
27.File open dialog
28.Show error AlertDialog
29.extends DialogPreference
30.Show Notification Alert Dialog
31.Text Dialog
32.Create Chang Log Dialog
33.Show Title And Message Dialog
34.Show dialog and parse URL
35.Error Dialog Wrapper
36.Ok Dialog Wrapper
37.Display an alert dialog
38.AlertDialog Question
40.dialog Builder
41.A dialog that allows the user to select multiple entries.
42.Help Dialog Creator
43.Color Select Dialog
44.Create MessageBox