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
, 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.