1

Create an activity which comprises of

1) an edittext (Commonsware RichEditText)

2) a toolbar with the following buttons

Bold, Italic, underline, strikethrough, font, Center Align, Left Align, Right Align, Justified,...etc

4) A Save button

Usage:

User enters text and edits it by selecting text as a whole or parts. Effects are applied to selected text.When the user is finished editing the text into the editor, he clicks Save button. Everything is read from the RichEditText and sent to a database for later usage with the tags and formatting in html. I use the following code for saving and retriving.

Spanned s = Html.fromHtml("<i>Hi</i> There ! <b>how're you ?</b>");
et.setText(s);

//--save to string--
Editable e = et.getText();
String s2 = Html.toHtml(e);

//--restore from string--
Spanned s3 = Html.fromHtml(s2);
et.setText(s3);

src: Copy to clipboard using commonsware cwac-richedit library

The Problem:

I have to apply Bold, Italic, underline, strikethrough, font, Center Align, Left Align, Right Align, Justified,...etc to selected text onClick() of a button in a tool bar different than an action bar button. Is there any way I can place the entire view shown in the actionbar when selection is made in a layout above the RichEditText? Say a Horizontal LinearLayout or some adaptive layouts?

It would be really helpfull if some one can tell he how to add any one effect to a selected text on click of a button. I saw the following method but not sure what is T value.

myRichEditText.applyEffect(effect, value);

Thanks.

1 Answers1

1

Is there any way I can place the entire view shown in the actionbar when selection is made in a layout above the RichEditText?

No. Your toolbar would need to be in the main UI, such as above the RichEditText widget itself.

It would be really helpfull if some one can tell he how to add any one effect to a selected text on click of a button.

Quoting the documentation:

applyEffect() changes the current selection, applying or removing an effect (e.g., making the selection bold). The first parameter is the effect to apply (e.g., RichEditText.BOLD). The second parameter is the new value for the effect. Many effects take boolean values, so applyEffect(RichEditText.BOLD, true) would format the current selection as bold.

FWIW, I plan to offer a standard toolbar option for RichEditText, but I am unlikely to have a chance to do that before this autumn.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Is there any situation that we need to pass a 'false' to add a particular effect? – Anoop Chandrika HarisudhanNair Jul 07 '14 at 09:43
  • is there any way to add those buttons above the RichEditText apart from the actionbar or actionbarsherlock.? I mean like a tool bar. I am thinking about a floating toolbar as well above the text that's the reason I am asking. (Sorry for multiple comments, only 5 mins to edit) – Anoop Chandrika HarisudhanNair Jul 07 '14 at 09:48
  • @AndroidKid: "Is there any situation that we need to pass a 'false' to add a particular effect?" -- when you want to get rid of it. To apply bold to the current selection, use `applyEffect(RichEditText.BOLD, true)`. To remove bold from the current selection, use `applyEffect(RichEditText.BOLD, false)`. "is there any way to add those buttons above the RichEditText" -- use a vertical `LinearLayout` to hold your toolbar and your `RichEditText`. "I am thinking about a floating toolbar" -- use a `RelativeLayout` with the toolbar as a later child than is the `RichEditText`. – CommonsWare Jul 07 '14 at 10:31
  • are there any ways to get the tool bar from the control itself? I already have a few buttons in the action bar. The edit controls seems bit congested. You should also consider adding a special method like `richEditText.getRichText()` for getting the entered text with all formatting. – Anoop Chandrika HarisudhanNair Jul 07 '14 at 10:42
  • "are there any ways to get the tool bar from the control itself?" -- the "control" does not have a toolbar at present. Even when I do add a toolbar, it will be a separate custom view. What `RichEditText` uses today is an action mode (a.k.a., contextual action bar), not a toolbar `View`. "You should also consider adding a special method like richEditText.getRichText() for getting the entered text with all formatting" -- it is already there. It is called `getText()`, and it is inherited from `EditText`. – CommonsWare Jul 07 '14 at 10:56