Open xml file and below code
Now open java file and paste below code
}
We can use maximum three buttons in dialog box by default and builder is used to create dialog box. Sequence of the buttons to appear on box: positive, neutral than negative.
<RelativeLayout xmlns:android="http://schemas. android.com/apk/res/android"
xmlns:tools="http://schemas. android.com/tools"
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_alignParentLeft ="true"
android:layout_alignParentTop= "true"
android:layout_marginLeft=" 24dp"
android:layout_marginTop=" 17dp"
android:text="CLOSE"
android:onClick="close" />
</RelativeLayout>Now open java file and paste below code
package selecom.alert;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.app.AlertDialog;
importandroid.app.AlertDialog. Builder;
importandroid.app.Dialog;
importandroid.content. DialogInterface;
importandroid.view.View;
importandroid.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState);
setContentView(R.layout.activi ty_main);
}
public void close(View v)
{
onCreateDialog(10);
}
protected Dialog onCreateDialog(int id)
{
switch(id)
{
case 10:
Builder builder=newAlertDialog.Builder(this);
builder.setMessage("Activity will close!!!");
builder.setTitle("Warning..." );
builder.setIcon(R.drawable.warning_image);
//button
builder.setNegativeButton(" No", new DialogInterface. OnClickListener() {
@Override
public voidonClick(DialogInterface dialog, int which)
{
Toast.makeText( getApplicationContext(), "Nothing happened", Toast.LENGTH_LONG).show();
}
});
//button
builder.setPositiveButton(" yes", new DialogInterface. OnClickListener() {
@Override
public voidonClick(DialogInterface dialog, int which)
{
finish();
}
});
//button
builder.setNeutralButton(" Thinking", newDialogInterface. OnClickListener() {
@Override
public voidonClick(DialogInterface dialog, int which)
{
Toast.makeText( getApplicationContext(), "i have to think", Toast.LENGTH_LONG).show();
}
});
AlertDialog dialog=builder.create();
dialog.show();
}
return super.onCreateDialog(id);
}
We can use maximum three buttons in dialog box by default and builder is used to create dialog box. Sequence of the buttons to appear on box: positive, neutral than negative.
0 comments:
Post a Comment