1

I have a custom style for a switch widget in my app. There is text for both, textOff and textOn. The text that is shown is short in English, but unfortunately long or even contains more than one word in other languages and therefore does not fit into the widget. This causes the widget's drawable be pushed out of the widget's visible area and sometimes the switch button itself does not fit in.

(unfortunately I cannot post images due to lack of reputation)

I want the text to be wrapped if the text does not fit anymore. Is this possible? I tried singleLine="false" as well as the ellipsize attribute and both do not show any effect.

<Switch
  style="@style/MarginedElement"
  android:id="@+id/toggle_button"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:drawableStart="@drawable/toggle_switch"
  android:textOff="@string/textOff"
  android:textOn="@string/textOn"
  android:thumb="@drawable/click_style_switch_button_thumb"
  android:track="@drawable/click_style_switch_button_bg"
  android:background="@drawable/click_style_switch_bg"
  android:switchTextAppearance="@style/switchText"
 />

Any idea how I could go about this? Or alternative solutions besides changing the text itself.

EDIT: Fixing the switch's width, setting singleLine="true" and ellipsize="end" does not have any effect.

SOERGI
  • 193
  • 2
  • 13
  • Just as a note for people who might come across this issue: I think the switch widget is not thought to support long texts, so after fiddling with it for a couple of hours I decided to redesign that part of the app so it does not require lengthy texts in the first place. – SOERGI Jul 03 '15 at 13:39

2 Answers2

0

You've written you tried singleLine="false" but this only sets it to be not one-lined. If you'd like to ellipsize, you should set:

android:ellipsize="end"
android:singleLine="true"

Also play with Switch's layout_width: now it fills parent, try to set the exact size you need, if there is. This way the widget will always the size you want, and the text will ellipsize.

abbath
  • 2,444
  • 4
  • 28
  • 40
  • Unfortunately, fixing the width and setting the other two attributes as you specified does not have any effect. I suspect that the switch button is not thought to support lengthy texts as I also tried to manually break the text by adding a \n, the button does not render correctly – SOERGI Jul 03 '15 at 09:15
0

If you need to ellipsize, use singleLine=true then ellipsize=end. Your custom view should have a fixed width (either by weight or fixed dp). If you want the text resized, there's more customization needed. Check this out: https://stackoverflow.com/a/17782522

Community
  • 1
  • 1
josephus
  • 8,284
  • 1
  • 37
  • 57