Pages

Friday, May 6, 2016

Android-AlertDialogTutorial

MainActivity.java
package kantapp.android_alertdialogtutorial;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button alert_1=(Button)findViewById(R.id.alert_1);
        alert_1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {

                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);

                builder.setPositiveButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "Try Again", Toast.LENGTH_SHORT).show();
                    }
                }).setNegativeButton("yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });//.setMessage("Want to Exit");

                AlertDialog alertDialog=builder.create();
                //alertDialog.setTitle("Only Negative and positive");
                alertDialog.show();
            }
        });

       Button alert_2=(Button)findViewById(R.id.alert_2);
        alert_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                AlertDialog.Builder alert2=new AlertDialog.Builder(MainActivity.this);

                AlertDialog alertDialog=alert2.create();
                alertDialog.setTitle("This is Only TiTle");
                alertDialog.show();
            }
        });

        Button alert_3=(Button)findViewById(R.id.alert_3);
        alert_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                AlertDialog.Builder alert2=new AlertDialog.Builder(MainActivity.this);

                alert2.setMessage("This is Only Messege Alert");

                AlertDialog alertDialog=alert2.create();
                alertDialog.show();
            }
        });

        Button alert=(Button)findViewById(R.id.alert);
        alert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);

                builder.setPositiveButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this, "Try Again", Toast.LENGTH_SHORT).show();
                    }
                }).setNegativeButton("yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                });

                builder.setMessage("This is Only Messege Alert");
                builder.setTitle("This is Only TiTle");

                AlertDialog alertDialog=builder.create();
                alertDialog.show();
            }
        });
    }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="kantapp.android_alertdialogtutorial.MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/alert_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Alert 1" />

        <Button
            android:id="@+id/alert_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Alert 2" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/alert_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Alert 3" />

        <Button
            android:id="@+id/alert"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Complete Alert" />
    </LinearLayout>


</LinearLayout>

Download Complete Project :
https://userscloud.com/radx2cr5kppv

Download Only Source Code:
https://userscloud.com/gfofr9vdkezd

No comments:

Post a Comment