XML File ( Layout File )
[ RelativeLayout, Button - 3 ]
<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/oldWayButton"
android:onClick="oldWay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/old_way"
tools:context=".MainActivity" />
<Button
android:id="@+id/qdWayButton"
android:onClick="qdWay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/oldWayButton"
android:layout_alignBottom="@+id/oldWayButton"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/oldWayButton"
android:padding="@dimen/padding_medium"
android:text="@string/qd_way"
tools:context=".MainActivity" />
<Button
android:id="@+id/dlgFragButton"
android:onClick="dlgFragWay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/oldWayButton"
android:layout_alignBottom="@+id/qdWayButton"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/oldWayButton"
android:padding="@dimen/padding_medium"
android:text="@string/dlgFragWay"
tools:context=".MainActivity" />
</RelativeLayout>
strings XML File
<resources>
<string name="app_name">DialogDemos</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="old_way">Old way</string>
<string name="qd_way">Q&D way</string>
<string name="unsaved_changes_title">Unsaved</string>
<string name="unsaved_changes_message">You have unsaved changes!</string>
<string name="save_changes">Save</string>
<string name="discard_changes">Discard</string>
<string name="cancel_dialog">Cancel</string>
<string name="dlgFragWay">DialogFragment</string>
</resources>
Java File ( Code )
package com.example.dialogdemos;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class MainActivity extends Activity {
private static final int DLG_UNSAVED = 1;
private static final String TAG = "DialogDemos";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@SuppressWarnings("deprecation")
public void oldWay(View v) {
showDialog(DLG_UNSAVED);
}
@Override
protected Dialog onCreateDialog(int id) {
switch(id) {
case DLG_UNSAVED:
final AlertDialog alertDialog = new AlertDialog.Builder(this)
.setTitle(R.string.unsaved_changes_title)
.setMessage(R.string.unsaved_changes_message)
.setPositiveButton(R.string.save_changes, new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// saveInformation(); // application-provided Save method.
}
})
.setNeutralButton(R.string.discard_changes, new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton(R.string.cancel_dialog, new AlertDialog.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.create();
return alertDialog;
default:
//@SuppressWarnings("deprecation")
return super.onCreateDialog(id);
}
}
public void qdWay(View v) {
Log.d(TAG, "Quick&Dirty Way invoked");
Dialog dlg = onCreateDialog(DLG_UNSAVED);
<div style="border-image-outset: initial; border-image-repeat: initial; border-image-slice: initial; border-image