I am creating a simple toggle button in android and setting background as a drawable.
<ToggleButton
android:layout_width="wrap_content"
android:drawablePadding="0dp"
android:layout_height="wrap_content"
android:text=""
android:textSize="12sp"
android:padding="0dp"
android:id="@+id/tag_text"
android:background="@drawable/toggle_selector"/>
toggle_selector.xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/toggle_button_off" android:state_checked="false"/>
<item android:drawable="@drawable/toggle_button_on" android:state_checked="true"/>
</selector>
toggle_button_off and toggle_button_on have simple shape drawable with some color.
And this is how I am inflating this toggle button into my view:
View child = getLayoutInflater().inflate(R.layout.tags, null);
ToggleButton tag = ((ToggleButton)child.findViewById(R.id.tag_text));
tag.setText("Testing");
tag.setTextOff("Testing");
tag.setTextOn("Testing");
flowlayout.addView(child);
The problem is there is just too much padding around the text in toggle button and I am not able to get rid of it by setting padding = "0dp"
. Text on these buttons are dynamically added so setting a constant height weight is not helping too.
I got the solution by setting minWidth, minHeight to 0dp. Wrapping content in the width and height. And then adding the custom padding to togglebutton that I want.
<ToggleButton
android:layout_width="wrap_content"
android:minWidth="0dp"
android:minHeight="0dp"
android:layout_height="wrap_content"
android:text=""
android:textSize="12sp"
android:padding="2dp"
android:id="@+id/tag_text"
android:background="@drawable/toggle_selector"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With