Friday 27 April 2012

AlertDialog Box With Two Buttons In Android

A dialog is usually a small window that appears in front of the current Activity. The underlying Activity loses focus and the dialog accepts all user interaction. Dialogs are normally used for notifications that should interupt the user and to perform short tasks that directly relate to the application in progress (such as a progress bar or a login prompt).
 
Declare the AlertDialog 

            Builder b=new AlertDialog.Builder(this);

Set Title

            b.setTitle("Class Changing Activity....");

 Set Message

            b.setMessage("Do you want to go next class ?");

 Button Creation 

Here we setting the two buttons with 
setPositiveButton()
setNegativeButton() methods

b.setPositiveButton("YES", new DialogInterface.OnClickListener() {
               
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                startActivity(new Intent(AlertDialog1Activity.this, SecondActivity.class));
                }
         });

b.setNegativeButton("No", new DialogInterface.OnClickListener() {
           
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
               Toast.LENGTH_LONG).show();
                AlertDialog1Activity.this.finish();
            }
        });


 Create and Show the AlertDialog 

AlertDialog d=b.create();
        d.show();

Example Program


package com.android.alert1;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class AlertDialog1Activity extends Activity {
    Button btn;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //btn=(Button)findViewById(R.id.button1);
       
    }
    public void MyClick(View v){
        showDialog(10);
       
        }   
    public Dialog onCreateDialog(int id){
        switch(id){
        case 10:
            Builder b=new AlertDialog.Builder(this);
            b.setMessage("Do you want to go next class ?");
            b.setCancelable(true);
            b.setPositiveButton("YES", new DialogInterface.OnClickListener() {
               
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                startActivity(new Intent(AlertDialog1Activity.this, SecondActivity.class));
                }
            });
        b.setNegativeButton("No", new DialogInterface.OnClickListener() {
           
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                //Toast.makeText(getApplicationContext(), "This will continue", Toast.LENGTH_LONG).show();
                AlertDialog1Activity.this.finish();
            }
        });
        AlertDialog d=b.create();
        d.show();
                }
              
        return super.onCreateDialog(id);
       
    }
}

SecondActivity.class


package com.android.alert1;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class SecondActivity extends Activity{
   

        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.sec);
                   
                }
}



main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button android:text="Show Message"
       android:layout_marginTop="120dp"
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         android:clickable="true"
          android:onClick="MyClick"
          android:textStyle="bold"></Button>
</LinearLayout>

sec.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:onClick="onClick" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the Second Page"
        android:layout_marginTop="200dp"
        android:layout_marginLeft="40dp"
        android:textColor="#FF0000"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceLarge" />
   
</LinearLayout>























AlertDialog in Android

A dialog is usually a small window that appears in front of the current Activity. The underlying Activity loses focus and the dialog accepts all user interaction. Dialogs are normally used for notifications that should interupt the user and to perform short tasks that directly relate to the application in progress (such as a progress bar or a login prompt).


AlertDialog box with Single Button


Here is an example which showing how we can create AlertDialog box in android.
Creation

AlertDialog alert=new AlertDialog.Builder(AlertDialogExp.this).create();

where AlertDialogExp.this is the main class.

Set Title
You can set the title of the AlertDialog like this:

alert.setTitle("Next Activityyy...");

Set Message
you can set the message body of the AlertDialog obx like this:

alert.setMessage("do you want to go net Activity?");
Set Button(S)
Next step is setting the button/buttons:


alert.setButton("Ok",new DialogInterface.OnClickListener() {
           
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Intent i=new Intent(AlertDialgExpActivity.this,second.class);
                startActivity(i);
            }
        });


Show your dialog

Don't forget to show your dialog

alert.show();

Example Program

AlertDialgExpActivity.class
package com.ann;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AlertDialgExpActivity extends Activity {
    //Button b;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
        public void onClickExp(View v){
             // b=(Button)findViewById(R.id.button);
            AlertDialog alert=new AlertDialog.Builder(this).create();
            alert.setTitle("Next Activityyy...");
          
           alert.setMessage("Do you want to go next Activity?");
           alert.setButton("Ok",new DialogInterface.OnClickListener() {
           
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Intent i=new Intent(AlertDialgExpActivity.this,second.class);
                startActivity(i);
            }
        });
           alert.show();
    }
}


second.class

package com.ann;

import android.app.Activity;
import android.os.Bundle;

public class second extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sec);
    }

}

main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click the button to view AlertDialog"
        android:textColor="#FF0000"
        android:textStyle="bold"
        android:layout_marginTop="120dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="82dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="120dp"
        android:layout_marginTop="50dp"
        android:onClick="onClickExp"
        android:text="Click"
        android:textColor="#FF0000" />

</LinearLayout>


sec.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

   
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the Second Page"
        android:layout_marginTop="200dp"
        android:layout_marginLeft="40dp"
        android:textColor="#FF0000"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceLarge" />
   
</LinearLayout>