TextView Justification in Android

Recommended by 13 users

Feb 27,2015
TextView Justification in Android
Add to Bookmark Email this Post 15.3K    4

TextView in Android is a complete text editor that helps us to display text to the user. TextView also optionally allows the user to edit the text, though the basic class is configured to not allow editing.

Justifying the text means aligning the text from both sides i.e. Left Hand Side and Right Hand Side.

Steps to TextView Justification In Android: 

Step 1

Create an XML file and drag the TextView and save some text in it.

</p>
<p>&lt;LinearLayout xmlns:android=&lt;i&gt;&quot;http://schemas.android.com/apk/res/android&quot;&lt;/i&gt;</p>
<p>xmlns:myapp=&lt;i&gt;&quot;http://schemas.android.com/apk/res/com.example.justifysampleapp&quot;&lt;/i&gt;</p>
<p>android:layout_width=&lt;i&gt;&quot;match_parent&quot;&lt;/i&gt;</p>
<p>android:layout_height=&lt;i&gt;&quot;match_parent&quot;&lt;/i&gt;</p>
<p>android:orientation=&lt;i&gt;&quot;vertical&quot;&lt;/i&gt;&gt;</p>
<p>&lt;LinearLayout</p>
<p>android:layout_width=&lt;i&gt;&quot;wrap_content&quot;&lt;/i&gt;</p>
<p>android:layout_height=&lt;i&gt;&quot;wrap_content&quot;&lt;/i&gt;</p>
<p>android:orientation=&lt;i&gt;&quot;vertical&quot;&lt;/i&gt;</p>
<p>&lt;TextView</p>
<p>android:layout_width=&lt;i&gt;&quot;wrap_content&quot;&lt;/i&gt;</p>
<p>android:layout_height=&lt;i&gt;&quot;wrap_content&quot;&lt;/i&gt;</p>
<p>android:gravity=&lt;i&gt;&quot;center&quot;&lt;/i&gt;</p>
<p>android:layout_marginTop=&lt;i&gt;&quot;10dp&quot;&lt;/i&gt;</p>
<p>android:background=&lt;i&gt;&quot;#ffb100&quot;&lt;/i&gt;</p>
<p>android:text=&lt;i&gt;&quot;The Google Directions API is a service that calculates directions between locations using an HTTP request. You can search for directions for several modes of transportation, include transit, driving, walking or cycling. Directions may specify origins, destinations and waypoints either as text strings or as latitude/longitude coordinates. The Directions API can return multi-part directions using a series of waypoints.&quot;&lt;/i&gt;/&gt;</p>
<p>&lt;/LinearLayout&gt;<br />

 Step 2

Define the XML in the Main Activity to get the main screen displayed.

<br />
&lt;b&gt;package&lt;/b&gt; com.example.textviewjustification;<br />
&lt;b&gt;import&lt;/b&gt; android.os.Bundle;<br />
&lt;b&gt;import&lt;/b&gt; android.app.Activity;<br />
&lt;b&gt;import&lt;/b&gt; android.view.Menu;<br />
&lt;b&gt;public class&lt;/b&gt; MainActivity &lt;b&gt;extends&lt;/b&gt; Activity<br />
{<br />
&lt;strong&gt;@Override&lt;/strong&gt;<br />
&lt;b&gt;protected void&lt;/b&gt; onCreate(Bundle savedInstanceState)<br />
{<br />
&lt;b&gt;super&lt;/b&gt;.onCreate(savedInstanceState);<br />
setContentView(R.layout.&lt;i&gt;main&lt;/i&gt;);<br />
}<br />
}<br />

Step 3

Create an XML file in the ‘Value’ folder with the name justified_text_view_attr.xml for the justification attributes. It will contain the below points:

  • Text
  • TextColor
  • BackgroundColor
  • TextSize

Text view justification in Android

Step 4

Define the XML Code for justification in Main.Xml.

