Android Open Source - androidui Alert Dialog Activity






From Project

Back to project page androidui.

License

The source code is released under:

MIT License

If you think the Android project androidui listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package course.examples.UI.AlertDialog;
/*from   ww  w.j a  va  2s  . co m*/
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.ProgressDialog;
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;

public class AlertDialogActivity extends Activity {

  // Identifier for each type of Dialog
  private static final int ALERTTAG = 0, PROGRESSTAG = 1;

  private static final String TAG = "AlertDialogActivity";
  private Button mShutdownButton = null;
  private DialogFragment mDialog;

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

    // ShutDown Button
    mShutdownButton = (Button) findViewById(R.id.shutdownButton);
    mShutdownButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        showDialogFragment(ALERTTAG);
      }
    });
  }

  // Show desired Dialog
  void showDialogFragment(int dialogID) {

    switch (dialogID) {

    // Show AlertDialog
    case ALERTTAG:

      // Create a new AlertDialogFragment
      mDialog = AlertDialogFragment.newInstance();

      // Show AlertDialogFragment
      mDialog.show(getFragmentManager(), "Alert");

      break;

    // Show ProgressDialog
    case PROGRESSTAG:

      // Create a new ProgressDialogFragment
      mDialog = ProgressDialogFragment.newInstance();

      // Show new ProgressDialogFragment
      mDialog.show(getFragmentManager(), "Shutdown");
      break;
    }
  }

  // Abort or complete ShutDown based on value of shouldContinue
  private void continueShutdown(boolean shouldContinue) {
    if (shouldContinue) {

      // Prevent further interaction with the ShutDown Button
      mShutdownButton.setEnabled(false);
      
      // Show ProgressDialog as shutdown process begins 
      showDialogFragment(PROGRESSTAG);
      
      // Finish the ShutDown process
      finishShutdown();
    
    } else {

      // Abort ShutDown and dismiss dialog
      mDialog.dismiss();
    }
  }

  private void finishShutdown() {
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          // Pretend to do something before
          // shutting down
          Thread.sleep(5000);
        } catch (InterruptedException e) {
          Log.i(TAG, e.toString());
        } finally {
          finish();
        }
      }
    }).start();
  }

  // Class that creates the AlertDialog
  public static class AlertDialogFragment extends DialogFragment {

    public static AlertDialogFragment newInstance() {
      return new AlertDialogFragment();
    }

    // Build AlertDialog using AlertDialog.Builder
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
      return new AlertDialog.Builder(getActivity())
          .setMessage(getString(R.string.exit_confirm_string))
          
          // User cannot dismiss dialog by hitting back button
          .setCancelable(false)
          
          // Set up No Button
          .setNegativeButton("No",
              new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                    int id) {
                  ((AlertDialogActivity) getActivity())
                      .continueShutdown(false);
                }
              })
              
          // Set up Yes Button
          .setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(
                                        final DialogInterface dialog, int id) {
                                    ((AlertDialogActivity) getActivity())
                                            .continueShutdown(true);
                                }
                            }).create();
    }
  }

  // Class that creates the ProgressDialog
  public static class ProgressDialogFragment extends DialogFragment {

    public static ProgressDialogFragment newInstance() {
      return new ProgressDialogFragment();
    }

    // Build ProgressDialog
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

      //Create new ProgressDialog
      final ProgressDialog dialog = new ProgressDialog(getActivity());
      
      // Set Dialog message
      dialog.setMessage("Activity Shutting Down.");
      
      // Dialog will be displayed for an unknown amount of time
      dialog.setIndeterminate(true);

      return dialog;
    }
  }
}




Java Source Code List

