Are you wan to create Yes/No Dialog Box in Android. Here is the simple example to create yes / no dialog box when button click event.
first you have to defined on xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="CreateYesNo" />
</RelativeLayout>
Now , Define button and click with display dialog box .
public class YesNoButton extends Activity {
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
Button btnYesNo;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.yesno);
btnYesNo=(Button) findViewById(R.id.button1);
btnYesNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayYesNo();
}
});
}
protected void displayYesNo() {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
Toast.makeText(getApplicationContext(), "Yes Button Click", Toast.LENGTH_SHORT).show();
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
Toast.makeText(getApplicationContext(), "NO Button Click", Toast.LENGTH_SHORT).show();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
}
Here is the snapshots of this application:
Happy Coding!!!
first you have to defined on xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="CreateYesNo" />
</RelativeLayout>
Now , Define button and click with display dialog box .
public class YesNoButton extends Activity {
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
Button btnYesNo;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.yesno);
btnYesNo=(Button) findViewById(R.id.button1);
btnYesNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
displayYesNo();
}
});
}
protected void displayYesNo() {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
Toast.makeText(getApplicationContext(), "Yes Button Click", Toast.LENGTH_SHORT).show();
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
Toast.makeText(getApplicationContext(), "NO Button Click", Toast.LENGTH_SHORT).show();
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
}
}
Here is the snapshots of this application:
Happy Coding!!!