<br />
&lt;RelativeLayout</p>
<p>android:layout_width=&lt;em&gt;&quot;match_parent&quot;&lt;/em&gt;</p>
<p>android:layout_height=&lt;i&gt;&quot;wrap_content&quot;&lt;/i&gt;&gt;</p>
<p>&lt;com.example.textviewjustification.Justification</p>
<p>android:id=&lt;i&gt;&quot;@+id/justifiedTextView1&quot;&lt;/i&gt;</p>
<p>android:layout_width=&lt;i&gt;&quot;wrap_content&quot;&lt;/i&gt;</p>
<p>android:layout_height=&lt;i&gt;&quot;wrap_content&quot;&lt;/i&gt;</p>
<p>android:layout_alignParentLeft=&lt;i&gt;&quot;true&quot;&lt;/i&gt;</p>
<p>android:layout_below=&lt;i&gt;&quot;@+id/justifiedTextView1&quot;&lt;/i&gt;</p>
<p>app:backgroundColor=&lt;i&gt;&quot;#ADD8E6&quot;&lt;/i&gt;</p>
<p>app:text=&lt;i&gt;&quot;The Google Directions API is a service that calculates directions between locations using an HTTP request. You can search for directions for several modes of transportation, include transit, driving, walking or cycling. Directions may specify origins, destinations and waypoints either as text strings or as latitude/longitude coordinates. The Directions API can return multi-part directions using a series of waypoints.&quot;&lt;/i&gt;/&gt;</p>
<p>&lt;com.example.textviewjustification.Justification</p>
<p>android:id=&lt;i&gt;&quot;@+id/justifiedTextView1&quot;&lt;/i&gt;</p>
<p>android:layout_width=&lt;i&gt;&quot;wrap_content&quot;&lt;/i&gt;</p>
<p>android:layout_height=&lt;i&gt;&quot;wrap_content&quot;&lt;/i&gt;</p>
<p>android:layout_alignParentLeft=&lt;i&gt;&quot;true&quot;&lt;/i&gt;</p>
<p>android:layout_alignParentTop=&lt;i&gt;&quot;true&quot;&lt;/i&gt;</p>
<p>android:layout_marginTop=&lt;i&gt;&quot;16dp&quot;&lt;/i&gt;</p>
<p>app:text=&lt;i&gt;&quot;Justified Text&quot;&lt;/i&gt;</p>
<p>app:textColor=&lt;i&gt;&quot;#1a0edb&quot;&lt;/i&gt;</p>
<p>app:textSize=&lt;i&gt;&quot;15&quot;&lt;/i&gt;/&gt;</p>
<p>&lt;View</p>
<p>android:layout_width=&lt;i&gt;&quot;fill_parent&quot;&lt;/i&gt;</p>
<p>android:layout_height=&lt;i&gt;&quot;2dp&quot;&lt;/i&gt;</p>
<p>android:layout_below=&lt;i&gt;&quot;@+id/justifiedTextView1&quot;&lt;/i&gt;</p>
<p>android:background=&lt;i&gt;&quot;#e00707&quot;&lt;/i&gt;/&gt;</p>
<p>&lt;/RelativeLayout&gt;</p>
<p>

Step 5

Create another Class named – justification.

This class is used to implement the code for justification with the help of HTML to fetch data and set it in TextView with the justified format.

