0

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 ?

pheromix
  • 18,213
  • 29
  • 88
  • 158

2 Answers2

1

If you use SpannableStringBuilder you can't use toString() because otherwise you lost the bold formatting you just added.

Change your method bold to return SpannedString

public class Formatage {

    public Formatage() {
        super();
    }

    public static SpannedString bold(String text, String boldText) {
        int start = text.length() + 1;

        SpannableStringBuilder ssb = new SpannableStringBuilder(text + " " + boldText);
        StyleSpan boldSpan = new StyleSpan(Typeface.BOLD);
        ssb.setSpan(boldSpan, start, start + boldText.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        return new SpannedString(ssb);
    }
}

Now you need to change your activity method to support the new bold method:

private boolean chpObligVide() {
    if (TextUtils.isEmpty(identification.getText().toString())) {
        champOblig = getResources().getString(R.string.identification)); 
        return true;
    }
    champOblig = "";
    return false;
}

private void displaychpObligError() {
    dlg.setTitre(getResources().getString(R.string.titreErrMsgBox));
    dlg.setMsg(Formatage.bold(getResources().getString(R.string.chpOblig), champOblig));
    dlg.show();
}

In the end edit MessageDialogView to accept CharSequence instead String:

public void setMsg(CharSequence text) {
    if (text != null)
        ((TextView)contenu.findViewById(R.id.msgText)).setText(text);
}
Mattia Maestrini
  • 32,270
  • 15
  • 87
  • 94
0

Basically, you can set 'Bold' style in the xml, or in the code.

Set TextView style (bold or italic)

If you do in the code, I'll do like this,

public void setMsg(String text){
    if (text != null){
        TextView textView = ((TextView)contenu.findViewById(R.id.msgText));
        textView.setText(text);            
        textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
    }
}

In the xml,

<TextView
    android:id="@+id/msgText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    style="@style/ImpotsStyleTransparent"
    android:gravity="center"
    android:textStyle="bold"
    android:paddingTop="35dp"
    android:paddingBottom="35dp" />
Community
  • 1
  • 1
Tay Cleed
  • 69
  • 8
  • It is not the entire text that I want to bold , but the last part of it ! Look : `dlg.setMsg(getResources().getString(R.string.chpOblig) + " " + champOblig);` – pheromix Apr 16 '15 at 16:11