7

I am new to Android. I have a paragraph in string which looks like below

String my_paragraph ="Software is a set of instructions, data or programs used to operate computers and execute specific tasks. It is the opposite of hardware, which describes the physical aspects of a computer. Software is a generic term used to refer to applications, scripts and programs that run on a device."
textView.setText(my_paragraph);

I want to display it in the Text View like this below image:

enter image description here

But what I got is I added below

enter image description here

I want the first line in text view(paragraph) should starts in the middle. I don't want to add leading spaces in string. I don't know how to achieve this. Please help me with some solutions.

Zain
  • 37,492
  • 7
  • 60
  • 84
  • why would you want to start in the middle? that might give us a clue for solutions – ariefbayu Mar 25 '21 at 06:32
  • @ariefbayu does there really have to be a reason ? this could just be a design requirement or something OP has to achieve – a_local_nobody Mar 25 '21 at 06:34
  • @ariefbayu, Thanks for the response. I want to show some questions with answers in my app. so for answers text view, I want it to start in middle. –  Mar 25 '21 at 06:35
  • because as it is right now, I don't see a way other than prepending the strings with a bunch of white space... – ariefbayu Mar 25 '21 at 06:35

2 Answers2

5

Android has libraries that can do this for you. I found an old post with a great how to use the SpannableString and LeadingMarginSpan libraries.

The question can be found at: What is Leading Margin in Android?

  • 2
    i think this is the best solution you'll find for this, but i wouldn't consider this question a duplicate of the one you've mentioned here. good find – a_local_nobody Mar 25 '21 at 06:55
  • I agree, this solution looks "more pro", mine is just a shortcut without way of defining size of indetation :) – snachmsm Mar 25 '21 at 07:07
2

try to add \t or \u0009 (tabulator character) at the begining of String - this is a symbol of default indetation for paragraphs

textView.setText("\u0009" + my_paragraph);
snachmsm
  • 17,866
  • 3
  • 32
  • 74