LoginSignup
149

More than 5 years have passed since last update.

[Android] DialogFragmentを使ってダイアログを表示する

Last updated at Posted at 2013-12-12

APIレベル13からActivity.showDialog(int id)は非推奨となってしまいました。
DialogFragmentを使ってダイアログを表示させる方法が推奨されています。
(2013年12月12日現在)

参考

  • 公式ドキュメント Dialogs

  • DialogFragmentを利用するときのチェックポイント

シンプルなアラートダイアログ

kobito.1386850867.739619.png

※ 縦横切替時にTestDialogFragmentのコンストラクタがないと落ちるかもしれません(未確認)

HogeActivity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_top);

        // ダイアログを表示する
        DialogFragment newFragment = new TestDialogFragment();
        newFragment.show(getFragmentManager(), "test");
    }

    public static class TestDialogFragment 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("ダイアログ")
                    .setPositiveButton("はい", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // FIRE ZE MISSILES!
                        }
                    })
                    .setNegativeButton("キャンセル", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // User cancelled the dialog
                        }
                    });
            // Create the AlertDialog object and return it
            return builder.create();
        }
    }


リストを表示するダイアログ

iOSのActionSheetの変わりにするならこのダイアログだと思います。

kobito.1386855856.924778.png

HogeActivity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_top);

        // ダイアログを表示する
        DialogFragment newFragment = new ContactUsDialogFragment();
        newFragment.show(getFragmentManager(), "contact_us");
    }

    public static class ContactUsDialogFragment extends DialogFragment {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            CharSequence[] items = {"使い方", "よくある質問", "メール", "閉じる"};

             Activity activity = getActivity();

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                        case 0:
                            Toast.makeText(activity, "使い方が押された", Toast.LENGTH_LONG).show();
                            break;
                        case 1:
                            Toast.makeText(activity, "よくある質問が押された", Toast.LENGTH_LONG).show();
                            break;
                        case 2:
                            Toast.makeText(activity, "メールが押された", Toast.LENGTH_LONG).show();
                            break;
                        case 3:
                            Toast.makeText(activity, "閉じる", Toast.LENGTH_LONG).show();
                            break;
                        default:
                            break;
                    }
                }
            });
            return builder.create();
        }
    }

カスタムダイアログ

kobito.1388994152.377665.png

HogeActivity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_top);

        // ダイアログを表示する
        DialogFragment newFragment = new TestDialogFragment();
        newFragment.show(getFragmentManager(), "test");
    }

    public static class TestDialogFragment extends DialogFragment {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            // Use the Builder class for convenient dialog construction
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View content = inflater.inflate(R.layout.dialog_setting, null);

            builder.setView(content);

            builder.setMessage("設定")
                    .setNegativeButton("閉じる", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // User cancelled the dialog
                        }
                    });
            // Create the AlertDialog object and return it
            return builder.create();
        }
    }
dialog_setting.xml
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="340dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:paddingBottom="20dp">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="声音量"
            android:id="@+id/textView"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"/>

        <SeekBar
            android:layout_width="10dp"
            android:layout_height="wrap_content"
            android:id="@+id/seek_bar_voice_volume"
            android:layout_gravity="center_vertical"
            android:max="1000"
            android:layout_weight="4"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="340dp"
        android:layout_height="wrap_content"
        android:orientation="horizontal">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="エコー"
            android:id="@+id/textView"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"/>

        <SeekBar
            android:layout_width="10dp"
            android:layout_height="wrap_content"
            android:id="@+id/seek_bar_echo"
            android:layout_gravity="center_vertical"
            android:max="1000"
            android:layout_weight="4"/>
    </LinearLayout>

</LinearLayout>

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
149