curmudgeon69
curmudgeon69

Reputation: 37

Android - Hide Label on Switch Widget

I'm adding a Switch widget to my screen but I do not want the label to show. If I set android:text to an empty string, or don't set it at all, there is empty gap to the left of the widget and tapping the gap causes the widget to toggle.

Does anyone know how to hide the label portion of a switch? Thank you in advance for your help.

<Switch
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text=""
  android:textOn="@string/switch_text_yes"
  android:textOff="@string/switch_text_no"
  android:checked="false" />

Upvotes: 1

Views: 5174

Answers (3)

Borna
Borna

Reputation: 11

The gap that you are seeing is the switchPadding - minimum space between the switch and caption text.

There is an XML attribute switchPadding (in Switch and SwitchCompat view) that you can set to 0dp to remove the gap.

Docs: SwitchCompat.setSwitchPadding

Upvotes: 1

Dorkmania
Dorkmania

Reputation: 962

I solved this issue by first deleting the value in "text" setting the "layout_width" to "wrap_content", setting the "layout_gravity" to include "left" and adjusting the "switchMinWidth" parameter to make it longer.

Upvotes: 0

curmudgeon69
curmudgeon69

Reputation: 37

I discovered it has something to do with the fact I am using TableLayout. The Switch (although not the actual visible button) was expanding to be as wide as the entire cell even though android:layout_width was set to wrap_content. The visible button was its normal size on the right and the gap on the left was as big as necessary to take up the rest of the cell.

The answer is to set android:layout_gravity="left" on the Switch.

Upvotes: 1

Related Questions