1

I have a button with a long text that I would like to spread over two lines to the width of the button is the same size as the others.

The issue is that when I do this via the XML code, the newline is ignored by both the AVD's and my hardware devices (but displayed correctly in the editor preview). When I do it with Java code, it's respected.

This is ignored:

<Button
    android:id="@+id/button"
    android:layout_columnWeight="1"
    android:layout_gravity="fill"
    android:layout_rowWeight="1"
    android:onClick="playSpeech"
    android:tag="doyouspeakenglish"
    android:text="Do you\nspeak English?" />

This applies the newline correctly:

((Button) findViewById(R.id.button)).setText("Do you\nspeak English?");

What could be the reason for the XML way to be ignored? I also tried &#10;, as suggested here: how can I display multiple lines of text on a button. Again, no success.

Full layout code

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <GridLayout
        android:layout_width="368dp"
        android:layout_height="495dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:columnCount="2"
        android:rowCount="4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/button"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            android:layout_rowWeight="1"
            android:onClick="playSpeech"
            android:tag="doyouspeakenglish"
            android:text="Do you\nspeak English?" />

        <Button
            android:id="@+id/button2"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            android:layout_rowWeight="1"
            android:onClick="playSpeech"
            android:tag="goodevening"
            android:text="Good Evening" />

        <Button
            android:id="@+id/button3"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            android:layout_rowWeight="1"
            android:onClick="playSpeech"
            android:tag="hello"
            android:text="Hello" />

        <Button
            android:id="@+id/button4"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            android:layout_rowWeight="1"
            android:onClick="playSpeech"
            android:tag="howareyou"
            android:text="How are you?" />

        <Button
            android:id="@+id/button5"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            android:layout_rowWeight="1"
            android:onClick="playSpeech"
            android:tag="ilivein"
            android:text="I live in..." />

        <Button
            android:id="@+id/button6"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            android:layout_rowWeight="1"
            android:onClick="playSpeech"
            android:tag="mynameis"
            android:text="My name is..." />

        <Button
            android:id="@+id/button7"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            android:layout_rowWeight="1"
            android:onClick="playSpeech"
            android:tag="please"
            android:text="Please" />

        <Button
            android:id="@+id/button8"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            android:layout_rowWeight="1"
            android:onClick="playSpeech"
            android:tag="welcome"
            android:text="Welcome" />
    </GridLayout>
</android.support.constraint.ConstraintLayout>

Other details: Using Android Studio 3.1.4 and SDK v26

Note that this question is not the same as How do I add a newline to a TextView in Android?, because the suggested solutions for the textview there do not work for my button case.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
aardbol
  • 2,147
  • 3
  • 31
  • 42
  • Try this **`android:text="Do you \r\n speak English?"`** have u check this answer https://stackoverflow.com/a/23721628/7666442 See the out put **[screenshot image](https://i.stack.imgur.com/o0IHM.png)** – AskNilesh Aug 23 '18 at 07:51
  • @NileshRathod Yeah, I tried that too, but it doesn't work. Also, note that the screenshot you linked to is a screenshots of the preview in the editor. In my editor it shows up correct too, but in the emulator and on my phones it doesn't. – aardbol Aug 23 '18 at 09:47
  • For me **`android:text="Do you \n speak English?"`** and **`android:text="Do you \r\n speak English?"`** is working fine in real device check screenshot **[image screen shot](https://i.stack.imgur.com/I8P4X.png)** – AskNilesh Aug 23 '18 at 09:54
  • @NileshRathod Can you provide me with your whole XML code, please? So I can compare to see if it's a syntax issue – aardbol Aug 23 '18 at 10:07
  • check my below layout – AskNilesh Aug 23 '18 at 10:38

3 Answers3

1

Try this

      <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical">
          <Button
              android:id="@+id/button"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:onClick="playSpeech"
              android:tag="doyouspeakenglish"
              android:maxLines="2"
              android:text="Do you \n speak English?" />
          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:onClick="playSpeech"
              android:tag="doyouspeakenglish"
              android:maxLines="2"
              android:text="Do you \n speak English?" />

          <Button
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:onClick="playSpeech"
              android:tag="doyouspeakenglish"
              android:maxLines="2"
              android:text="Do you \n speak English?" />

      </LinearLayout>
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • Strange things are going on... I replaced my buttons with yours (I didn't copy the LinearLayout), but my button layout is always showing in the emulator. I cleaned, rebuild and invalidated/restart caches multiple times, but nothing changes. But if I look at your code, it's exactly what I tried before, except for the LinearLayout because that's not something I want now. – aardbol Aug 23 '18 at 11:11
  • @EarthMind also `Strange` you code is working find in my device https://i.stack.imgur.com/qzm8X.png – AskNilesh Aug 23 '18 at 11:14
  • 1
    As I suspected after all this searching, the issue was lying somewhere else. Luckily not a corrupt installation, but rather simply a different mail layout that was being used all the time. I had an extra activity_main.xml (v21) that I created by accident before, this contained older code. That's why the newline was ignored and your XML code was also not applied. I'm glad it's all over now :-) – aardbol Aug 23 '18 at 12:18
1

This is an issue I did not know, because I use always string resources.
Also the problem exists only in design. In the emulator I can see the line break.
Anyway, add this line in strings.xml:

<string name="mytext">Do you\nspeak English?</string>

and set the button's text:

android:text="@string/mytext" 

This way \n works.

  • Unfortunately that doesn't solve it. Still only the Java code works. – aardbol Aug 23 '18 at 11:20
  • @EarthMind it seems that we are facing different issues since in my case the linebreak works in emulator/device but not in preview –  Aug 23 '18 at 11:26
  • As you assumed, the issue was lying elsewhere. Have a look at my last comment to Nilesh if you're interested. – aardbol Aug 23 '18 at 12:19
  • @EarthMind I'm glad your problem is solved. Mine is not but as I mentioned I always use resources (I believe its the best practice) so there is no problem. –  Aug 23 '18 at 12:25
  • That's true that's it's the recommended practice. In my case I use hardcoded strings because it's a very simple app for training and serves no other use – aardbol Aug 23 '18 at 16:25
0

As I suspected after all this searching, the issue was lying somewhere else than in the code. Luckily not a corrupt installation, but rather simply a different main layout that was being used as default in the emulator or my phones all the time.

I had an extra activity_main.xml (v21), that I created by accident before, that contained older code. That's why my updates to the real activity_main.xml were ignored.

aardbol
  • 2,147
  • 3
  • 31
  • 42