20

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

Pableras84
  • 1,195
  • 5
  • 18
  • 30

10 Answers10

27

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)
    
Vinod Maurya
  • 4,167
  • 11
  • 50
  • 81
SteveR
  • 2,496
  • 1
  • 24
  • 23
  • 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 – NullPointerException Jul 23 '12 at 11:50
  • 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). – SteveR Jul 23 '12 at 16:43
20

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~

surhidamatya
  • 2,419
  • 32
  • 56
RRTW
  • 3,160
  • 1
  • 35
  • 54
13

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.

Tad
  • 4,668
  • 34
  • 35
1

I thought so

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

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"

/>
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
0

Not the most elegant solution but you could try tb.setTextSize(0); if you're just trying to hide the extra space on top. May need to set the textOn/textOff to an empty string: tb.setTextOn("");

EDIT: I better option would be to create your own custom button by extending Button, hold the state (see this SO post for pressed/unpressed states: Pressed android button state) and find the assets to set as the appropriate state...

Community
  • 1
  • 1
Salil Pandit
  • 1,498
  • 10
  • 13
0
 <ToggleButton
                android:id="@+id/setting_tb"
                style="@style/style_setting_tb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:checked="true"
                android:gravity="center"
                android:minHeight="0dp"
                android:minWidth="0dp"
                android:textColor="@color/White"
                android:textOff=""
                android:textOn=""
                android:textSize="14sp" />
Fakhar
  • 3,946
  • 39
  • 35
0

It's much more hassle-free to use a CheckBox instead of the ToggleButton. They have the same states and thus achieve the same functionality, but you can easily change the CheckBox appearence by defining its <android:button="@drawable/..."> attribute.

PJ_Finnegan
  • 1,981
  • 1
  • 20
  • 17
0
<ToggleButton
        android:id="@+id/tooglebutton"
        android:layout_width="60dp"
        android:layout_height="25dp"
        android:textOff="off"
        android:textOn="on"
        android:textSize="0dp"
       />
Sanjay Goswami
  • 191
  • 2
  • 7
  • 4
    Include some explanation, please. – Gnqz May 04 '17 at 11:45
  • 1
    While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) 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. – Nahuel Ianni May 04 '17 at 12:07
0

Layout File.xml

       <ToggleButton
    android:id="@+id/toggle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:background="@drawable/check"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

In main activity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final ToggleButton mic = (ToggleButton)findViewById(R.id.toggle);
    mic.setTextOff(null);
    mic.setTextOn(null);
    mic.setText(null);

check.xml which is used by layout.xml

<?xml version="1.0" encoding="utf-8"?>

    <item android:drawable="@drawable/mic_on"
        android:state_checked="true">

    </item>
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/mic_off"
        android:state_checked="false">

    </item>

These lines are those what you are looking for

    mic.setTextOff(null);
    mic.setTextOn(null);
    mic.setText(null);
Ramandeep Singh
  • 552
  • 6
  • 11