• We just launched and are currently in beta. Join us as we build and grow the community.

Android - Simple Alert Dialog

jon waek

Crypto Compliance Analyst
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
127
Likes
60
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
Operating System

Android

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. <

    TextView
  9. android:

    layout_height=

    "wrap_content"
  10. android:

    layout_width=

    "wrap_content"
  11. android:

    text=

    "Simple Alert Dialog"
  12. android:

    textSize=

    "30sp"
  13. android:

    layout_alignParentTop=

    "true"
  14. android:

    layout_centerHorizontal=

    "true"
  15. android:

    layout_marginTop=

    "20dp"
  16. android:

    id=

    "@+id/tv_title"

    />

  17. <

    Button


  18. android:

    id=

    "@+id/btn_click"
  19. android:

    layout_width=

    "wrap_content"
  20. android:

    layout_height=

    "wrap_content"
  21. android:

    layout_centerInParent=

    "true"
  22. android:

    text=

    "Click Here"

    />

  23. </

    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. <

    application
  5. android:

    allowBackup=

    "true"
  6. android:

    icon=

    "@mipmap/ic_launcher"
  7. android:

    label=

    "@string/app_name"
  8. android:

    roundIcon=

    "@mipmap/ic_launcher_round"
  9. android:

    supportsRtl=

    "true"
  10. android:

    theme=

    "@Style/AppTheme"

    >
  11. <

    activity android:

    name=

    ".MainActivity"
  12. android:

    configChanges=

    "orientation"
  13. android:

    screenOrientation=

    "portrait"

    >
  14. <

    intent-

    filter>
  15. <

    action android:

    name=

    "android.intent.action.MAIN"

    />

  16. <

    category android:

    name=

    "android.intent.category.LAUNCHER"

    />
  17. </

    intent-

    filter>
  18. </

    activity>
  19. </

    application>
  20. </

    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. import

    android.app.Application

    ;
  3. import

    android.content.DialogInterface

    ;
  4. import

    android.support.v7.app.AlertDialog

    ;
  5. import

    android.support.v7.app.AppCompatActivity

    ;
  6. import

    android.os.Bundle

    ;
  7. import

    android.util.Log

    ;
  8. import

    android.view.View

    ;
  9. import

    android.widget.Button

    ;
  10. import

    android.widget.TextView

    ;

  11. public

    class

    MainActivity extends

    AppCompatActivity {

  12. Button

    btn_click;


  13. @Override
  14. protected

    void

    onCreate(

    Bundle savedInstanceState)

    {
  15. super

    .onCreate

    (

    savedInstanceState)

    ;
  16. setContentView(

    R.layout

    .activity_main

    )

    ;

  17. btn_click =

    (

    Button

    )

    findViewById(

    R.id

    .btn_click

    )

    ;

  18. btn_click.setOnClickListener

    (

    new

    View

    .OnClickListener

    (

    )

    {
  19. @Override
  20. public

    void

    onClick(

    View

    v)

    {

  21. AlertDialog.Builder

    builder=

    new

    AlertDialog.Builder

    (

    MainActivity.this

    )

    ;
  22. builder.setMessage

    (

    "Are you sure you want to exit?"

    )
  23. .setTitle

    (

    "System Configuration"

    )
  24. .setCancelable

    (

    false

    )
  25. .setPositiveButton

    (

    "YES"

    , new

    DialogInterface.OnClickListener

    (

    )

    {
  26. @Override
  27. public

    void

    onClick(

    DialogInterface dialog, int

    which)

    {
  28. moveTaskToBack(

    true

    )

    ;
  29. android.os

    .Process

    .killProcess

    (

    android.os

    .Process

    .myPid

    (

    )

    )

    ;
  30. System

    .exit

    (

    1

    )

    ;
  31. }

    }

    )
  32. .setNegativeButton

    (

    "NO"

    , new

    DialogInterface.OnClickListener

    (

    )

    {
  33. @Override
  34. public

    void

    onClick(

    DialogInterface dialog, int

    which)

    {
  35. dialog.cancel

    (

    )

    ;
  36. }
  37. }

    )

    ;

  38. AlertDialog alert =

    builder.create

    (

    )

    ;
  39. alert.show

    (

    )

    ;
  40. }
  41. }

    )

    ;
  42. }
  43. }

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.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

338,631

338,639

Top