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