// AlertDialogDemoActivity.java (c) Kari Laitinen // http://www.naturalprogramming.com // 2013-04-02 File created. // 2015-03-17 Latest modification. // A long press on the touch display shows an alert dialog. // This program also shows how an activity can terminate itself. // When creating a project for this app, set the Minimum Required SDK // to something like Android 4.0. This program will not work on old // Androids. // This program is a simplified version of the program shown at: // http://developer.android.com/reference/android/app/DialogFragment.html // To run this program, create a project and set AlertDialogDemoActivity as // its main activity, and use the package name that is used here. // The you can use this file to overwrite the .java file created by // Android Studio. package alert.dialog.demo ; import android.app.Activity ; import android.os.Bundle ; import android.graphics.* ; // Classes Canvas, Color, Paint, RectF, etc. import android.view.View ; import android.view.MotionEvent ; import android.view.GestureDetector ; import android.content.Context ; import java.util.ArrayList ; import android.app.AlertDialog ; import android.app.Dialog ; import android.content.DialogInterface ; import android.app.DialogFragment ; class AlertDialogDemoView extends View implements GestureDetector.OnGestureListener { GestureDetector gesture_detector ; public AlertDialogDemoView( Context context ) { super( context ) ; gesture_detector = new GestureDetector( context, this ) ; } public boolean onDown( MotionEvent motion_event ) { return true ; } public boolean onFling( MotionEvent first_down_motion, MotionEvent last_move_motion, float velocity_x, float velocity_y ) { return true ; } public void onLongPress( MotionEvent first_down_motion ) { ((AlertDialogDemoActivity) getContext()).show_alert_dialog() ; } public boolean onScroll( MotionEvent first_down_motion, MotionEvent last_move_motion, float distance_x, float distance_y ) { return true ; } public void onShowPress( MotionEvent down_motion ) { } public boolean onSingleTapUp( MotionEvent up_motion ) { return true ; } public boolean onTouchEvent ( MotionEvent motion_event ) { gesture_detector.onTouchEvent( motion_event ) ; return true ; } protected void onDraw( Canvas canvas ) { Paint outline_paint = new Paint() ; outline_paint.setStyle( Paint.Style.STROKE ) ; outline_paint.setTextSize( 24 ) ; // Default color for a Paint is black. canvas.drawText( "LONG PRESS SHOWS AN ALERT DIALOG", 20, 80, outline_paint ) ; } } public class AlertDialogDemoActivity extends Activity { public static class AlertDialogFragment extends DialogFragment { public static AlertDialogFragment newInstance( String given_title ) { AlertDialogFragment alert_dialog_fragment = new AlertDialogFragment() ; Bundle arguments = new Bundle() ; arguments.putString( "title", given_title ) ; alert_dialog_fragment.setArguments( arguments ) ; return alert_dialog_fragment ; } @Override public Dialog onCreateDialog( Bundle savedInstanceState ) { String dialog_title = getArguments().getString( "title" ) ; return new AlertDialog.Builder( getActivity() ) //.setIcon(R.drawable.alert_dialog_icon) .setTitle( dialog_title ) .setPositiveButton( "Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton ) { ((AlertDialogDemoActivity)getActivity()).do_positive_things() ; } } ) .setNegativeButton( "No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton ) { ((AlertDialogDemoActivity)getActivity()).do_negative_things() ; } } ) .create() ; } } public void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ) ; setContentView( new AlertDialogDemoView( this ) ) ; } // The following method will be called from the View-based class. public void show_alert_dialog() { DialogFragment alert_dialog_fragment = AlertDialogFragment.newInstance( "EXIT THIS ACTIVITY ?" ) ; alert_dialog_fragment.show( getFragmentManager(), "dialog" ) ; } public void do_positive_things() { finish() ; // This will finish the entire activity. } public void do_negative_things() { } }