It is possible to create a ToggleButton without text?

23,927

Solution 1

The empty space in the top is caused by the 2 Ninepatch (btn_toggle_off.9.png and btn_toggle_on.9.png ) used in default toggleButton style.

To perfectly center the horizontal bar, you must create a new style for ToggleButton that will use two new ninepatch derived form original.

Edit: Method requiring minimal XML:

  • create your two ninepatches for your togglebutton, name it: my_btn_toggle_on and my_btn_toggle_off

  • in drawable folder, create my_btn_toggle.xml:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="false" android:drawable="@drawable/my_btn_toggle_off" />
        <item android:state_checked="true" android:drawable="@drawable/my_btn_toggle_on" />
    </selector>
    
  • in your code, add tb.setBackgroundResource(R.drawable.my_btn_toggle) :

    ToggleButton tb = new ToggleButton(map);
    tb.setText(null);
    tb.setTextOn(null);
    tb.setTextOff(null);
    tb.setBackgroundResource(R.drawable.my_btn_toggle)
    

Solution 2

Just set following things:

<ToggleButton android:id="@+id/tbtn1" 
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:background="@drawable/bg_check"
 android:textOn="" 
 android:textOff="" 
 android:text="" />

And, the style xml, if you need...

<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_checked="true" 
android:drawable="@drawable/bg_check_on" /> <!-- pressed -->

  <item android:drawable="@drawable/bg_check_off" /> <!-- default/unchecked -->
</selector>

Hope it helps~

Solution 3

Using

<ToggleButton
    ...
    android:textOff="@null"
    android:textOn="@null"
    android:textSize="0dp" />

will not only make the text go away, but also get rid of the empty space where it would otherwise be shown.

Solution 4

I thought so

android:textColor="@android:color/transparent"

Solution 5

Fill style XML like this.. the magic is text color as transparent...

android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textOff="blabla"
android:textOn="blablabla"
android:textColor="@android:color/transparent"
android:background="@drawable/boutmediumrouge"

/>
Share:
23,927
Pableras84
Author by

Pableras84

Updated on July 09, 2022

Comments

  • Pableras84
    Pableras84 almost 2 years

    i want to create a android default ToggleButton like this: enter image description here

    but i want to create it without TEXT

    I tryed with this code:

    ToggleButton tb = new ToggleButton(map);
    tb.setText(null);
    tb.setTextOn(null);
    tb.setTextOff(null);
    

    But it is leaving a empty space in the top of the horizontal green bar.

    I dont want that empty space, i only want the horizontal green bar.

    How to achieve it?

    thanks

  • NullPointerException
    NullPointerException almost 12 years
    can you explain how to create that style and how to add it?? all with java code, Pableras is not using XML for designing the tooglebutton
  • SteveR
    SteveR almost 12 years
    I don't think it is possible to achieve without XML. (Pableras doesn't explicitly say he wants in java code only). I edited my answer with a method requiring minimal xml (perhaps useful).
  • Gilian
    Gilian almost 8 years
    Correct answer, and not is needed android:textSize="0dp"
  • Gnqz
    Gnqz about 7 years
    Include some explanation, please.
  • Nahuel Ianni
    Nahuel Ianni about 7 years
    While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
  • Mathews Sunny
    Mathews Sunny over 6 years
    add some description to your answer on, how this would work.
  • Sam Chen
    Sam Chen over 3 years
    Perfect answer. Thank you!