Sunday, August 16, 2015

How to use AlertDialog Builder in Android

activity_main.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp"
    tools:context=".MainActivity"
    android:background="#d2d452"
    >
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change Background Color"
        />
</RelativeLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;


public class MainActivity extends Activity {

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

        // Get reference of widgets from XML layout
        final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        Button btn = (Button) findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Build an AlertDialog
                /*
                    AlertDialog.Builder(Context context)
                        Constructor using a context for this builder
                        and the AlertDialog it creates.
                 */
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

                // Set a message/question for alert dialog
                /*
                    AlertDialog.Builder setMessage(CharSequence message)
                        Set the message to display.
                 */
                builder.setMessage("Want to change the background color?");

                // Specify the dialog is not cancelable
                /*
                    AlertDialog.Builder setCancelable(boolean cancelable)
                        Sets whether the dialog is cancelable or not.
                 */
                builder.setCancelable(false);

                // Set a title for alert dialog
                /*
                    AlertDialog.Builder setTitle(CharSequence title)
                        Set the title displayed in the Dialog.
                 */
                builder.setTitle("Title of AlertDialog.");

                // Set the positive/yes button click click listener
                /*
                    AlertDialog.Builder setPositiveButton(CharSequence text,
                    DialogInterface.OnClickListener listener)
                        Set a listener to be invoked when the positive button of the dialog is pressed.
                 */
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Do something when click positive button
                        rl.setBackgroundColor(Color.parseColor("#FF60D4A4"));
                        Toast.makeText(getApplicationContext(),
                                "Color Changed",Toast.LENGTH_SHORT).show();
                    }
                });

                // Set the negative/no button click click listener
                /*
                    AlertDialog.Builder setNegativeButton(CharSequence text,
                    DialogInterface.OnClickListener listener)
                        Set a listener to be invoked when the negative button of the dialog is pressed.
                 */
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Do something when click the negative button
                        Toast.makeText(getApplicationContext(),
                                "No Button Clicked",Toast.LENGTH_SHORT).show();
                    }
                });

                AlertDialog dialog = builder.create();
                // Display the alert dialog on interface
                dialog.show();
            }
        });
    }
}



More android examples