1 /* 2 * Copyright (C) 2010 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 */ 16 17 package com.example.android.apis.app; 18 19 import com.example.android.apis.R; 20 21 import android.app.Activity; 22 import android.app.DialogFragment; 23 import android.app.Fragment; 24 import android.app.FragmentTransaction; 25 import android.os.Bundle; 26 import android.view.LayoutInflater; 27 import android.view.View; 28 import android.view.View.OnClickListener; 29 import android.view.ViewGroup; 30 import android.widget.Button; 31 import android.widget.TextView; 32 33 public class FragmentDialog extends Activity { 34 int mStackLevel = 0; 35 36 @Override 37 protected void onCreate(Bundle savedInstanceState) { 38 super.onCreate(savedInstanceState); 39 setContentView(R.layout.fragment_dialog); 40 41 View tv = findViewById(R.id.text); 42 ((TextView)tv).setText("Example of displaying dialogs with a DialogFragment. " 43 + "Press the show button below to see the first dialog; pressing " 44 + "successive show buttons will display other dialog styles as a " 45 + "stack, with dismissing or back going to the previous dialog."); 46 47 // Watch for button clicks. 48 Button button = (Button)findViewById(R.id.show); 49 button.setOnClickListener(new OnClickListener() { 50 public void onClick(View v) { 51 showDialog(); 52 } 53 }); 54 55 if (savedInstanceState != null) { 56 mStackLevel = savedInstanceState.getInt("level"); 57 } 58 } 59 60 @Override 61 public void onSaveInstanceState(Bundle outState) { 62 super.onSaveInstanceState(outState); 63 outState.putInt("level", mStackLevel); 64 } 65 66 //BEGIN_INCLUDE(add_dialog) 67 void showDialog() { 68 mStackLevel++; 69 70 // DialogFragment.show() will take care of adding the fragment 71 // in a transaction. We also want to remove any currently showing 72 // dialog, so make our own transaction and take care of that here. 73 FragmentTransaction ft = getFragmentManager().beginTransaction(); 74 Fragment prev = getFragmentManager().findFragmentByTag("dialog"); 75 if (prev != null) { 76 ft.remove(prev); 77 } 78 ft.addToBackStack(null); 79 80 // Create and show the dialog. 81 DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel); 82 newFragment.show(ft, "dialog"); 83 } 84 //END_INCLUDE(add_dialog) 85 86 static String getNameForNum(int num) { 87 switch ((num-1)%6) { 88 case 1: return "STYLE_NO_TITLE"; 89 case 2: return "STYLE_NO_FRAME"; 90 case 3: return "STYLE_NO_INPUT (this window can't receive input, so " 91 + "you will need to press the bottom show button)"; 92 case 4: return "STYLE_NORMAL with dark fullscreen theme"; 93 case 5: return "STYLE_NORMAL with light theme"; 94 case 6: return "STYLE_NO_TITLE with light theme"; 95 case 7: return "STYLE_NO_FRAME with light theme"; 96 case 8: return "STYLE_NORMAL with light fullscreen theme"; 97 } 98 return "STYLE_NORMAL"; 99 } 100 101 //BEGIN_INCLUDE(dialog) 102 public static class MyDialogFragment extends DialogFragment { 103 int mNum; 104 105 /** 106 * Create a new instance of MyDialogFragment, providing "num" 107 * as an argument. 108 */ 109 static MyDialogFragment newInstance(int num) { 110 MyDialogFragment f = new MyDialogFragment(); 111 112 // Supply num input as an argument. 113 Bundle args = new Bundle(); 114 args.putInt("num", num); 115 f.setArguments(args); 116 117 return f; 118 } 119 120 @Override 121 public void onCreate(Bundle savedInstanceState) { 122 super.onCreate(savedInstanceState); 123 mNum = getArguments().getInt("num"); 124 125 // Pick a style based on the num. 126 int style = DialogFragment.STYLE_NORMAL, theme = 0; 127 switch ((mNum-1)%6) { 128 case 1: style = DialogFragment.STYLE_NO_TITLE; break; 129 case 2: style = DialogFragment.STYLE_NO_FRAME; break; 130 case 3: style = DialogFragment.STYLE_NO_INPUT; break; 131 case 4: style = DialogFragment.STYLE_NORMAL; break; 132 case 5: style = DialogFragment.STYLE_NORMAL; break; 133 case 6: style = DialogFragment.STYLE_NO_TITLE; break; 134 case 7: style = DialogFragment.STYLE_NO_FRAME; break; 135 case 8: style = DialogFragment.STYLE_NORMAL; break; 136 } 137 switch ((mNum-1)%6) { 138 case 4: theme = android.R.style.Theme_Holo; break; 139 case 5: theme = android.R.style.Theme_Holo_Light_Dialog; break; 140 case 6: theme = android.R.style.Theme_Holo_Light; break; 141 case 7: theme = android.R.style.Theme_Holo_Light_Panel; break; 142 case 8: theme = android.R.style.Theme_Holo_Light; break; 143 } 144 setStyle(style, theme); 145 } 146 147 @Override 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)); 154 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 ((FragmentDialog)getActivity()).showDialog(); 161 } 162 }); 163 164 return v; 165 } 166 } 167 //END_INCLUDE(dialog) 168 } 169