Demonstrates how to show an AlertDialog that is managed by a Fragment. : Dialog « UI « Android






Demonstrates how to show an AlertDialog that is managed by a Fragment.

  
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package app.test;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * Demonstrates how to show an AlertDialog that is managed by a Fragment.
 */
public class Test extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        View tv = findViewById(R.id.text);
        ((TextView)tv).setText("Example of displaying an alert dialog with a DialogFragment");

        // Watch for button clicks.
        Button button = (Button)findViewById(R.id.show);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showDialog();
            }
        });
    }


    void showDialog() {
        DialogFragment newFragment = MyAlertDialogFragment.newInstance(1);
        newFragment.show(getFragmentManager(), "dialog");
    }

    public void doPositiveClick() {
        // Do stuff here.
        Log.i("FragmentAlertDialog", "Positive click!");
    }
    
    public void doNegativeClick() {
        // Do stuff here.
        Log.i("FragmentAlertDialog", "Negative click!");
    }

    

    public static class MyAlertDialogFragment extends DialogFragment {

        public static MyAlertDialogFragment newInstance(int title) {
            MyAlertDialogFragment frag = new MyAlertDialogFragment();
            Bundle args = new Bundle();
            args.putInt("title", title);
            frag.setArguments(args);
            return frag;
        }
        
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            int title = getArguments().getInt("title");
            
            return new AlertDialog.Builder(getActivity())
                    .setIcon(R.drawable.icon)
                    .setTitle(title)
                    .setPositiveButton("R.string.alert_dialog_ok",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                ((Test)getActivity()).doPositiveClick();
                            }
                        }
                    )
                    .setNegativeButton("R.string.alert_dialog_cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                                ((Test)getActivity()).doNegativeClick();
                            }
                        }
                    )
                    .create();
        }
    }

}
//main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2010 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<!-- Top-level content view for the simple fragment sample. -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:padding="4dip"
    android:gravity="center_horizontal"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center_vertical|center_horizontal"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:gravity="top|center_horizontal" />

    <Button android:id="@+id/show"
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="show">
        <requestFocus />
    </Button>

</LinearLayout>

   
    
  








Related examples in the same category

1.Dialog Helper
2.Use AlertDialog to inform exception
3.Add option item to Alert dialog and get user selection result
4.Add some padding to keep the dialog borders away
5.Dialog activity and TextView
6.Preference dialog
7.Show dialog
8.Dialog layout
9.Layout dialog with xml layout file
10.extends DialogFragment
11.Color Picker Dialog
12.Dialog Yes No Message
13.Dialog Yes No Old School Message
14.Dialog Yes No Holo Light Message
15.Dialog Yes No Long Message
16.Dialog Yes No Ultra Long Message
17.Dialog with List of value
18.Dialog with Progress
19.Dialog with Single Choice
20.Dialog Multiple Choice
21.Dialog Multiple Choice Cursor
22.Dialog with Text Entry
23.Dialog with Xml layout
24.Dialog Activity
25.Fragment Dialog
26.Demonstrates the use of progress dialogs.
27.File open dialog
28.Show error AlertDialog
29.extends DialogPreference
30.Show Notification Alert Dialog
31.Text Dialog
32.Create Chang Log Dialog
33.Show Title And Message Dialog
34.Show dialog and parse URL
35.Error Dialog Wrapper
36.Ok Dialog Wrapper
37.Display an alert dialog
38.AlertDialog Question
39.import android.app.AlertDialog;
40.dialog Builder
41.A dialog that allows the user to select multiple entries.
42.Help Dialog Creator
43.Color Select Dialog
44.Create MessageBox