11

I am writing a text in android which take more than two lines now my problem is how I indent the second line my text lines are.

1.my name is qadeer hussain

iam fine how  Ru.

but I want

1.my name is qadeer hussain

     iam fine how  Ru.

here the second line not start right below the one but it right below the name

IARI
  • 1,217
  • 1
  • 18
  • 35
Qadeer Hussain
  • 653
  • 2
  • 7
  • 17

3 Answers3

33

Use a LeadingMarginSpan.Standard with a SpannableString, or if you also need bullets use a BulletSpan:

Just create a function like this:

static SpannableString createIndentedText(String text, int marginFirstLine, int marginNextLines) {
    SpannableString result=new SpannableString(text);
    result.setSpan(new LeadingMarginSpan.Standard(marginFirstLine, marginNextLines),0,text.length(),0);
    return result;
}

And then, everytime you need an indented line you can do:

myTextView.setText(createIndentedText("my text that's gonna be indented", 10, 10));

The first parameter is the indentation of the first line, the second parameter is the indentation of the next lines, just in case you want to indent the first line differently to the following ones.

rupps
  • 9,712
  • 4
  • 55
  • 95
5

Try using a \t before what you want to indent. Use more if you need to indent it further

tf.alves
  • 919
  • 8
  • 15
  • 2
    but how i know that from where my string display on next line because different android devices have different screen sizes – Qadeer Hussain Apr 22 '14 at 10:53
  • well, you can actually predict that, try looking here: http://stackoverflow.com/questions/3654321/measuring-text-height-to-be-drawn-on-canvas-android All you have to do now is to check your screen size and compare to the text size, to calculate how much of it will be in the same line. Now it all comes down to what you want to achieve. – tf.alves Apr 22 '14 at 15:25
2

You can use \t for spacing and \n for new line. you can use as many new lines and \t spacing for your text according to your needs. For your example:

    textView.setSingleLine(false);
    textView.setText("1.my name is qadeer hussain\n\tiam fine how  Ru.");
canova
  • 3,965
  • 2
  • 22
  • 39
  • 3
    but i dont know from where my string break and display on next line because different android devices have different screen size – Qadeer Hussain Apr 22 '14 at 10:57