2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.example.android.supportv4.app;
19 import com.example.android.supportv4.R;
21 import android.support.v4.app.DialogFragment;
22 import android.support.v4.app.Fragment;
23 import android.support.v4.app.FragmentActivity;
24 import android.support.v4.app.FragmentTransaction;
26 import android.os.Bundle;
27 import android.view.LayoutInflater;
28 import android.view.View;
29 import android.view.View.OnClickListener;
30 import android.view.ViewGroup;
31 import android.widget.Button;
32 import android.widget.TextView;
34 public class FragmentDialogSupport extends FragmentActivity {
38 protected void onCreate(Bundle savedInstanceState) {
39 super.onCreate(savedInstanceState);
40 setContentView(R.layout.fragment_dialog);
42 View tv = findViewById(R.id.text);
43 ((TextView)tv).setText("Example of displaying dialogs with a DialogFragment. "
44 + "Press the show button below to see the first dialog; pressing "
45 + "successive show buttons will display other dialog styles as a "
46 + "stack, with dismissing or back going to the previous dialog.");
48 // Watch for button clicks.
49 Button button = (Button)findViewById(R.id.show);
50 button.setOnClickListener(new OnClickListener() {
51 public void onClick(View v) {
56 if (savedInstanceState != null) {
57 mStackLevel = savedInstanceState.getInt("level");
62 public void onSaveInstanceState(Bundle outState) {
63 super.onSaveInstanceState(outState);
64 outState.putInt("level", mStackLevel);
67 //BEGIN_INCLUDE(add_dialog)
71 // DialogFragment.show() will take care of adding the fragment
72 // in a transaction. We also want to remove any currently showing
73 // dialog, so make our own transaction and take care of that here.
74 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
75 Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");
79 ft.addToBackStack(null);
81 // Create and show the dialog.
82 DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
83 newFragment.show(ft, "dialog");
85 //END_INCLUDE(add_dialog)
87 static String getNameForNum(int num) {
89 case 1: return "STYLE_NO_TITLE";
90 case 2: return "STYLE_NO_FRAME";
91 case 3: return "STYLE_NO_INPUT (this window can't receive input, so "
92 + "you will need to press the bottom show button)";
93 case 4: return "STYLE_NORMAL with dark fullscreen theme";
94 case 5: return "STYLE_NORMAL with light theme";
95 case 6: return "STYLE_NO_TITLE with light theme";
96 case 7: return "STYLE_NO_FRAME with light theme";
97 case 8: return "STYLE_NORMAL with light fullscreen theme";
99 return "STYLE_NORMAL";
102 //BEGIN_INCLUDE(dialog)
103 public static class MyDialogFragment extends DialogFragment {
107 * Create a new instance of MyDialogFragment, providing "num"
110 static MyDialogFragment newInstance(int num) {
111 MyDialogFragment f = new MyDialogFragment();
113 // Supply num input as an argument.
114 Bundle args = new Bundle();
115 args.putInt("num", num);
116 f.setArguments(args);
122 public void onCreate(Bundle savedInstanceState) {
123 super.onCreate(savedInstanceState);
124 mNum = getArguments().getInt("num");
126 // Pick a style based on the num.
127 int style = DialogFragment.STYLE_NORMAL, theme = 0;
128 switch ((mNum-1)%6) {
129 case 1: style = DialogFragment.STYLE_NO_TITLE; break;
130 case 2: style = DialogFragment.STYLE_NO_FRAME; break;
131 case 3: style = DialogFragment.STYLE_NO_INPUT; break;
132 case 4: style = DialogFragment.STYLE_NORMAL; break;
133 case 5: style = DialogFragment.STYLE_NO_TITLE; break;
134 case 6: style = DialogFragment.STYLE_NO_FRAME; break;
135 case 7: style = DialogFragment.STYLE_NORMAL; break;
137 switch ((mNum-1)%6) {
138 case 2: theme = android.R.style.Theme_Panel; break;
139 case 4: theme = android.R.style.Theme; break;
140 case 5: theme = android.R.style.Theme_Light; break;
141 case 6: theme = android.R.style.Theme_Light_Panel; break;
142 case 7: theme = android.R.style.Theme_Light; break;
144 setStyle(style, theme);
148 public View onCreateView(LayoutInflater inflater, ViewGroup container,
149 Bundle savedInstanceState) {
150 View v = inflater.inflate(R.layout.fragment_dialog, container, false);
151 View tv = v.findViewById(R.id.text);
152 ((TextView)tv).setText("Dialog #" + mNum + ": using style "
153 + getNameForNum(mNum));
155 // Watch for button clicks.
156 Button button = (Button)v.findViewById(R.id.show);
157 button.setOnClickListener(new OnClickListener() {
158 public void onClick(View v) {
159 // When button is clicked, call up to owning activity.
160 ((FragmentDialogSupport)getActivity()).showDialog();
167 //END_INCLUDE(dialog)