Android App Development | Lecture#30 | Hive Learners

๐“–๐“ป๐“ฎ๐“ฎ๐“ฝ๐“ฒ๐“ท๐“ฐ๐“ผ

Hello Hive Learners community members, I hope you all are well. Today we added some confirmation to our app. For example, if a action, we need to confirm by asking the user again that he wants to perform this action. May be user accidentally clicked that button. So we will use an AlertDialog today. Let's get started.

GitHub Link

Use this GitHub project to clone into your directory. The following lecture will constantly update it so you will never miss the latest code. Happy Coding!.

What Should I Learn

  • How to create AlertDialog
  • Add Buttons to AlertDialog

Assignment

  • Show an AlertDialog on the button click

Procedure

First, we need to declare and initialize the AlertDialog. I am going to add the AlertDialog in the Welcome Activity.

Now in the logout button clock listeners, I am adding the Alert Dialog and moving the listener code inside the AletDialog listener. Alert Dialog can haButtonsutton with listeners, Positive, Negative, and Neutral.

I set the title and message for the AlertDialog and in the end, write the show() function to show the AlerDialog on button click.

 alertDialog.setTitle("Confirmation");
                alertDialog.setMessage("Do you really want to logout?");

                alertDialog.setPositiveButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                }).setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        sharedPreferences.edit().remove("login_account").apply();
                        startActivity(new Intent(Welcome_Activity.this, MainActivity.class));
                        Toast.makeText(Welcome_Activity.this, "Logout Successfully", Toast.LENGTH_SHORT).show();
                        finish();

                    }
                });
                alertDialog.show();

When a user clicks on Yes then the code in the NegationButton listeners will run and If the user clicks on No then the Positive Listnere code run that will cancel the AlerDialog.

Now I am using the ProgressBAr code in the Yes Button and moving the Yes button code inside the ProfressBar. It will show a ProgressBar for two seconds and then execute the logout code.

 logout_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                alertDialog.setTitle("Confirmation");
                alertDialog.setMessage("Do you really want to logout?");

                alertDialog.setPositiveButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                }).setNegativeButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        progressDialog.show();
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if (progressDialog.isShowing()) {
                                    progressDialog.cancel();
                                }
                                sharedPreferences.edit().remove("login_account").apply();
                                startActivity(new Intent(Welcome_Activity.this, MainActivity.class));
                                Toast.makeText(Welcome_Activity.this, "Logout Successfully", Toast.LENGTH_SHORT).show();
                                finish();

                            }
                        }, 2000);

                    }
                });
                alertDialog.show();


            }
        });

Now let's run and check the app if the AlertDialog is showing or not.


hl_divider.png

Thank You

hl_footer_banner.png



0
0
0.000
5 comments
avatar

Good! Interesting the amount of code for an alert dialog
!1UP

You can earn passive income by delegation of tribe tokens to "The Cartel".

dlmmqb-TheCartel-banner
Click this banner to join "The Cartel" discord server to know more.

0
0
0.000
avatar

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.ย 
ย 

0
0
0.000
avatar
Thank you for sharing this amazing post on HIVE!
  • Your content got selected by our fellow curator @priyanarc & you just received a little thank you via an upvote from our non-profit curation initiative!

  • You will be featured in one of our recurring curation compilations and on our pinterest boards! Both are aiming to offer you a stage to widen your audience within and outside of the DIY scene of hive.

Join the official DIYHub community on HIVE and show us more of your amazing work and feel free to connect with us and other DIYers via our discord server: https://discord.gg/mY5uCfQ !

If you want to support our goal to motivate other DIY/art/music/homesteading/... creators just delegate to us and earn 100% of your curation rewards!

Stay creative & hive on!
0
0
0.000