/* Swisscom Safe Connect Copyright (C) 2014 Swisscom This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.swisscom.safeconnect.fragment; import android.content.Context; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.TextView; import com.swisscom.safeconnect.BuildConfig; import com.swisscom.safeconnect.R; import com.swisscom.safeconnect.utils.Fonts; public class CustomAlertDialogBuilder extends AlertDialog.Builder { public CustomAlertDialogBuilder(Context context) { super(context, R.style.AppCompat_Pipe_Dialog_Alert); } public static AlertDialog showInfoDlg(Context ctx, int titleId, int msgId) { return showInfoDlg(ctx, titleId, msgId, null, null); } public static AlertDialog showInfoDlg(Context ctx, int titleId, int msgId, DialogInterface.OnClickListener closeButtonListener) { return showInfoDlg(ctx, titleId, msgId, closeButtonListener, null); } public static AlertDialog showInfoDlg(Context ctx, int titleId, int msgId, DialogInterface.OnClickListener closeButtonListener, boolean showCancelButton) { return showInfoDlg(ctx, titleId, msgId, closeButtonListener, null, showCancelButton); } public static AlertDialog showInfoDlg(Context ctx, int titleId, int msgId, DialogInterface.OnClickListener closeButtonListener, DialogInterface.OnCancelListener onCancelListener) { return showInfoDlg(ctx, titleId, msgId, closeButtonListener, null, false); } public static AlertDialog showInfoDlg(Context ctx, int titleId, int msgId, DialogInterface.OnClickListener closeButtonListener, DialogInterface.OnCancelListener onCancelListener, boolean showCancelButton) { CustomAlertDialogBuilder dlgBuilder = new CustomAlertDialogBuilder(ctx); dlgBuilder.setTitle(titleId); dlgBuilder.setMessage(msgId); dlgBuilder.setPositiveButton(R.string.lab_btn_ok, closeButtonListener); dlgBuilder.setOnCancelListener(onCancelListener); if (showCancelButton) { dlgBuilder.setNegativeButton(R.string.lab_btn_cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); } return dlgBuilder.createCustomAlertDialog(); //shows it } /* public static AlertDialog showYesNoDlg(Context ctx, int titleId, int msgId, DialogInterface.OnClickListener yesButtonListener, DialogInterface.OnClickListener noButtonListener) { CustomAlertDialogBuilder dlgBuilder = new CustomAlertDialogBuilder(ctx); dlgBuilder.setTitle(titleId); dlgBuilder.setMessage(msgId); dlgBuilder.setPositiveButton(R.string.btn_yes, yesButtonListener); dlgBuilder.setNegativeButton(R.string.btn_no, noButtonListener); return dlgBuilder.createCustomAlertDialog(); //shows it } */ public AlertDialog createCustomAlertDialog() { AlertDialog dialog = show(); dialog.setCanceledOnTouchOutside(false); // customize title final int alertTitleId = getContext().getResources().getIdentifier( "alertTitle", "id", "android"); TextView tvTitle = (TextView) dialog.findViewById(alertTitleId); if (tvTitle != null) { tvTitle.setTypeface(Fonts.FONT_BOLD); tvTitle.setTextColor(getContext().getResources(). getColor(R.color.navy)); } // customize message TextView tvMessage = (TextView) dialog .findViewById(android.R.id.message); if (tvMessage != null) tvMessage.setTypeface(Fonts.FONT_NORMAL); // customize buttons Button btnPositive = (Button) dialog.findViewById(android.R.id.button1); if (btnPositive != null) { styleButton(btnPositive); } Button btnNegative = (Button) dialog.findViewById(android.R.id.button2); if (btnNegative != null) { styleButton(btnNegative); } /* final int dividerId = getContext().getResources().getIdentifier( "titleDivider", "id", "android"); View divider = dialog.findViewById(dividerId); if (divider != null) { divider.setBackgroundColor(getContext().getResources().getColor(R.color.navy)); } */ return dialog; } private void styleButton(Button btn) { btn.setTypeface(Fonts.FONT_BOLD); /*btn.setBackgroundResource(R.drawable.btn_filled_bg_selector); btn.setTextColor(getContext().getResources().getColor(android.R.color.white)); */ } @Override public AlertDialog.Builder setSingleChoiceItems(CharSequence[] items, int checkedItem, DialogInterface.OnClickListener listener) { CustomAdapter ca = new CustomAdapter(this.getContext(), android.R.layout.select_dialog_singlechoice, android.R.id.text1, items); return super.setSingleChoiceItems(ca, checkedItem, listener); } public class CustomAdapter extends ArrayAdapter<CharSequence> { private int mResource; private LayoutInflater mInflater; private int mFieldId; public CustomAdapter(Context context, int resource, int textViewResourceId, CharSequence[] items) { super(context, resource, textViewResourceId, items); this.mResource = resource; this.mInflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.mFieldId = textViewResourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { View v = createViewFromResource(position, convertView, parent, mResource); TextView tv = (TextView) v.findViewById(mFieldId); tv.setTypeface(Fonts.FONT_NORMAL); return v; } private View createViewFromResource(int position, View convertView, ViewGroup parent, int resource) { View view; TextView text; if (convertView == null) { view = mInflater.inflate(resource, parent, false); } else { view = convertView; } try { if (mFieldId == 0) { // If no custom field is assigned, assume the whole resource // is a TextView text = (TextView) view; } else { // Otherwise, find the TextView field within the layout text = (TextView) view.findViewById(mFieldId); } } catch (ClassCastException e) { if (BuildConfig.DEBUG) Log.e("ArrayAdapter", "You must supply a resource ID for a TextView"); throw new IllegalStateException( "ArrayAdapter requires the resource ID to be a TextView", e); } CharSequence item = getItem(position); text.setText(item); text.setTypeface(Fonts.FONT_NORMAL); return view; } } public class CustomAlertDialog extends AlertDialog { protected CustomAlertDialog(Context context) { super(context); } public AlertDialog getDialog() { return this; } } }