Pages

Saturday, September 10, 2016

Android Example – Simple Alert Dialog in Button Click


activity_main.xml
<RelativeLayout 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: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=".MainActivity" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="SHOW DIALOG" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dialog 1"
        android:id="@+id/button2"
        android:layout_above="@+id/button"
        android:layout_marginBottom="69dp"
        android:layout_alignRight="@+id/button"
        android:layout_alignEnd="@+id/button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

MainActivity.java
package in.kantapp.example10;


import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
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 button= (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                // Create an alert dialog box
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        MainActivity.this);

                // Set alert title
                builder.setTitle("Alert Dialog!!");

                // Set the value for the positive reaction from the user
                // You can also set a listener to call when it is pressed
                builder.setPositiveButton("OK", null);

                // The message
                builder.setMessage("You Can Go Now");

                // Create the alert dialog and display it
                AlertDialog theAlertDialog = builder.create();
                theAlertDialog.show();
            }
        });

        Button button2= (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                AlertDialog.Builder builder2=new AlertDialog.Builder(MainActivity.this);
                builder2.setTitle("Exit The Window..");
                builder2.setMessage("Want to exit now!!");
                builder2.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(MainActivity.this, "Ok Fine", Toast.LENGTH_SHORT).show();
                    }
                });
                builder2.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        finish();
                    }
                });



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

    @Override
    public void onBackPressed()
    {
        Toast.makeText(MainActivity.this, "You Are not Allow to Exit", Toast.LENGTH_SHORT).show();
    }

}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.example10">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

No comments:

Post a Comment