It is possible to create a ToggleButton without text?

Viewed 24459

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

10 Answers

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);
Related