Showing posts with label How to Display an Alert Dialog in a Fragment. Show all posts
Showing posts with label How to Display an Alert Dialog in a Fragment. Show all posts

Tuesday, August 19, 2014

How to Display an Alert Dialog in a Fragment

 Display an Alert Dialog in a Fragment

//SplashScreenActivity.class is your Launcher Activity
 // In Case of Activity instead of fragment Replace getActivity() with getApplicationContext()

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setCancelable(false);
            builder.setTitle(Html.fromHtml("<font color='#7F02AE'><b>Star Cineplex</b></font>"));
            builder.setMessage(Html.fromHtml("<font color='#120049'>Your data services are not working.Please check your data services.</font>"));
            builder.setPositiveButton(Html.fromHtml("<font color='#7F02AE'><b>OK</b></font>"), new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                                         
  Intent intent = new Intent(getActivity(), SplashScreenActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        intent.putExtra("EXIT", true);
                        startActivity(intent);
            
                                  
                   
                }
            });
                   AlertDialog alert=builder.create();
            alert.show();

Now at OnCreate of the Launcher Activity(eg. SplashScreenActivity.class)

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loading_image_mn);
        //To exit
        if (getIntent().getBooleanExtra("EXIT", false)) {
            SplashScreenActivity.this.finish();
            System.exit(0);
        }
Like the following: