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.ViewGroup; 29 import android.view.View.OnClickListener; 30 import android.widget.Button; 31 import android.widget.TextView; 32 33 public class FragmentDialogOrActivity extends Activity { 34 @Override onCreate(Bundle savedInstanceState)35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 setContentView(R.layout.fragment_dialog_or_activity); 38 39 if (savedInstanceState == null) { 40 // First-time init; create fragment to embed in activity. 41 //BEGIN_INCLUDE(embed) 42 FragmentTransaction ft = getFragmentManager().beginTransaction(); 43 DialogFragment newFragment = MyDialogFragment.newInstance(); 44 ft.add(R.id.embedded, newFragment); 45 ft.commit(); 46 //END_INCLUDE(embed) 47 } 48 49 // Watch for button clicks. 50 Button button = (Button)findViewById(R.id.show_dialog); 51 button.setOnClickListener(new OnClickListener() { 52 public void onClick(View v) { 53 showDialog(); 54 } 55 }); 56 } 57 58 //BEGIN_INCLUDE(show_dialog) showDialog()59 void showDialog() { 60 // Create the fragment and show it as a dialog. 61 DialogFragment newFragment = MyDialogFragment.newInstance(); 62 newFragment.show(getFragmentManager(), "dialog"); 63 } 64 //END_INCLUDE(show_dialog) 65 66 //BEGIN_INCLUDE(dialog) 67 public static class MyDialogFragment extends DialogFragment { newInstance()68 static MyDialogFragment newInstance() { 69 return new MyDialogFragment(); 70 } 71 72 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)73 public View onCreateView(LayoutInflater inflater, ViewGroup container, 74 Bundle savedInstanceState) { 75 View v = inflater.inflate(R.layout.hello_world, container, false); 76 View tv = v.findViewById(R.id.text); 77 ((TextView)tv).setText("This is an instance of MyDialogFragment"); 78 return v; 79 } 80 } 81 //END_INCLUDE(dialog) 82 } 83