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 Jan 15, 2015 · 16 revisions

The following examples should show you to setup your code.

##StringDocumentLayout StringDocumentLayout will render plain text efficiently.

###XML

<com.bluejamesbond.text.DocumentView xmlns:ext="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    ext:documentView_antialias="true"
    ext:documentView_cacheConfig="auto_quality"
    ext:documentView_hyphen="-"
    ext:documentView_lineHeightMultiplier="2.0"
    ext:documentView_maxLines="100"
    ext:documentView_offsetX="10dp"
    ext:documentView_offsetY="10dp"
    ext:documentView_insetPadding="10dp"
    ext:documentView_insetPaddingBottom="10dp"
    ext:documentView_insetPaddingLeft="10dp"
    ext:documentView_insetPaddingRight="10dp"
    ext:documentView_insetPaddingTop="10dp"
    ext:documentView_progressBar="@id/progressBarXml"
    ext:documentView_reverse="false"
    ext:documentView_text="@string/xml_test_data"
    ext:documentView_textAlignment="justified"
    ext:documentView_textColor="@android:color/white"
    ext:documentView_textFormat="plain"
    ext:documentView_textSize="12sp"
    ext:documentView_textStyle="bold|strikeThru|underline"
    ext:documentView_textSubPixel="true"
    ext:documentView_textTypefacePath="fonts/helvetica.ttf"
    ext:documentView_wordSpacingMultiplier="5.0" />

###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

###Hyphenator Please refer to HyphenatorTest.java.

##SpannedDocumentLayout

###XML

<com.bluejamesbond.text.DocumentView xmlns:ext="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    ext:documentView_antialias="true"
    ext:documentView_cacheConfig="auto_quality"
    ext:documentView_hyphen="-"
    ext:documentView_lineHeightMultiplier="2.0"
    ext:documentView_maxLines="100"
    ext:documentView_offsetX="10dp"
    ext:documentView_offsetY="10dp"
    ext:documentView_insetPadding="10dp"
    ext:documentView_insetPaddingBottom="10dp"
    ext:documentView_insetPaddingLeft="10dp"
    ext:documentView_insetPaddingRight="10dp"
    ext:documentView_insetPaddingTop="10dp"
    ext:documentView_progressBar="@id/progressBarXml"
    ext:documentView_reverse="false"
    ext:documentView_text="@string/xml_test_data"
    ext:documentView_textAlignment="justified"
    ext:documentView_textColor="@android:color/white"
    ext:documentView_textFormat="formatted"
    ext:documentView_textSize="12sp"
    ext:documentView_textStyle="bold|strikeThru|underline"
    ext:documentView_textSubPixel="true"
    ext:documentView_textTypefacePath="fonts/helvetica.ttf"
    ext:documentView_wordSpacingMultiplier="5.0" />

###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