Question

the user should be able to create something like the following while typing inside the edittext itself.

"this text is Bold and this is italics ."

I don't want the whole text to be in any one format(ie. bold/italics/underline).

thank you.

Was it helpful?

Solution

Here is the solution which can help u .. first get the selected data of editext (on bold/italic/underline button click):

EditText et=(EditText)findViewById(R.id.edit);
String text = et.getText();

int startSelection=et.getSelectionStart();
int endSelection=et.getSelectionEnd();

String selectedText = et.getText().toString().substring(startSelection, endSelection);

There is 2 way to set the style in edit text : [here option check is the button which got click ]

1) use the html

if(option.bold) 
{
   text = text.replace(selectedText , "<b>"+ (selectedText + "</b>");
   et.setText(Html.fromHtml(text));

 }
if(option.italic)  
{
    text = text.replace(selectedText , "<i>"+ (selectedText + "</i>");
    et.setText(Html.fromHtml(text));  
}
if(option.underline)  
{
    text = text.replace(selectedText , "<u>"+ (selectedText + "</u>");
    et.setText(Html.fromHtml(text));  
}

2) use the Spannable

 SpannableString span = new SpannableString(text);

 if(option.underline) 
           { 
     span.setSpan(new UnderlineSpan(),startSelection, endSelection , 0); 
      }
   if(option.bold) 
    { 
       span.setSpan(new StyleSpan(Typeface.BOLD), startSelection, endSelection ,  0);
      }
    if(option.italic) 
        {   
       span.setSpan(new StyleSpan(Typeface.ITALIC), startSelection, endSelection , 0);
      }
   et.setText(span, TextView.BufferType.SPANNABLE);

OTHER TIPS

this is how i resolved this problem...thanks everyone

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items

    switch (item.getItemId()) {
    case R.id.action_bold:

        System.out.println("bold pressed");
        action_bold.setVisible(false);
        action_unbold.setVisible(true);
        content.append("<b>");

        return true;
    case R.id.action_unbold:

        System.out.println("unbolded pressed");
        action_bold.setVisible(true);
        action_unbold.setVisible(false);
        content.append("</b>");
        return true;

    case R.id.action_italics:

        System.out.println("italics pressed");
        action_italics.setVisible(false);
        action_unitalics.setVisible(true);
        content.append("<i>");

        return true;
    case R.id.action_unitalics:

        System.out.println("unitalics pressed");
        action_italics.setVisible(true);
        action_unitalics.setVisible(false);
        content.append("</i>");
        return true;
    case R.id.action_underline:

        System.out.println("underline pressed");
        action_underline.setVisible(false);
        action_ununderline.setVisible(true);
        content.append("<u>");

        return true;
    case R.id.action_ununderline:

        System.out.println("ununderline pressed");
        action_underline.setVisible(true);
        action_ununderline.setVisible(false);
        content.append("</u>");
        return true;

    default:
        return super.onOptionsItemSelected(item);
    }

Split the text and do the Styles to the text and concatenate the split-ted String.

If I understood you question correctly, you might as well want to try this?

CodePen.io

Credits to the author from CodePen.

Try Html.fromHtml(), and mark up your text with bold and italic HTML tags

e.g.,

 Html.fromHtml("This mixes <b>bold</b> and <i>italic</i> stuff);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top