Adding support for rich text in Eventyay Organizer App
The Open Event Organizer App provides the users with its one of the core features of the ability to create or update an event. To add this feature, we will use HTML and android’s WebView in order to aid us integrate support for rich text in the app.
The first step is adding the Wasabeef RichText library to your project. Open your build.gradle file and add the support library to the dependency section.
Adding the dependency in build.gradle(app-level) in the project:
dependencies { //Other dependencies //Rich text editor implementation “jp.wasabeef:richeditor-android:1.2.2” } |
What we need is an activity accessible throughout the project for any element that needs to input rich text, which means that this activity goes in the utils package.
Let’s start with building this activity:
We need to first set a hint text or a placeholder text for the editor in case there’s no saved text for the concerned field.
public class RichEditorActivity extends AppCompatActivity { … @Override binding.editor.setPlaceholder(getString(R.string.enter_text)); |
Now let’s see how we add the formatted text to our WebView. Currently we are supporting the options:
- Undo
- Redo
- Bold
- Italic
- StrikeThrough
- Bulleted list
- Numbered list
as follows:
binding.actionUndo.setOnClickListener(v –> binding.editor.undo()); binding.actionRedo.setOnClickListener(v –> binding.editor.redo()); binding.actionBold.setOnClickListener(v –> binding.editor.setBold()); binding.actionItalic.setOnClickListener(v –> binding.editor.setItalic()); binding.actionStrikethrough.setOnClickListener(v –> binding.editor.setStrikeThrough()); binding.actionInsertBullets.setOnClickListener(v –> binding.editor.setBullets()); binding.actionInsertNumbers.setOnClickListener(v –> binding.editor.setNumbers()); |
To add support for adding links, we need to first setup a click listener and show a dialog on click:
binding.actionInsertLink.setOnClickListener(v -> { if (linkDialog == null) { createLinkDialog(); } linkDialog.show(); }); |
The above listener is using a linkDialog, which is initialized in the method below, as follows:
We are first dynamically creating a LinearLayout, and then 2 EditTextViews, and then adding those views to the LinearLayout. Finally we build the AlertDialog as usual and set the Dialog’s view to the LinearLayout we created.
private void createLinkDialog() { LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); final EditText text = new EditText(this); text.setHint(getString(R.string.text)); layout.addView(text); final EditText link = new EditText(this); link.setHint(getString(R.string.insert_url)); layout.addView(link); linkDialog = new AlertDialog.Builder(this) .setPositiveButton(getString(R.string.create), (dialog, which) -> { binding.editor.insertLink(link.getText().toString(), text.getText().toString()); }) .setNegativeButton(getString(R.string.cancel), (dialog, which) -> { dialog.dismiss(); }) .setView(layout) .create(); } |
Here’s how the result looks like:
Resources
- Wasabeef’s Rich Editor README
https://github.com/wasabeef/richeditor-android