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 android.os.Bundle; 20 import android.view.LayoutInflater; 21 import android.view.View; 22 import android.view.View.OnClickListener; 23 import android.view.ViewGroup; 24 import android.widget.Button; 25 import android.widget.TextView; 26 27 import androidx.fragment.app.DialogFragment; 28 import androidx.fragment.app.Fragment; 29 import androidx.fragment.app.FragmentActivity; 30 import androidx.fragment.app.FragmentTransaction; 31 32 import com.example.android.supportv4.R; 33 34 public class FragmentDialogSupport extends FragmentActivity { 35 int mStackLevel = 0; 36 37 @Override onCreate(Bundle savedInstanceState)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 @Override 52 public void onClick(View v) { 53 showDialog(); 54 } 55 }); 56 57 if (savedInstanceState != null) { 58 mStackLevel = savedInstanceState.getInt("level"); 59 } 60 } 61 62 @Override onSaveInstanceState(Bundle outState)63 public void onSaveInstanceState(Bundle outState) { 64 super.onSaveInstanceState(outState); 65 outState.putInt("level", mStackLevel); 66 } 67 68 //BEGIN_INCLUDE(add_dialog) showDialog()69 void showDialog() { 70 mStackLevel++; 71 72 // DialogFragment.show() will take care of adding the fragment 73 // in a transaction. We also want to remove any currently showing 74 // dialog, so make our own transaction and take care of that here. 75 FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 76 Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog"); 77 if (prev != null) { 78 ft.remove(prev); 79 } 80 ft.addToBackStack(null); 81 82 // Create and show the dialog. 83 DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel); 84 newFragment.show(ft, "dialog"); 85 } 86 //END_INCLUDE(add_dialog) 87 getNameForNum(int num)88 static String getNameForNum(int num) { 89 switch ((num-1)%6) { 90 case 1: return "STYLE_NO_TITLE"; 91 case 2: return "STYLE_NO_FRAME"; 92 case 3: return "STYLE_NO_INPUT (this window can't receive input, so " 93 + "you will need to press the bottom show button)"; 94 case 4: return "STYLE_NORMAL with dark fullscreen theme"; 95 case 5: return "STYLE_NORMAL with light theme"; 96 case 6: return "STYLE_NO_TITLE with light theme"; 97 case 7: return "STYLE_NO_FRAME with light theme"; 98 case 8: return "STYLE_NORMAL with light fullscreen theme"; 99 } 100 return "STYLE_NORMAL"; 101 } 102 103 //BEGIN_INCLUDE(dialog) 104 public static class MyDialogFragment extends DialogFragment { 105 int mNum; 106 107 /** 108 * Create a new instance of MyDialogFragment, providing "num" 109 * as an argument. 110 */ newInstance(int num)111 static MyDialogFragment newInstance(int num) { 112 MyDialogFragment f = new MyDialogFragment(); 113 114 // Supply num input as an argument. 115 Bundle args = new Bundle(); 116 args.putInt("num", num); 117 f.setArguments(args); 118 119 return f; 120 } 121 122 @Override onCreate(Bundle savedInstanceState)123 public void onCreate(Bundle savedInstanceState) { 124 super.onCreate(savedInstanceState); 125 mNum = getArguments().getInt("num"); 126 127 // Pick a style based on the num. 128 int style = DialogFragment.STYLE_NORMAL, theme = 0; 129 switch ((mNum-1)%6) { 130 case 1: style = DialogFragment.STYLE_NO_TITLE; break; 131 case 2: style = DialogFragment.STYLE_NO_FRAME; break; 132 case 3: style = DialogFragment.STYLE_NO_INPUT; break; 133 case 4: style = DialogFragment.STYLE_NORMAL; break; 134 case 5: style = DialogFragment.STYLE_NO_TITLE; break; 135 case 6: style = DialogFragment.STYLE_NO_FRAME; break; 136 case 7: style = DialogFragment.STYLE_NORMAL; break; 137 } 138 switch ((mNum-1)%6) { 139 case 2: theme = android.R.style.Theme_Panel; break; 140 case 4: theme = android.R.style.Theme; break; 141 case 5: theme = android.R.style.Theme_Light; break; 142 case 6: theme = android.R.style.Theme_Light_Panel; break; 143 case 7: theme = android.R.style.Theme_Light; break; 144 } 145 setStyle(style, theme); 146 } 147 148 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)149 public View onCreateView(LayoutInflater inflater, ViewGroup container, 150 Bundle savedInstanceState) { 151 View v = inflater.inflate(R.layout.fragment_dialog, container, false); 152 View tv = v.findViewById(R.id.text); 153 ((TextView)tv).setText("Dialog #" + mNum + ": using style " 154 + getNameForNum(mNum)); 155 156 // Watch for button clicks. 157 Button button = (Button)v.findViewById(R.id.show); 158 button.setOnClickListener(new OnClickListener() { 159 @Override 160 public void onClick(View v) { 161 // When button is clicked, call up to owning activity. 162 ((FragmentDialogSupport)getActivity()).showDialog(); 163 } 164 }); 165 166 return v; 167 } 168 } 169 //END_INCLUDE(dialog) 170 } 171