Wednesday, June 18, 2014

Justify Text In Android



Justifying text inside TextView in Android is not as simple as most of us may expect it to be. Android does not support full justification of text in a TextView. It does not have a default option for it. We can set alignment to the right or to the left, but we cannot simply justify text with generic attributes. However, we can justify text in android using the WebView.

Here’s how to proceed forward…

Write the following code in your Java file:

setContentView(R.layout.main);
WebView view =newWebView(this);
view.setVerticalScrollBarEnabled(false);
((LinearLayout)findViewById(R.id.inset_web_view)).addView(view);
view.loadData(getString(R.string.example),"text/html","utf-8");
  


You should write your text in the following manner in the strings.xml file.

<stringname="example">
<html>
<head>
</head>

<body style="text-align:justify;color:white;background-color:black;">

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

</body>
</html>
</string>


Voila! Your text will be now be justified on the android device.

No comments:

Post a Comment