How to use TextViewEx, (Textjustify) clases in android

580 views Asked by At

I am trying to use TextViewEx, (Textjustify) from here [link] (https://github.com/bluejamesbond/TextJustify-Android ) to get text justification effect in my project but if I copy and paste the files directly to my project then the files are giving error like some thing (other files) are missing. Also I have searched for how to use TextViewEx but I got is this result below [link] (TextViewEx, (Textjustify)) In this some one told to import the files to the root folder. What does it mean (the root folder). Also if any one has sample code that uses TextViewEx or any other easy way to justify text in Android or sample code that shows justification of text then plz help me Thanks.

1

There are 1 answers

1
Furqan Ali On BEST ANSWER

Well I have struggled a lot but could not found any help to solve this problem but I have found another alternative to justify text. Use this class if one having problem with justification of text.

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebView;

public class JustifiedTextView extends WebView {
    private String core = "<html><body style='text-align:justify;color:rgba(%s);font-size:%dpx;margin: 0px 0px 0px 0px;'>%s</body></html>";
    private String text;
    private int textColor;
    private int backgroundColor;
    private int textSize;

    public JustifiedTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public JustifiedTextView(Context context, AttributeSet attrs, int i) {
        super(context, attrs, i);
        init(attrs);
    }

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    public JustifiedTextView(Context context, AttributeSet attrs, int i,
            boolean b) {
        super(context, attrs, i, b);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        TypedArray a = getContext().obtainStyledAttributes(attrs,
                R.styleable.JustifiedTextView);

        text = a.getString(R.styleable.JustifiedTextView_text);
        if (text == null)
            text = "";
        textColor = a.getColor(R.styleable.JustifiedTextView_textColor,
                Color.BLACK);
        backgroundColor = a.getColor(
                R.styleable.JustifiedTextView_backgroundColor,
                Color.TRANSPARENT);
        textSize = a.getInt(R.styleable.JustifiedTextView_textSize, 12);

        a.recycle();

        this.setWebChromeClient(new WebChromeClient() {
        });
        reloadData();
    }

    public void setText(String s) {
        if (s == null)
            this.text = "";
        else
            this.text = s;
        reloadData();
    }

    @SuppressLint("NewApi")
    private void reloadData() {

        if (text != null) {
            String data = String
                    .format(core, toRgba(textColor), textSize, text);
            Log.d("test", data);
            this.loadDataWithBaseURL(null, data, "text/html", "utf-8", null);
        }

        // set WebView's background color *after* data was loaded.
        super.setBackgroundColor(backgroundColor);

        // Hardware rendering breaks background color to work as expected.
        // Need to use software renderer in that case.
        if (android.os.Build.VERSION.SDK_INT >= 11)
            this.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
    }

    public void setTextColor(int hex) {
        textColor = hex;
        reloadData();
    }

    public void setBackgroundColor(int hex) {
        backgroundColor = hex;
        reloadData();
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
        reloadData();
    }

    @SuppressLint("DefaultLocale")
    private String toRgba(int hex) {
        String h = Integer.toHexString(hex);
        int a = Integer.parseInt(h.substring(0, 2), 16);
        int r = Integer.parseInt(h.substring(2, 4), 16);
        int g = Integer.parseInt(h.substring(4, 6), 16);
        int b = Integer.parseInt(h.substring(6, 8), 16);
        return String.format("%d,%d,%d,%d", r, g, b, a);
    }
}

this is the attrib.xml class

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="JustifiedTextView">
        <attr name="text" format="string" localization="suggested" />
        <attr name="textColor" format="color|reference" />
        <attr name="backgroundColor" format="color|reference" />
        <attr name="textSize" format="integer" min="1" />
    </declare-styleable>

</resources>

In the layout class use this as

<com.example.animationtest.JustifiedTextView
    android:id="@+id/tjTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    app:text="Your Text Here" >
</com.example.animationtest.JustifiedTextView>

com.example.animationtest is the package name in your project where you put the file JustifiedTextView.class

In the code behind class access to the control as

JustifiedTextView tjTextView;
tvTextView2.setTextSize(convertToDp(24));
tjTextView.setTextColor(Color.RED);
tjTextView.setTextSize((int) convertFromDp(18));

where convertFromDp is used to get text size according to screen.

public float convertFromDp(int input) {
    final float scale = getResources().getDisplayMetrics().density;
    return ((input - 0.5f) / scale);
}