<br />
&lt;b&gt;private&lt;/b&gt; String htmlcode = &quot;&lt;html&gt;&lt;body style='textalign:justify;color:rgba(%s);font-size:%dpx;margin: 10px 10px 10px 10px;'&gt;%s&lt;/body&gt;&lt;/html&gt;&quot;;</p>
<p>&lt;b&gt;private&lt;/b&gt; String justifytextview;</p>
<p>&lt;b&gt;private int &lt;/b&gt;justifycolortextview;</p>
<p>&lt;b&gt;private int &lt;/b&gt;justifycolorbackground;</p>
<p>&lt;b&gt;private int &lt;/b&gt;JustifytextSize;</p>
<p>&lt;b&gt;public&lt;/b&gt; Justification(Context context, AttributeSet attributes)</p>
<p>{</p>
<p>&lt;b&gt;super&lt;/b&gt;(context, attributes);</p>
<p>&lt;strong&gt;init&lt;/strong&gt;(attributes);</p>
<p>}</p>
<p>&lt;b&gt;public&lt;/b&gt; Justification(Context context, AttributeSet attributes, &lt;b&gt;int&lt;/b&gt; x) {</p>
<p>&lt;b&gt;super&lt;/b&gt;(context, attributes, x);</p>
<p>&lt;strong&gt;init&lt;/strong&gt;(attributes);</p>
<p>}</p>
<p>&lt;b&gt;private void&lt;/b&gt; init(AttributeSet attributes)</p>
<p>{</p>
<p>TypedArray array = getContext().obtainStyledAttributes(attributes, R.styleable.&lt;i&gt;JustifiedTextView&lt;/i&gt;);</p>
<p>justifytextview = array.getString(R.styleable.&lt;i&gt;JustifiedTextView_text&lt;/i&gt;);</p>
<p>&lt;b&gt;if&lt;/b&gt; (justifytextview == &lt;b&gt;null&lt;/b&gt;)</p>
<p>justifytextview = &quot;&quot;;</p>
<p>justifycolortextview = array.getColor(R.styleable.&lt;i&gt;JustifiedTextView_textColor&lt;/i&gt;,Color.&lt;i&gt;BLACK&lt;/i&gt;);</p>
<p>justifycolorbackground = array.getColor(R.styleable.&lt;i&gt;JustifiedTextView_backgroundColor&lt;/i&gt;,Color.&lt;i&gt;TRANSPARENT&lt;/i&gt;);</p>
<p>JustifytextSize = array.getInt(R.styleable.&lt;i&gt;JustifiedTextView_textSize&lt;/i&gt;, 14);</p>
<p>array.recycle();</p>
<p>&lt;b&gt;this&lt;/b&gt;.setWebChromeClient(&lt;b&gt;new&lt;/b&gt; WebChromeClient()</p>
<p>{</p>
<p>});</p>
<p>reloadData();</p>
<p>}</p>
<p>&lt;b&gt;public void&lt;/b&gt; setText(&lt;strong&gt;String&lt;/strong&gt; s)</p>
<p>{</p>
<p>&lt;b&gt;if&lt;/b&gt; (s == &lt;b&gt;null&lt;/b&gt;)</p>
<p>&lt;b&gt;this&lt;/b&gt;.justifytextview = &quot;&quot;;</p>
<p>&lt;b&gt;else&lt;/b&gt;</p>
<p>&lt;b&gt;this&lt;/b&gt;.justifytextview = s;</p>
<p>reloadData();</p>
<p>}</p>
<p>&lt;strong&gt;@SuppressLint&lt;/strong&gt;(&quot;NewApi&quot;)</p>
<p>&lt;b&gt;private void&lt;/b&gt; reloadData()</p>
<p>{</p>
<p>&lt;b&gt;if&lt;/b&gt; (justifytextview != &lt;b&gt;null&lt;/b&gt;)</p>
<p>{</p>
<p>&lt;strong&gt;String&lt;/strong&gt; data = String.&lt;i&gt;format&lt;/i&gt;(htmlcode, toRgba(justifycolortextview), JustifytextSize, justifytextview);</p>
<p>Log.&lt;i&gt;d&lt;/i&gt;(&quot;test&quot;, data);</p>
<p>&lt;b&gt;this&lt;/b&gt;.loadDataWithBaseURL(&lt;b&gt;null&lt;/b&gt;, data, &quot;text/html&quot;, &quot;utf-8&quot;, &lt;b&gt;null&lt;/b&gt;);</p>
<p>}</p>
<p>// set WebView's background color *after* data was loaded.</p>
<p>&lt;b&gt;super&lt;/b&gt;.setBackgroundColor(justifycolorbackground);</p>
<p>// Hardware rendering breaks background color to work as expected.</p>
<p>&lt;b&gt;if&lt;/b&gt; (android.os.Build.VERSION.&lt;i&gt;SDK_INT&lt;/i&gt;&gt;= 11)</p>
<p>&lt;b&gt;this&lt;/b&gt;.setLayerType(WebView.&lt;i&gt;LAYER_TYPE_SOFTWARE&lt;/i&gt;, &lt;b&gt;null&lt;/b&gt;);</p>
<p>}</p>
<p>&lt;b&gt;public void&lt;/b&gt; setTextColor(&lt;b&gt;int&lt;/b&gt; valuehex)</p>
<p>{</p>
<p>justifycolortextview = valuehex;</p>
<p>reloadData();</p>
<p>}</p>
<p>&lt;b&gt;public void&lt;/b&gt; setBackgroundColor(&lt;b&gt;int&lt;/b&gt; valuehex)</p>
<p>{</p>
<p>justifycolorbackground = valuehex;</p>
<p>reloadData();</p>
<p>}</p>
<p>&lt;b&gt;public void&lt;/b&gt; setTextSize(&lt;b&gt;int&lt;/b&gt; textSize)</p>
<p>{</p>
<p>&lt;b&gt;this&lt;/b&gt;.JustifytextSize = textSize;</p>
<p>reloadData();</p>
<p>}</p>
<p>&lt;b&gt;private&lt;/b&gt;&lt;strong&gt; String&lt;/strong&gt; toRgba(&lt;b&gt;int&lt;/b&gt; values)</p>
<p>{</p>
<p>&lt;strong&gt;String&lt;/strong&gt; get = Integer.&lt;i&gt;toHexString&lt;/i&gt;(values);</p>
<p>&lt;b&gt;int&lt;/b&gt; M = Integer.&lt;i&gt;parseInt&lt;/i&gt;(get.substring(0, 2), 16);</p>
<p>&lt;b&gt;int&lt;/b&gt; N = Integer.&lt;i&gt;parseInt&lt;/i&gt;(get.substring(2, 4), 16);</p>
<p>&lt;b&gt;int&lt;/b&gt; O = Integer.&lt;i&gt;parseInt&lt;/i&gt;(get.substring(4, 6), 16);</p>
<p>&lt;b&gt;int&lt;/b&gt; P = Integer.&lt;i&gt;parseInt&lt;/i&gt;(get.substring(6, 8), 16);</p>
<p>&lt;b&gt;return&lt;/b&gt; String.&lt;i&gt; format&lt;/i&gt;(&quot;%d,%d,%d,%d&quot;, M, N, O, P);</p>
<p>}</p>
<p>}</p>
<p>

Got a question for us? Mention them in the comments section and we will get back to you. 

Related Posts:

Understanding Android Emulators

Notification in Android

Android SDK Installation

Start your Android Development Course

Share on

Browse Categories

Comments
4 Comments