Home | History | Annotate | Download | only in app
      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.supportv4.app;
     18 
     19 import com.example.android.supportv4.R;
     20 
     21 import android.os.Bundle;
     22 import android.support.v4.app.DialogFragment;
     23 import android.support.v4.app.FragmentActivity;
     24 import android.support.v4.app.FragmentTransaction;
     25 import android.view.LayoutInflater;
     26 import android.view.View;
     27 import android.view.View.OnClickListener;
     28 import android.view.ViewGroup;
     29 import android.widget.Button;
     30 import android.widget.TextView;
     31 
     32 public 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