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>