Download the New LearnVern Android App and enter Lucky Draw to win Free Certificate
If you are facing any issue in playing video please use Firefox browser.
Now we will see practical how we can design Alert dialog in Android.
Step:1 First of all, we will create an application named 'alertdialog'. And now we will design its code.
Step:2 Now we will see its manifest file, which is the default generated and is named 'AndroidManifest.xml'.
Step:3 After that we will design its layout, for that we will open the file 'activity_main.xml' in the 'layout' folder, then inside it we have used relative layout, whose basic properties we already set, and then simply we have taken a text view, which has set basic properties such as height, width.
Step:4 After this we will see the 'strings.xml' file under the 'values' folder, here we have set some string which we will use inside our 'activity_main.xml'. Basic things we have specified in string.xml.
Step:5 Now we have to design our main activity, for that we will see our MainActivity file inside 'java' folder, which is default generated, so now we have to design an alert dialog here.
Step:6 First of all, we will create Builder and we will give its instance 'builder', and firstly we will pass 'this' from it.
Step:7 Now with the help of the instance we have created, we will set the title.
Step:8 Now we will set the message, whatever message we want to set, we can set it here,
builder.setMessage(""Do you want to close the app?"");
Step:9 Now we can add some more properties like this
.setPositiveButton (""Yes"",new DialogInterface().OnClickListener());
Step:10 This is done for the positive button but if the user wants to press no, then
Step:11 Now we will create an alert dialog,
AlertDialog alert-builder.creat ();
Step:12 So in this way, the code of our alert dialog has been designed, now let us execute it. When it is executed alert dialog is running correctly.
Have a friend to whom you would want to share this course?