DialogUsage.java revision fe61a38a254d7360d26dfdc37e5c1105ec7f793c
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.example.android.supportv7.app;
17
18import com.example.android.supportv7.R;
19
20import android.app.Dialog;
21import android.content.Context;
22import android.os.Bundle;
23import android.support.v4.view.WindowCompat;
24import android.support.v7.app.AppCompatActivity;
25import android.support.v7.app.AppCompatDialog;
26import android.view.Menu;
27import android.view.MenuItem;
28import android.view.View;
29import android.widget.Spinner;
30import android.widget.Toast;
31
32/**
33 * This demonstrates idiomatic usage of AppCompatDialog.
34 */
35public class DialogUsage extends AppCompatActivity {
36
37    private Spinner mSpinner;
38
39    @Override
40    protected void onCreate(Bundle savedInstanceState) {
41        super.onCreate(savedInstanceState);
42        setContentView(R.layout.dialog_usage);
43
44        mSpinner = (Spinner) findViewById(R.id.spinner_dialogs);
45
46        // Add an OnClickListener to show our selected dialog
47        findViewById(R.id.btn_show_dialog).setOnClickListener(new View.OnClickListener() {
48            @Override
49            public void onClick(View view) {
50                showSelectedDialog();
51            }
52        });
53    }
54
55    private void showSelectedDialog() {
56        switch (mSpinner.getSelectedItemPosition()) {
57            case 0:
58                showSimpleDialog();
59                break;
60            case 1:
61                showSimpleDialogWithActionBar();
62                break;
63            case 2:
64                showButtonBarDialog();
65                break;
66        }
67    }
68
69    private void showSimpleDialog() {
70        Dialog dialog = new AppCompatDialog(this);
71        dialog.setTitle(R.string.dialog_title);
72        dialog.setContentView(R.layout.dialog_content);
73        dialog.show();
74    }
75
76    private void showSimpleDialogWithActionBar() {
77        AppCompatDialog dialog = new MenuDialog(this);
78        // Request the support Action Bar window feature
79        dialog.supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);
80        dialog.setTitle(R.string.dialog_title);
81        dialog.setContentView(R.layout.dialog_content);
82        dialog.show();
83    }
84
85    private void showButtonBarDialog() {
86        Dialog dialog = new AppCompatDialog(this);
87        dialog.setTitle(R.string.dialog_title);
88        dialog.setContentView(R.layout.dialog_content_buttons);
89        dialog.show();
90    }
91
92    /**
93     * A simple {@link android.support.v7.app.AppCompatDialog} implementation which
94     * inflates some items into it's options menu, and shows a toast when one is selected.
95     */
96    private class MenuDialog extends AppCompatDialog {
97
98        public MenuDialog(Context context) {
99            super(context);
100        }
101
102        @Override
103        public boolean onCreateOptionsMenu(Menu menu) {
104            getMenuInflater().inflate(R.menu.actions, menu);
105            return true;
106        }
107
108        @Override
109        public boolean onOptionsItemSelected(MenuItem item) {
110            Toast.makeText(getOwnerActivity(), "Dialog action selected: " + item.getTitle(),
111                    Toast.LENGTH_SHORT).show();
112            return true;
113        }
114    }
115
116}
117