- add new button on the layout:
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:text="Delete file" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> </LinearLayout>Modyfy your main activity as follow:
2. Add imports:
import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button;
3. override the onCreateDialog method :
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
switch (id) {
case ALERT_DIALOG_ID:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Delete file");
builder.setMessage("Are you shure ?");
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// perform action for Yes button
}
});
builder.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// perform action for No button
}
});
builder.setNeutralButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// perform action for Cancel button
}
});
dialog = builder.create();
break;
default:
return null;
}
return dialog;
}
4. define the unique dialog ID into your Activity class:final int ALERT_DIALOG_ID = 0;5. Modify the onCreate method :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(ALERT_DIALOG_ID);
}
});
}
0 comentarii:
Post a Comment