com.example.aporter.helloandroidwithimageview.HelloAndroidWithImageViewActivity.java
course.examples.Fragments.DynamicLayout.QuoteViewerActivity.java
course.examples.Fragments.DynamicLayout.QuotesFragment.java
course.examples.Fragments.DynamicLayout.TitlesFragment.java
course.examples.Notification.StatusBarWithCustomView.NotificationSpecialActivity.java
course.examples.Notification.StatusBarWithCustomView.NotificationStatusBarWithExpandedViewActivity.java
course.examples.Notification.Toast.NotificationToastActivity.java
course.examples.Notification.ToastWithCustomView.NotificationToastActivity.java
course.examples.UI.AlertDialog.AlertDialogActivity.java
course.examples.UI.AutoComplete.AutoCompleteActivity.java
course.examples.UI.Button.ButtonActivity.java
course.examples.UI.CheckBox.CheckBoxActivity.java
course.examples.UI.GridView.GridLayoutActivity.java
course.examples.UI.GridView.ImageAdapter.java
course.examples.UI.GridView.ImageViewActivity.java
course.examples.UI.LinearLayout.LinearLayoutActivity.java
course.examples.UI.ListLayout.ListViewActivity.java
course.examples.UI.ListLayout.ListViewAdapter.java
course.examples.UI.MapView.GoogleMapActivity.java
course.examples.UI.RadioGroup.RadioGroupActivity.java
course.examples.UI.RatingsBar.RatingsBarActivity.java
course.examples.UI.RecyclerView.MyRecyclerViewAdapter.java
course.examples.UI.RecyclerView.RecyclerViewActivity.java
course.examples.UI.RelativeLayout.RelativeLayoutActivity.java
course.examples.UI.Spinner.SpinnerActivity.java
course.examples.UI.TabLayout.GridFragment.java
course.examples.UI.TabLayout.ImageAdapter.java
course.examples.UI.TabLayout.ImageViewActivity.java
course.examples.UI.TabLayout.TabLayoutActivity.java
course.examples.UI.TableLayout.TableLayoutActivity.java
course.examples.UI.ViewPager.GalleryWithViewPagerActivity.java
course.examples.UI.ViewPager.ImageAdapter.java
course.examples.UI.ViewPager.ImageHolderFragment.java
course.examples.UI.WebView.WebViewActivity.java
course.examples.UI.datepicker.DatePickerFragmentActivity.java
course.examples.UI.timepicker.TimePickerFragmentActivity.java
course.examples.UI.togglebutton.ToggleButtonActivity.java
course.examples.colorpalettewithnavdrawer.ApplicationTest.java
course.examples.colorpalettewithnavdrawer.DisplayColorActivity.java
course.examples.colorpalettewithnavdrawer.DisplaySingleColorActivity.java
course.examples.colorpalettewithnavdrawer.PaletteNameAdapter.java
course.examples.colorpalettewithswipe.ApplicationTest.java
course.examples.colorpalettewithswipe.DisplayColorPaletteActivity.java
course.examples.colorpalettewithswipe.DisplaySingleColorActivity.java
course.examples.colorpalettewithswipe.PaletteAdapter.java
course.examples.fragments.StaticLayout.QuoteViewerActivity.java
course.examples.fragments.StaticLayout.QuotesFragment.java
course.examples.fragments.StaticLayout.TitlesFragment.java
course.examples.fragments.staticconfiglayout.QuoteViewerActivity.java
course.examples.fragments.staticconfiglayout.QuotesFragment.java
course.examples.fragments.staticconfiglayout.TitlesFragment.java
course.examples.helloandroidwithlogin.ApplicationTest.java
course.examples.helloandroidwithlogin.HelloAndroidWithImageViewActivity.java
course.examples.helloandroidwithlogin.LoginActivity.java
course.examples.modernartpiano.MainActivity.java
course.examples.modernartui.MainActivity.java
course.examples.notification.StatusBar.NotificationStatusBarActivity.java
course.examples.notification.StatusBar.NotificationSubActivity.java
course.examples.quoteviewer.QuoteListActivity.java
course.examples.quoteviewer.TitlesListActivity.java
course.examples.ui.fragmentactionbar.QuoteFragment.java
course.examples.ui.fragmentactionbar.QuoteViewerActivity.java
course.examples.ui.fragmentactionbar.TitlesFragment.java
course.examples.ui.helloworldwithmenus.HelloAndroidWithMenuActivity.java
course.labs.multipane.MainActivity.java
course.labs.multipane.QuoteFragment.java
course.labs.multipane.TitlesFragment.java
course.labs.placebadges.MockLocationProvider.java
course.labs.placebadges.PlaceDownloaderTask.java
course.labs.placebadges.PlaceRecord.java
course.labs.placebadges.PlaceViewActivity.java
course.labs.placebadges.PlaceViewAdapter.java
course.labs.placebadges.PlaceViewDetailActivity.java
course.labs.placebadges_prel.MockLocationProvider.java
course.labs.placebadges_prel.PlaceDownloaderTask.java
course.labs.placebadges_prel.PlaceRecord.java
course.labs.placebadges_prel.PlaceViewActivity.java
course.labs.placebadges_prel.PlaceViewAdapter.java
course.labs.placebadges_prel.PlaceViewDetailActivity.java
examples.course.basiccolorpalette.ApplicationTest.java
examples.course.basiccolorpalette.DisplayColorActivity.java
examples.course.basiccolorpalette.DisplayColorNames.java
examples.course.basiccolorpalette.DisplaySingleColorActivity.java
examples.course.basiccolorpalette.PaletteAdapter.java
examples.course.basiccolorpaletteupnav.ApplicationTest.java
examples.course.basiccolorpaletteupnav.DisplayColorActivity.java
examples.course.basiccolorpaletteupnav.DisplayColorNamesActivity.java
examples.course.basiccolorpaletteupnav.DisplaySingleColorActivity.java
examples.course.basiccolorpaletteupnav.PaletteAdapter.java
examples.course.ticker.TickerDisplayActivity.java
examples.course.uicardview.ApplicationTest.java
examples.course.uicardview.CardViewActivity.java