Android - Simple Alert Dialog

Operating System

In this tutorial we will try to create a Simple Alert Dialog Using Android. Android is the world’s most widely used operating system with over a millions of user, that are already installed to smartphones, tablets, and even television. Android is an open source so that developer find it easy to develop and expand new features. So let's do the coding...

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things. Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first locate the activity_main.xml and click text to view the script. Then copy and paste the code below.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="com.razormist.simplealertdialog.MainActivity">
  8.  
  9.  
  10. <TextView
  11. android:layout_height="wrap_content"
  12. android:layout_width="wrap_content"
  13. android:text="Simple Alert Dialog"
  14. android:textSize="30sp"
  15. android:layout_alignParentTop="true"
  16. android:layout_centerHorizontal="true"
  17. android:layout_marginTop="20dp"
  18. android:id="@+id/tv_title"/>
  19.  
  20. android:id="@+id/btn_click"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_centerInParent="true"
  24. android:text="Click Here"/>
  25.  
  26. </RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code.
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.razormist.simplealertdialog">
  4.  
  5. <application
  6. android:allowBackup="true"
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:roundIcon="@mipmap/ic_launcher_round"
  10. android:supportsRtl="true"
  11. android:theme="@style/AppTheme">
  12. <activity android:name=".MainActivity"
  13. android:configChanges="orientation"
  14. android:screenOrientation="portrait">
  15. <intent-filter>
  16. <action android:name="android.intent.action.MAIN" />
  17.  
  18. <category android:name="android.intent.category.LAUNCHER" />
  19. </intent-filter>
  20. </activity>
  21. </application>
  22. </manifest>

The Main Function

This code contains the main function of the application. This code will generate a dialog to prompt the user to what to do next when the button is clicked. To create first locate your java file and open it, then write these blocks of code.
  1. package com.razormist.simplealertdialog;
  2.  
  3. import android.app.Application;
  4. import android.content.DialogInterface;
  5. import android.support.v7.app.AlertDialog;
  6. import android.support.v7.app.AppCompatActivity;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.TextView;
  12.  
  13. public class MainActivity extends AppCompatActivity {
  14.  
  15. Button btn_click;
  16.  
  17.  
  18. @Override
  19. protected void onCreate(Bundle savedInstanceState) {
  20. super.onCreate(savedInstanceState);
  21. setContentView(R.layout.activity_main);
  22.  
  23. btn_click = (Button)findViewById(R.id.btn_click);
  24.  
  25. btn_click.setOnClickListener(new View.OnClickListener() {
  26. @Override
  27. public void onClick(View v) {
  28.  
  29. AlertDialog.Builder builder= new AlertDialog.Builder(MainActivity.this);
  30. builder.setMessage("Are you sure you want to exit?")
  31. .setTitle("System Configuration")
  32. .setCancelable(false)
  33. .setPositiveButton("YES", new DialogInterface.OnClickListener() {
  34. @Override
  35. public void onClick(DialogInterface dialog, int which) {
  36. moveTaskToBack(true);
  37. android.os.Process.killProcess(android.os.Process.myPid());
  38. System.exit(1);
  39. }})
  40. .setNegativeButton("NO", new DialogInterface.OnClickListener() {
  41. @Override
  42. public void onClick(DialogInterface dialog, int which) {
  43. dialog.cancel();
  44. }
  45. });
  46.  
  47. AlertDialog alert = builder.create();
  48. alert.show();
  49. }
  50. });
  51. }
  52. }
There you have it we successfully created a Simple Alert Dialog using android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Tags

Add new comment