diff --git a/android/src/main/java/com/contentful/rich/android/renderer/views/BlockRenderer.kt b/android/src/main/java/com/contentful/rich/android/renderer/views/BlockRenderer.kt index 7902094..410ecf1 100644 --- a/android/src/main/java/com/contentful/rich/android/renderer/views/BlockRenderer.kt +++ b/android/src/main/java/com/contentful/rich/android/renderer/views/BlockRenderer.kt @@ -46,33 +46,35 @@ open class BlockRenderer( viewGroup.addView(childView) } } - childNode is CDARichHyperLink && childNode.data is String -> { - val childLayout = childView.findViewById(R.id.rich_content) - if (childLayout.childCount > 0) { - val childTextView = childLayout.getChildAt(0) as TextView - val span = UrlSpan(childNode.data as String) - val text = lastTextView?.let { - SpannableStringBuilder(it.text).append(childTextView.text) - } ?: SpannableStringBuilder(childTextView.text) + childNode is CDARichHyperLink -> { + getDataUrl(childNode.data)?.let { data-> + val childLayout = childView.findViewById(R.id.rich_content) + if (childLayout.childCount > 0) { + val childTextView = childLayout.getChildAt(0) as TextView + val span = UrlSpan(data) + val text = lastTextView?.let { + SpannableStringBuilder(it.text).append(childTextView.text) + } ?: SpannableStringBuilder(childTextView.text) - context.config?.linkColor?.let { - span.textColor = it - } + context.config?.linkColor?.let { + span.textColor = it + } - text.setSpan( - span, - lastTextView?.text?.length ?: 0, - text.length, - Spanned.SPAN_EXCLUSIVE_EXCLUSIVE - ) + text.setSpan( + span, + lastTextView?.text?.length ?: 0, + text.length, + Spanned.SPAN_EXCLUSIVE_EXCLUSIVE + ) - lastTextView?.let { - it.movementMethod = LinkMovementMethod.getInstance() - it.text = text - } ?: run { - childTextView.text = text - lastTextView = childTextView - viewGroup.addView(childView) + lastTextView?.let { + it.movementMethod = LinkMovementMethod.getInstance() + it.text = text + } ?: run { + childTextView.text = text + lastTextView = childTextView + viewGroup.addView(childView) + } } } } @@ -91,4 +93,10 @@ open class BlockRenderer( context: AndroidContext, node: CDARichNode ): View = context.inflater.inflate(R.layout.rich_block_layout, null, false) + + private fun getDataUrl(data: Any): String? = when { + data is String -> data + data is Map<*, *> && data["uri"] is String -> data["uri"] as? String + else -> null + } } diff --git a/android_sample/src/main/java/com/contentful/rich/android/sample/Page.kt b/android_sample/src/main/java/com/contentful/rich/android/sample/Page.kt index 551a7dc..24bb4af 100644 --- a/android_sample/src/main/java/com/contentful/rich/android/sample/Page.kt +++ b/android_sample/src/main/java/com/contentful/rich/android/sample/Page.kt @@ -6,6 +6,7 @@ import com.contentful.java.cda.CDAResource import com.contentful.java.cda.LocalizedResource import com.contentful.java.cda.rich.* import com.contentful.java.cda.rich.CDARichMark.* +import com.google.gson.internal.LinkedTreeMap import java.lang.reflect.Field data class Page(val name: String, val document: CDARichDocument) @@ -49,9 +50,21 @@ val PAGES = mutableListOf( CDARichListItem().addAll(text("second item")), CDARichListItem().addAll(text("third item")), CDARichListItem().addAll( - CDARichHyperLink("https://www.google.com/search?hl=en&site=imghp&tbm=isch&source=hp&q=dogs") - .addAll(text("Hyperlink to first item")), - CDARichListItem().addAll(text("Normal Text")) + CDARichParagraph().addAll( + text("First Text"), + CDARichHyperLink( + LinkedTreeMap().apply { + put( + "uri", + "https://www.google.com/search?hl=en&site=imghp&tbm=isch&source=hp&q=cats" + ) + } + ).addAll(text("Hyperlink to first item")), + text("Second Text") + ), + CDARichListItem().addAll( + text("Normal Text") + ) ), CDARichListItem().addAll( CDARichHyperLink("https://www.google.com/search?hl=en&site=imghp&tbm=isch&source=hp&q=cats")