Search code examples
javaandroidhtmlandroid-edittextspannablestring

Adding additional text to Spanned String gets rid of my Bold text.


I'm taking user input and adding that text as bold text in an edittext box on an alert dialog. The problem is, when the user adds more text a second or third or fourth time (etc), the previous bolded text goes away and only the latest bolded text shows. Help is appreciated, thank you.

Here is my code:

newField.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            storyText = story.getText().toString();


            AlertDialog.Builder builder = new AlertDialog.Builder(
                    getActivity());
            builder.setTitle("Title");

            // Set up the input
            final EditText input = new EditText(getActivity());
            input.setHint("Ex: Noun, Verb, Color, etc");
            // Specify the type of input expected; this, for example, sets
            // the input as a password, and will mask the text
            builder.setView(input);

            // Set up the buttons
            builder.setPositiveButton("Add Field",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            int y;
                            for(int i = 0; i < 200; i++)
                            {
                                field[i] = "";
                            }
                            for(y = 0; y < 200; y++)
                            {
                                if(field[y].equals(""))
                                {
                                    field[y] = input.getText().toString();
                                    break;
                                }
                            }


                            storyText = storyText + " " + "<b>" + field[y] + "</b>" + " ";
                            storyText.replaceAll("\n", "<br />");
                            spanned = Html.fromHtml(storyText);
                            story.setText(spanned);

                            Toast.makeText(
                                    getActivity().getApplicationContext(),
                                    "Field Added", Toast.LENGTH_SHORT).show();
                        }
                    });
            builder.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog,
                                int which) {
                            dialog.cancel();
                        }
                    });

            builder.show();


        }
    });

Solution

  • You are getting the String rather than the SpannableString from the TextView, so you are losing your formatting.

    Currently you have something like:

    String storyText = story.getText().toString();
    // ...
    storyText = storyText + " " + "<b>" + field[y] + "</b>" + " ";
    storyText.replaceAll("\n", "<br />");
    spanned = Html.fromHtml(storyText);
    story.setText(spanned);
    

    But you might want to try something like:

    SpannableString spannedStoryText = new SpannableString(story.getText());
    // ...
    String additionalStoryText = " " + "<b>" + field[y] + "</b>" + " ";
    additionalStoryText.replaceAll("\n", "<br />"); // assuming you only want this for the appended text.
    spannedStoryText = new SpannableString(TextUtils.concat(spannedStoryText, Html.fromHtml(additonalStoryText)));
    story.setText(spannedStoryText);