Skip to content
This repository has been archived by the owner on Apr 27, 2019. It is now read-only.

3 · Examples

Mathew Kurian edited this page Nov 26, 2014 · 16 revisions

The following examples should show you to setup your code.

##DocumentLayout DocumentLayout will render plain text efficiently.

###XML

<com.text.DocumentView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

###Java The following example will create a DocumentView and enable justification.

// Create DocumentView and set plain text
// Important: Use DocumentLayout.class
DocumentView documentView = new DocumentView(this, DocumentView.PLAIN_TEXT);  // Support plain text
documentView.getDocumentLayoutParams().setTextAlignment(TextAlignment.JUSTIFIED);
documentView.setText("Insert your text here", true); // Set to `true` to enable justification

##SpannedDocumentLayout SpannedDocumentLayout will help render Spannable text.

###XML Currently, no way to create a DocumentView with a SpannedDocumentLayout using XML.

###Java The following example will create a DocumentView and set some features such as padding and typeface

/*
 * To mark a region for justification in Spanned strings, you must setSpan(new JustifySpan(), start, end)
 * that particular region.
 */

// Create span
Spannable span = new SpannableString("In New York and New Jersey, governors Andrew Cuomo and Chris Christie have implemented controversial quarantines.");

// Set region to justify
span.setSpan(new JustifySpan(), 0, span.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

// Create DocumentView and set span.
// Important: Use SpannedDocumentLayout.class
DocumentView documentView = new DocumentView(this, DocumentView.FORMATTED_TEXT);  // Support spanned text

// Set the fallback alignment if an alignment is not specified for a line
documentView.getDocumentLayoutParams().setTextAlignment(TextAlignment.JUSTIFIED);

documentView.setText(span, true); // Set to `true` to enable justification

##ArticleBuilder ArticleBuilder is a class that builds spanned documents and enables multiple Spans to be set at once to the last inserted text. In order to use ArticleBuilder, you must import com.text.demo.helper#ArticleBuilder.

// NOTE: ArticleBuilder is NOT required for DocumentView to work.
//       It is just a helper class I made to make it easier to add
//       multiple spans to a certain text.
ArticleBuilder a = new ArticleBuilder();
a.append("WHO: Ebola Cases",
          false, new RelativeSizeSpan(2f), new StyleSpan(Typeface.BOLD))
 .append("<font color=0xFFC801>Sam Frizell</font><font color=0x888888> @Sam_Frizell  Oct. 25, 2014</font>",
          false, new RelativeSizeSpan(0.8f), new StyleSpan(Typeface.BOLD))
 .append("In New York and New Jersey, governors Andrew Cuomo and Chris Christie have implemented controversial quarantines on all healthcare workers returning from West Africa after a doctor returning from Guinea contracted the disease and was diagnosed in New York.",
          true, new RelativeSizeSpan(1f), new JustifySpan());

DocumentView documentView = new DocumentView(this, SpannedDocumentLayout.class);  // Support spanned text
documentView.setText(a, true); // Set to `true` to enable justification
Clone this wiki locally