Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Justify text in textview

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 this link. I am following the answer provided by @Kondzio but its not working. I dont know whats wrong in my code.

Code-

public class Benefits extends Activity{
    private Button back;
    LinearLayout bText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.benefits);
        WebView view = new WebView(this);
        view.setVerticalScrollBarEnabled(true);
        ((LinearLayout)findViewById(R.id.bText)).addView(view);
        view.loadData(getString(R.string.benef), "text/html", "utf-8");

        back.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v)
        {
            finish();
        }
    });
  }
}

Xml-

<LinearLayout 
       android:id="@+id/bText"
       android:orientation="horizontal"
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"
       android:layout_weight="4"
       android:gravity="center">            
 </LinearLayout>

In strings.xml-

<string name="benef">
        <![CDATA[
        <html>
        <head></head>
        <body style="text-align:justify;color:gray;background-color:black;">
         "1.some text\n
          2.some text\n
          .............
 </body>
</html>
]]>
</string> 

I am setting scrollbarenabled to true to make vertical scrolling on my text.

like image 668
John R Avatar asked Jan 17 '14 05:01

John R


People also ask

How to align text in TextView android?

To right align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “viewEnd” in layout file, or programmatically set the textAlignment property of the TextView object with View.

How do I align text in text view?

android:gravity="center" for text center in TextView. android:gravity="center_horizontal" inner text if you want horizontally centered. android:gravity="center_vertical" inner text if you want vertically centered. android:layout_centerInParent="true" if you want TextView in center position of parent view.

What is text align justify?

Text alignment is a paragraph formatting attribute that determines the appearance of the text in a whole paragraph. For example, in a paragraph that is left-aligned (the most common alignment), text is aligned with the left margin. In a paragraph that is justified, text is aligned with both margins.


1 Answers

// converting all the language content to justify using html tags.

String youtContentStr = String.valueOf(Html
                .fromHtml("<![CDATA[<body style=\"text-align:justify;color:#222222; \">"
                            + getResources().getString(R.string.passthe_content)
                            + "</body>]]>"));

    view.loadData(youtContentStr, "text/html", "utf-8");

EDIT: or change your code with added back slash

<body style=\"text-align:justify;color:gray;background-color:black;\">
like image 76
Padma Kumar Avatar answered Oct 03 '22 06:10

Padma Kumar