2 * Copyright (C) 2010 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.apis.support.app;
19 import com.example.android.apis.R;
21 import android.support.v4.app.DialogFragment;
22 import android.support.v4.app.FragmentActivity;
23 import android.support.v4.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;
33 public class FragmentDialogOrActivitySupport extends FragmentActivity {
35 protected void onCreate(Bundle savedInstanceState) {
36 super.onCreate(savedInstanceState);
37 setContentView(R.layout.fragment_dialog_or_activity);
39 if (savedInstanceState == null) {
40 // First-time init; create fragment to embed in activity.
41 //BEGIN_INCLUDE(embed)
42 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
43 DialogFragment newFragment = MyDialogFragment.newInstance();
44 ft.add(R.id.embedded, newFragment);
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) {
58 //BEGIN_INCLUDE(show_dialog)
60 // Create the fragment and show it as a dialog.
61 DialogFragment newFragment = MyDialogFragment.newInstance();
62 newFragment.show(getSupportFragmentManager(), "dialog");
64 //END_INCLUDE(show_dialog)
66 //BEGIN_INCLUDE(dialog)
67 public static class MyDialogFragment extends DialogFragment {
68 static MyDialogFragment newInstance() {
69 return new MyDialogFragment();
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");