There is an AlertDialog
:
public class MessageDialogView extends AlertDialog {
private View contenu, titleBar;
@SuppressLint("InlinedApi")
public MessageDialogView(Context context, LayoutInflater inflater) {
super(context, AlertDialog.THEME_HOLO_DARK);
contenu = inflater.inflate(R.layout.msg_dialog, null);
titleBar = inflater.inflate(R.layout.custom_dialog_title, null);
setCustomTitle(titleBar);
setView(contenu, 0, 0, 0, 0);
setButton(DialogInterface.BUTTON_POSITIVE, context.getResources().getString(R.string.button_ok), new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
}
public void setIcone(int resId) {
((ImageView)titleBar.findViewById(R.id.icone)).setImageResource(resId);
}
public void setTitre(String titre) {
if (titre != null)
((TextView)titleBar.findViewById(R.id.titre)).setText(titre);
}
public void setMsg(String text){
if (text != null)
((TextView)contenu.findViewById(R.id.msgText)).setText(text);
}
}
Layout msg_dialog :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
style="@style/ImpotsStyle" >
<TextView
android:id="@+id/msgText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="@style/ImpotsStyleTransparent"
android:gravity="center"
android:paddingTop="35dp"
android:paddingBottom="35dp" />
</LinearLayout>
layout custom_dialog_title :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/ImpotsTitleStyle"
android:layout_marginBottom="0dp"
android:padding="0dp">
<ImageView
android:id="@+id/icone"
android:src="@drawable/ic_action_warning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:contentDescription="@string/descr_alert_icone"/>
<TextView
android:id="@+id/titre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
style="@style/ImpotsStyleText"/>
</RelativeLayout>
I want to show this AlertDialog
inside an activity
:
...
private MessageDialogView dlg = null;
private String champOblig = "";
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.parcelle);
db = new Db(ParcelleActivity.this).open();
afficherParcelle();
dlg = new MessageDialogView(ParcelleActivity.this, getLayoutInflater());
}
public void submit(View vue) { // onClick of a button
if (chpObligVide()) {
displaychpObligError();
return;
}
...
}
private boolean chpObligVide() {
if (TextUtils.isEmpty(identification.getText().toString())) {
champOblig = Formatage.bold(getResources().getString(R.string.identification)); // here I want to make the text to be bold
return true;
}
champOblig = "";
return false;
}
private void displaychpObligError() {
dlg.setTitre(getResources().getString(R.string.titreErrMsgBox));
dlg.setMsg(getResources().getString(R.string.chpOblig) + " " + champOblig);
dlg.show();
}
Class Formatage :
public class Formatage {
public Formatage() {
super();
}
public static String bold(String text) {
SpannableStringBuilder ssb = new SpannableStringBuilder(text);
StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
ssb.setSpan(boldSpan, 0, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
return ssb.toString();
}
}
So how to make the text to be bold
when shown inside the AlertDialog
?