• Privacy Policy
  • Contact us
  • Register
  • Log In
  • Facebook
  • Pinterest
  • YouTube
  • WhatsUp
Nulled Source Code - NSC
  • Home
  • Mobile Apps
  • PHP Scripts
  • Paid Projects
  • WordPress Theme
Nulled Source Code - NSC
  • Home
  • Mobile Apps
  • PHP Scripts
  • Paid Projects
  • WordPress Theme

Android Widgets

  • Android DatePicker Example And Tutorial
  • Android SeekBar Exmple and Tutorial
  • Android WebView Example and Tutorial
  • Android RatingBar Tutorial and Example
  • Android ListView Tutorial and Example
  • Android Spinner Tutorial and Examples
  • Android Alert Dialog Tutorial and Example
  • Android Radio Button Tutorials
  • Android CheckBox Tutorial
  • Android Toggle Buttons
  • Android Toast Example
  • Types of Android Button and Examples
  • About Android Studio Widgets

About Android

  • How to Setup Different Android Emulators?
  • What is Android Architecture?
  • History of Android Studio and Version
  • What is Android Studio and Installation
  • Home
  • Courses
  • Android Widgets
  • Android Alert Dialog Tutorial and Example

Android Alert Dialog Tutorial and Example

Download Premium Version Free
Android Alert Dialog Tutorial and Example - step by step beginner totorial for Android Studio - NSC - Nulled Source Code

Aa the name suggest Alert means it will show a alert message to user before proceeding to take any action using a small winodws known as Android Dialog and hence the name Android Alert Dialog. You can create either default alert dialog or also can make your own custom alert dialog. Click here to Know about Custom Alert Dialog in Android Studio.

Creating an Alert will include three things such as: A Title, Content Area and Action buttons as showing below:

Android Alert Dialog Tutorial and Example - step by step beginner totorial for Android Studio
Alert Dialog Body Parts

Alert Dialog Parts:

Title:- It is the name of your content or dialog which is related to. It is optional to set title.

Content Area:- This incude the message or information which you want to show to the users. In the content area you can use anything like a button, slider, image, textview, Radio Button, Checkbox, List, EditText etc

Action Buttons:- An Alert dialog consist of maximum only three button such as: Positive, Negative and Neutral. Postive button will act like an OK button means it will use to accept and continue the app. And a Negatiev Button is used to cancel the dialog or message or refuse anything. And Neutral button is used when the user Dont want to accept (Positve) or Refuse (Negative) then you can use third button as Remind me Later which will show the dialog after some time.

How to add alert in app:

To add Android Alert Dialog you should use “AlertDialog.Builder” in your MainActivity.java file as:

AlertDialog.Builder myDialog;

Then add a button to your layout file to call or show the dialog:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">
   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textStyle="bold"
       android:onClick="ExitApp"
       android:padding="5dp"
       android:background="@drawable/gradeint"
       android:textColor="@color/white"
       android:text="Click Here to Exit "/>
</LinearLayout>
Android Alert Dialog Tutorial and Example - step by step beginner totorial for Android Studio - NSC
Button to show Alert Dialog

Now you can copy the below code to make a alert dialog every line is commented which helps you to learn quickly:

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Color;
import com.your.packagename.R;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;

public class MainActivity extends Activity {

    AlertDialog.Builder myDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myDialog = new AlertDialog.Builder(this);  // this is for Activity
    //  myDialog = new AlertDialog.Builder(getActivity());  // this is for fragment, to use fragment just comment above line and comment out this line
    }
    public void showToast (String message){
        Toast toast = Toast.makeText(this, message, Toast.LENGTH_LONG);
        View tv = toast.getView();
        tv.setPadding(20, 15, 20, 15);
        TextView toastMessage = (TextView) tv.findViewById(android.R.id.message);
        toastMessage.setTextSize(25);
        toastMessage.setTextColor(Color.WHITE);
        toastMessage.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_play, 0, 0, 0);
        toastMessage.setGravity(Gravity.CENTER);
        toastMessage.setCompoundDrawablePadding(16);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            tv.setBackground(getDrawable(R.drawable.gradeint));
        }
        toast.show();
    }

    public void ExitApp(View view) {
        myDialog.setTitle("App Close Dialog");  // dialog title
        myDialog.setMessage("Are you sure you want to close the app?")  // dialog message or content are
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {  // action button positive
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish(); // close the app
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {  // action button negative
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.cancel();  // cancel the dialog
                    }
                })
                .setNeutralButton("Remid Later", new DialogInterface.OnClickListener() {  // action button neutral
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        showToast("OK Remind you later");
                        dialogInterface.cancel();
                    }
                });
        AlertDialog alertDialog = myDialog.create();  // Create alert dialog box with dialog window
        alertDialog.show();  // finally show the alert dialog
    }
}

below screenshot showing how you dialod will appeare:

Android Alert Dialog Tutorial and Example - step by step beginner totorial for Android Studio - NSC - Nulled Source Code
Complete Alert Dialog Example
Alert Dialog
What are your Feelings
What are your Feelings
Share This Article :
  • Facebook
  • Twitter
  • LinkedIn
  • Pinterest
Updated on April 25, 2021
Android Radio Button TutorialsAndroid Spinner Tutorial and Examples

Leave a Comment X

You must be logged in to post a comment.

Copyright © 2023.Nuled Source Code.

  • Native Android
  • Paid Projects
  • Flutter
  • My Tickets
  • Register
  • Log In
  • Register
Loading...
 

Loading Comments...
 

You must be logged in to post a comment.

    Insert/edit link

    Enter the destination URL

    Or link to existing content

      No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.