Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I display multiple lines of text on a button

My button's layout_width set to match_parent.

In order to display multi lines on the button, I tried:

  • insert '\n' into the text on button

  • set Singleline false set Maxlines to 2 or 3

  • convert html from Html.fromHtml

Nothing worked. '\n' showed up as a small square on the button while showing single line of text.

Does anybody have any idea why this is happening and how I can fix this?

UPDATE: I just found out I was using custom button that has its own text drawing. That's the reason. Sorry for the confusion. I just punished myself by banging my head.

like image 585
Tae-Sung Shin Avatar asked Nov 12 '11 10:11

Tae-Sung Shin


People also ask

How do I display text on multiple lines?

Yes, you can put them all in separate paragraphs, using the <p></p> tag or you can separate them via a <br> tag at every line.

How do I show multiple lines of text in HTML?

To create a multi-line text input, use the HTML <textarea> tag. You can set the size of a text area using the cols and rows attributes. It is used within a form, to allow users to input text over multiple rows.

How do I insert a line break in button text?

The easiest way to have a line break is using the <br> tag on your text. It is used for inserting a single line break.


2 Answers

If you're trying to add a new line in a layout XML file:

Use &#10; (new line)

    android:text="Hi&#10;Hello" 

If you're trying to add a new line in code, just use '\n', same as in any other text.

If you can't see the second line, it may be that your Button doesn't have enough height. IE, in my case, the layout containing the button had a fixed height that just happened to make my button perfectly display one line of text.

like image 62
wannik Avatar answered Oct 13 '22 08:10

wannik


I just tried and it worked:

1) Define in ../res/values/strings.xml:

<string name="multilines">Line1Line1\nLine2Line2</string> 

2) Refer it in the layout file:

<Button     android:id="@+id/btn_multilines"     android:text="@string/multilines"     android:layout_height="wrap_content"     android:layout_width="fill_parent"> </Button> 
like image 20
hovanessyan Avatar answered Oct 13 '22 08:10

hovanessyan