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);