In my app first I have textview to show text. Then I want to justify text but in android its not possible to justify text in textview. To justify text I am taking help from
Copy the following class to your project and extend your textview from this:
JustifiedTextView.java :
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
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: 10px 10px 10px 10px;'>%s</body></html>";
private String textColor = "0,0,0,255";
private String text = "";
private int textSize = 12;
private int backgroundColor = Color.TRANSPARENT;
public JustifiedTextView(Context context) {
super(context);
this.setWebChromeClient(new WebChromeClient() {
});
}
public void setText(String s) {
this.text = s;
// this.setPadding(10, 10, 10, 10);
reloadData();
}
@SuppressLint("NewApi")
private void reloadData() {
// loadData(...) has a bug showing utf-8 correctly. That's why we need
// to set it first.
this.getSettings().setDefaultTextEncodingName("utf-8");
this.loadData(String.format(core, textColor, textSize, text),
"text/html", "utf-8");
// 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) {
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);
textColor = String.format("%d,%d,%d,%d", r, g, b, a);
reloadData();
}
public void setBackgroundColor(int hex) {
backgroundColor = hex;
reloadData();
}
public void setTextSize(int textSize) {
this.textSize = textSize;
reloadData();
}
}
You can use it like this:
JustifiedTextView myMsg = new JustifiedTextView(this);
myMsg.setText(msg);
If your text view is in xml, edit it like this:
<com.whatever.JustifiedTextView <!--path to the JustifiedTextView.java -->
android:id="@+id=t1">
</JustifiedTextView>
The do this in your code:
JustifiedTextView myMsg = (JustifiedTextView)findViewById(R.id.t1);
myMsg.setText("text");