JohnRock
JohnRock

Reputation: 6875

In an Android TextView, is it possible to insert paragraphs?

Below is an example of the type of problem that I have. I have data in a pojo that I need to display in a textview... the data has pseudo code that denotes each paragraph with [p]

I would like to somehow parse the [p]'s into paragraphs when they are displayed in the textview. Can this be done? Is there something I can substitute for the [p] that will make a new paragraph in the textview?

Question question = new Question();
question.setText("Here is the first paragraph.[p] And this should be the second."); 

TextView view = (TextView) findViewById(R.id.qtext);
view.setText(question.getParsedText());

Upvotes: 11

Views: 25571

Answers (4)

MGK
MGK

Reputation: 7098

You can also try this

String summary = "<html><font color=\"#FFFFFF\"" +
     
             "<p align="+ "\"" +"left" + "\""+ ">" +  "MY data" +"</p>"+
     
                "</font></html>";
mTextView.setText(Html.fromHtml(summary));

Upvotes: 5

pankaj sharma
pankaj sharma

Reputation: 181

TextView tw = findViewById(R.id.t);
tw.setText("first line\nsecond line\nthird line");

Upvotes: 1

<br> and <p> work if you need other formatting like bold and are in the middle of an Html.fromHtml:

import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;

public class Main extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final TextView tv = new TextView(this);
        tv.setText(Html.fromHtml("first<br><b>second</b>"));
        setContentView(tv);
    }
}

Tested on Android 22.

Upvotes: 0

RoflcoptrException
RoflcoptrException

Reputation: 52229

Hi i would parse the whole String and then replace every [p] with \n or even \n\n as you like.

\n in the String makes a linebreak. For example use it like that:

question.setText("Here is the first paragraph.\n\n And this should be the second.");`

The method which can do that easily is String.replace(...)

Upvotes: 11

Related Questions