I am new to Android and I have bound my application completely but I wish to a change now in it.
When someone sends an sms, I want to display a confirmation dialog for
use my class:
import java.util.ArrayList;
import org.mabna.order.R;
import org.mabna.order.dataModel.EntMapMovementsCollection;
import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MessageBox {
static ArrayList<AlertDialog> arrAlertDialog = new ArrayList<AlertDialog>();
static OnMyDialogResult mDialogResult; // the callback
public enum MessageBoxState {
OK, YesNo
}
public enum MessageBoxResult {
OK, Cancel, Yes, No
}
public final static void show(final Activity parent, String title,
String message, MessageBoxState state) {
// if (alertDialog != null)
// {
// alertDialog.dismiss();
// }
AlertDialog.Builder builder;
LayoutInflater inflater = (LayoutInflater) parent
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.dialog_messagebox,
(ViewGroup) parent.findViewById(R.id.layout_root));
layout.setMinimumWidth(400);
Typeface tf = Farsi.GetFarsiFont(parent);
TextView tvTitle = (TextView) layout.findViewById(R.id.tvTitle);
tvTitle.setText(Farsi.Convert(title));
tvTitle.setTypeface(tf);
tvTitle.setPadding(10, 10, 10, 10);
TextView tvMessage = (TextView) layout.findViewById(R.id.tvMessage);
tvMessage.setText(Farsi.Convert(message));
tvMessage.setTypeface(tf);
tvMessage.setPadding(10, 10, 10, 10);
Button btnOK = (Button) layout.findViewById(R.id.btnOK);
Button btnYes = (Button) layout.findViewById(R.id.btnYes);
Button btnNo = (Button) layout.findViewById(R.id.btnNo);
Button btnCancel = (Button) layout.findViewById(R.id.btnCancel);
btnOK.setText(Farsi.Convert("تایید"));
btnOK.setTypeface(tf);
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) btnOK
.getLayoutParams();
lp.setMargins(10, 10, 10, 10);
btnOK.setLayoutParams(lp);
btnYes.setText(Farsi.Convert("بله"));
btnYes.setTypeface(tf);
lp = (LinearLayout.LayoutParams) btnYes
.getLayoutParams();
lp.setMargins(10, 10, 10, 10);
btnYes.setLayoutParams(lp);
btnNo.setText(Farsi.Convert("خیر"));
btnNo.setTypeface(tf);
btnNo.setPadding(10, 10, 10, 10);
lp = (LinearLayout.LayoutParams) btnNo
.getLayoutParams();
lp.setMargins(10, 10, 10, 10);
btnNo.setLayoutParams(lp);
btnCancel.setText(Farsi.Convert("انصراف"));
btnCancel.setTypeface(tf);
btnCancel.setPadding(10, 10, 10, 10);
lp = (LinearLayout.LayoutParams) btnCancel
.getLayoutParams();
lp.setMargins(10, 10, 10, 10);
btnCancel.setLayoutParams(lp);
btnOK.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btnOK_onClick();
}
});
btnYes.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btnYes_onClick();
}
});
btnNo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btnNo_onClick();
}
});
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btnCancel_onClick();
}
});
LinearLayout layout_OK = (LinearLayout) layout
.findViewById(R.id.layout_OK);
LinearLayout layout_YesNo = (LinearLayout) layout
.findViewById(R.id.layout_YesNo);
switch (state) {
case OK:
layout_YesNo.setVisibility(LinearLayout.GONE);
layout_OK.setVisibility(LinearLayout.VISIBLE);
break;
case YesNo:
layout_YesNo.setVisibility(LinearLayout.VISIBLE);
layout_OK.setVisibility(LinearLayout.GONE);
break;
default:
break;
}
builder = new AlertDialog.Builder(parent);
builder.setView(layout);
AlertDialog alertDialog;
alertDialog = builder.create();
alertDialog.setCancelable(false);
alertDialog.show();
mDialogResult = null; // causes each messageBox instance has its own
// DialogResult because it removes previous
// DialogResults
arrAlertDialog.add(alertDialog);
return;
}
public static void showException(Activity act,
Exception e) {
MessageBox.show(act, "خطا", e.toString(), MessageBoxState.OK);
}
public static void showException(Activity act, String additionalDetails,
Exception e) {
MessageBox.show(act, "خطا", additionalDetails + ": " + e.toString(),
MessageBoxState.OK);
}
private static AlertDialog getLastDialog() {
AlertDialog dialog = arrAlertDialog.get(arrAlertDialog.size() - 1);
arrAlertDialog.remove(dialog);
return dialog;
}
private final static void btnOK_onClick() {
AlertDialog currentdialog = getLastDialog();
if (mDialogResult != null) {
mDialogResult.finish(MessageBoxResult.OK);
currentdialog.dismiss();
mDialogResult.dialogClosed();
} else {
currentdialog.dismiss();
}
}
private final static void btnYes_onClick() {
AlertDialog currentdialog = getLastDialog();
if (mDialogResult != null) {
mDialogResult.finish(MessageBoxResult.Yes);
currentdialog.dismiss();
mDialogResult.dialogClosed();
} else {
currentdialog.dismiss();
}
}
private final static void btnNo_onClick() {
AlertDialog currentdialog = getLastDialog();
if (mDialogResult != null) {
mDialogResult.finish(MessageBoxResult.No);
currentdialog.dismiss();
mDialogResult.dialogClosed();
} else {
currentdialog.dismiss();
}
}
private final static void btnCancel_onClick() {
AlertDialog currentdialog = getLastDialog();
if (mDialogResult != null) {
mDialogResult.finish(MessageBoxResult.Cancel);
currentdialog.dismiss();
mDialogResult.dialogClosed();
} else {
currentdialog.dismiss();
}
}
public final static void setDialogResult(OnMyDialogResult dialogResult) {
mDialogResult = dialogResult;
}
public interface OnMyDialogResult {
void finish(MessageBoxResult result);
void dialogClosed();
}
}
its layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:text="TextView"
android:textStyle="bold" >
</TextView>
<TextView
android:id="@+id/tvLine"
android:layout_width="fill_parent"
android:layout_height="2dip"
android:layout_weight="1"
android:background="@color/black"
android:text="" >
</TextView>
<TextView
android:id="@+id/tvMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:text="TextView" >
</TextView>
<LinearLayout
android:id="@+id/layout_YesNo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:gravity="center" >
<Button
android:id="@+id/btnCancel"
android:layout_width="70dip"
android:layout_height="40dip"
android:text="Button" >
</Button>
<Button
android:id="@+id/btnNo"
android:layout_width="70dip"
android:layout_height="40dip"
android:text="Button" />
<Button
android:id="@+id/btnYes"
android:layout_width="70dip"
android:layout_height="40dip"
android:text="Button" >
</Button>
</LinearLayout>
<LinearLayout
android:id="@+id/layout_OK"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:baselineAligned="true"
android:orientation="vertical" >
<Button
android:id="@+id/btnOK"
android:layout_width="80dip"
android:layout_height="40dip"
android:layout_gravity="center"
android:gravity="center"
android:text="Button" >
</Button>
</LinearLayout>
</LinearLayout>
and in Activity:
MessageBox.show(this, "warning", "Are you sure?", MessageBoxState.YesNo);
MessageBox.setDialogResult(new OnMyDialogResult() {
@Override
public void finish(MessageBoxResult result) {
if (result == MessageBoxResult.Yes)
//...
}
@Override
public void dialogClosed() {
}
});
dialog.setCancelable(true); should be like dialog.setCancelable(false);
It prevents closure of alert dialog when user presses back button :)