Skip to content

Instantly share code, notes, and snippets.

@pwittchen
Created August 31, 2013 21:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pwittchen/6400877 to your computer and use it in GitHub Desktop.
Save pwittchen/6400877 to your computer and use it in GitHub Desktop.
package com.github.sample.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.webkit.WebView;
import com.github.sample.R;
public class JustifiedTextView extends WebView {
private String text;
public JustifiedTextView(final Context context) {
this(context, null, 0);
}
public JustifiedTextView(final Context context, final AttributeSet attrs) {
this(context, attrs, 0);
}
public JustifiedTextView(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
if (attrs != null) {
final TypedValue typedValue = new TypedValue();
final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.JustifiedTextView, defStyle, 0);
if (typedArray != null) {
typedArray.getValue(R.styleable.JustifiedTextView_text, typedValue);
if (typedValue.resourceId > 0 && TextUtils.isEmpty(text)) {
text = context.getString(typedValue.resourceId);
text.replace("\n", "<br />");
loadDataWithBaseURL("file:///android_asset/", "<html><head>" +
"<link rel=\"stylesheet\" type=\"text/css\" href=\"justified_textview.css\" />" +
"</head><body>" + text + "</body></html>", "text/html", "UTF8", null);
}
setTransparentBackground();
}
}
}
public void setTransparentBackground() {
if (Build.VERSION.SDK_INT >= 11) {
try {
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
} catch (final NoSuchMethodError e) {
}
setBackgroundColor(Color.TRANSPARENT);
setBackgroundDrawable(null);
setBackgroundResource(0);
}
}
public String getText() {
return text;
}
public void setText(String currentText) {
this.text = currentText;
text.replace("\n", "<br />");
loadDataWithBaseURL("file:///android_asset/", "<html><head>" +
"<link rel=\"stylesheet\" type=\"text/css\" href=\"justified_textview.css\" />" +
"</head><body>" + text + "</body></html>", "text/html", "UTF8", null);
setTransparentBackground();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment