Hi All, Let us know see, how to create an Alert Dialog inside another Alert Dialog?
Step 1: Create a Alert Dialog as i shown below and implement click Listener as i shown below
package com.san.test;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MyActivity extends Activity {
/** Called when the activity is first created. */
Context ctx=null;
Button b=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button )findViewById(R.id.test1);
ctx=this;
b.setOnClickListener(new android.view.View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
displayAlert();
}}
);
}
public void displayAlert()
{
OnClickListener btnOnClick = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
new AlertDialog.Builder(ctx).setMessage("BUTTON_POSITIVE")
.setTitle("Alert Postive ")
.setCancelable(true)
.setNeutralButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
finish();
}
})
.show();
} else if(which == DialogInterface.BUTTON_NEGATIVE){
new AlertDialog.Builder(ctx).setMessage("BUTTON_NEGATIVE")
.setTitle("Alert Negative")
.setCancelable(true)
.setNeutralButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
finish();
}
})
.show();
} else if(which == DialogInterface.BUTTON_NEUTRAL) {
// More Info
new AlertDialog.Builder(ctx).setMessage("BUTTON_NEUTRAL")
.setTitle("Alert Neutral")
.setCancelable(true)
.setNeutralButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
finish();
}
})
.show();
}
}
};
AlertDialog promptInstall = new AlertDialog.Builder(ctx).setTitle("First Alert")
.setMessage(
"mY First Alert")
.setPositiveButton("OK", btnOnClick).setNegativeButton(
"Cancel", btnOnClick).setNeutralButton("More Info",
btnOnClick).create();
promptInstall.show();
}
}
See the images
Step 1: Create a Alert Dialog as i shown below and implement click Listener as i shown below
package com.san.test;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MyActivity extends Activity {
/** Called when the activity is first created. */
Context ctx=null;
Button b=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button )findViewById(R.id.test1);
ctx=this;
b.setOnClickListener(new android.view.View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
displayAlert();
}}
);
}
public void displayAlert()
{
OnClickListener btnOnClick = new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
new AlertDialog.Builder(ctx).setMessage("BUTTON_POSITIVE")
.setTitle("Alert Postive ")
.setCancelable(true)
.setNeutralButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
finish();
}
})
.show();
} else if(which == DialogInterface.BUTTON_NEGATIVE){
new AlertDialog.Builder(ctx).setMessage("BUTTON_NEGATIVE")
.setTitle("Alert Negative")
.setCancelable(true)
.setNeutralButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
finish();
}
})
.show();
} else if(which == DialogInterface.BUTTON_NEUTRAL) {
// More Info
new AlertDialog.Builder(ctx).setMessage("BUTTON_NEUTRAL")
.setTitle("Alert Neutral")
.setCancelable(true)
.setNeutralButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){
finish();
}
})
.show();
}
}
};
AlertDialog promptInstall = new AlertDialog.Builder(ctx).setTitle("First Alert")
.setMessage(
"mY First Alert")
.setPositiveButton("OK", btnOnClick).setNegativeButton(
"Cancel", btnOnClick).setNeutralButton("More Info",
btnOnClick).create();
promptInstall.show();
}
}
See the images