1 /* 2 * Copyright (C) 2011 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.supportv4.app; 18 19 import com.example.android.supportv4.R; 20 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; 25 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; 33 34 public class FragmentDialogSupport extends FragmentActivity { 35 int mStackLevel = 0; 36 37 @Override 38 protected void onCreate(Bundle savedInstanceState) { 39 super.onCreate(savedInstanceState); 40 setContentView(R.layout.fragment_dialog); 41 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."); 47 48 // Watch for button clicks. 49 Button button = (Button)findViewById(R.id.show); 50 button.setOnClickListener(new OnClickListener() { 51 public void onClick(View v) { 52 showDialog(); 53 } 54 }); 55 56 if (savedInstanceState != null) { 57 mStackLevel = savedInstanceState.getInt("level"); 58 } 59 } 60 61 @Override 62 public void onSaveInstanceState(Bundle outState) { 63 super.onSaveInstanceState(outState); 64 outState.putInt("level", mStackLevel); 65 } 66 67 //BEGIN_INCLUDE(add_dialog) 68 void showDialog() { 69 mStackLevel++; 70 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"); 76 if (prev != null) { 77 ft.remove(prev); 78 } 79 ft.addToBackStack(null); 80 81 // Create and show the dialog. 82 DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel); 83 newFragment.show(ft, "dialog"); 84 } 85 //END_INCLUDE(add_dialog) 86 87 static String getNameForNum(int num) { 88 switch ((num-1)%6) { 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"; 98 } 99 return "STYLE_NORMAL"; 100 } 101 102 //BEGIN_INCLUDE(dialog) 103 public static class MyDialogFragment extends DialogFragment { 104 int mNum; 105 106 /** 107 * Create a new instance of MyDialogFragment, providing "num" 108 * as an argument. 109 */ 110 static MyDialogFragment newInstance(int num) { 111 MyDialogFragment f = new MyDialogFragment(); 112 113 // Supply num input as an argument. 114 Bundle args = new Bundle(); 115 args.putInt("num", num); 116 f.setArguments(args); 117 118 return f; 119 } 120 121 @Override 122 public void onCreate(Bundle savedInstanceState) { 123 super.onCreate(savedInstanceState); 124 mNum = getArguments().getInt("num"); 125 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; 136 } 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; 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 ((FragmentDialogSupport)getActivity()).showDialog(); 161 } 162 }); 163 164 return v; 165 } 166 } 167 //END_INCLUDE(dialog) 168 } 169