-2

I have string in resources I want to do justification on that so that it could look good and easy to read. But Sadly Android does not support the justification straight away. To do some justification I am doing some researches and found the way to use the Html.fromHtml to justify the text.

But sadly I do not know How to use it. To me justification is not working I am setting it as follows

myTextView.setText(Html.fromHtml("<h2 align=right>Android</h2><br><p \"align=justify\">Sdasndakdnsakdnsakdnsandkasndasda adabdkjasbdkasj sadaskjdnksajdn asdnsadknas" +
                    "asjkdbaskdja asodnsakjdnas asdjasndsad alsdnasldnasl ansd dj jasdoa nqowdjnl hjd l  dpajpa ajop p adpjasiojdiosjaodj a sjdas nd op apjpdja apjdoajpj aldoapjdap pajdpajdpajpd " +
                    "asdasidnsalkdnsalkdaskldsalkdnlksadm" +
                    "asdksandlksadnlsakmdlskadksald" +
                    "asdnkasndlksandlksamdlsakdna" +
                    "asdlkasndlkasndksnakldnsa" +
                    "asdansldknsalkdnsalkdn</p>"));

i just put the demo text. No matter what Ever I put into the tag it alwasy comes with the left alignment.

Right now you can see I have used align=justify

So please kindly guide me How can justify my text.

Note : I do not want to center align my text . I just want to use the Justification

Allay Khalil
  • 674
  • 3
  • 11
  • 31

2 Answers2

0

Use the below code:

myTextView.setText(Html.fromHtml("<![CDATA[<h2 align=right>Android</h2><br><p align=justify>Sdasndakdnsakdnsakdnsandkasndasda adabdkjasbdkasj sadaskjdnksajdn asdnsadknas" +
                "asjkdbaskdja asodnsakjdnas asdjasndsad alsdnasldnasl ansd dj jasdoa nqowdjnl hjd l  dpajpa ajop p adpjasiojdiosjaodj a sjdas nd op apjpdja apjdoajpj aldoapjdap pajdpajdpajpd " + 
                "asdasidnsalkdnsalkdaskldsalkdnlksadm" + 
                "asdksandlksadnlsakmdlskadksald" + 
                "asdnkasndlksandlksamdlsakdna" + 
                "asdlkasndlkasndksnakldnsa" + 
                "asdansldknsalkdnsalkdn</p>]]>")); 

Basically you need to put your text indide CDATA as above for it to be formatted as an html

Sid
  • 1,224
  • 3
  • 23
  • 48
  • and its giving me error , I think you have made the typo error or braces error – Allay Khalil Mar 02 '16 at 10:30
  • it is showing all data as it is , and even including the tag of H2 aling = right till align = justify – Allay Khalil Mar 02 '16 at 10:33
  • Just try the edited code, see the thing is that CDATA is the way to make your text be read as an HTML. I am not running this code so making syntax errors is expected. I will post below how I had used CDATA, maybe that will help you. – Sid Mar 02 '16 at 10:37
  • This is how I used it: If you are not making a transaction or for help please contact us at <![CDATA[1-800]]> footer.setText(Html.fromHtml(getString(R.string.offline_digest_footer))); – Sid Mar 02 '16 at 10:38
  • you used this in resources ? – Allay Khalil Mar 02 '16 at 10:39
  • yes, I added the string with the html to the string resouces and then read it as html. Also did you try the edited code above? – Sid Mar 02 '16 at 10:39
  • ya I tried and it is showing me the tags in the text as well – Allay Khalil Mar 02 '16 at 10:40
  • try using escape characters for the tags – Sid Mar 02 '16 at 10:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/105117/discussion-between-sid-and-allay-khalil). – Sid Mar 02 '16 at 10:46
  • Not yet, seems like complete justification for textView in android is not possible. The ways to do it are not very straightforward. You can refer to this: http://stackoverflow.com/questions/1292575/android-textview-justify-text . I will keep trying, if I find somthing, will comment here. – Sid Mar 02 '16 at 11:27
-1

You can accomplish this without resorting to html using android:gravity in a TextView.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:text="Your text" />
</LinearLayout>

Available gravity options are shown in the docs here

Tristan Warner-Smith
  • 9,631
  • 6
  • 46
  • 75