Removing padding from toggle button in Android
Asked Answered
B

2

11

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.

enter image description here

Bitolj answered 28/4, 2015 at 6:11 Comment(13)
what You mean with too much padding? What about change the text size?Mammillary
padding between text and the boundary of togglebutton. I want the button to wrap the text.Bitolj
that is default height and width if you wanna wrap then you have to make drawables of image or give height or widthSonnie
Make sure there is no padding in toggle_button_off and toggle_button_onQuadroon
there is no padding in toggle_button_off and toggle_button_on. just the colorBitolj
instead of width and height as wrap content, give your own dp values.Cracking
But the text on toggle button is dynamic. how do I handle that?Bitolj
like Vishnu said, I think it is the only solution to give fixed sizes to the button..Mammillary
dynamic? You mean You set the text size programmatically?Mammillary
can you specify what do you mean by dynamic, is it other than on/off.Cracking
No I am writing text on toggle button dynamically while creating it. I have added that code in question. check it outBitolj
set android:minWidth and android:minHeight to your toggle button. The button will automatically increase with if the width of the text is long.Cracking
I have set the min width height to 0dp and wrapped the width and height. And this seem to work. Thanks for the help!Bitolj
B
39

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"/>
Bitolj answered 28/4, 2015 at 6:57 Comment(2)
Does not work for me, unfortunately. I have programatically set those values and the ToggleButton still adds that large padding. Android 5.0.1Rejoice
The only way I found to remove padding is to set programmatically the minHeight AND the minimumHeight to 0 (you can't set the minimumHeight into the xml layout) : toggleButton.setMinHeight(0); toggleButton.setMinimumHeight(0); Idem for the widthKautz
R
1

Use CheckedTextView instead:

 final CheckedTextView toggle = new CheckedTextView(clipboardWordsLayout.getContext());
 toggle.setText(kanji);
 final int min = Views.dp2px(48, clipboardWordsLayout.getContext());
 toggle.setMinimumWidth(min);
 toggle.setMinimumHeight(min);
 toggle.setGravity(Gravity.CENTER);
 toggle.setBackgroundResource(R.drawable.togglebutton);
 toggle.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
         toggle.setChecked(!toggle.isChecked());
     }
 });
Rejoice answered 18/3, 2016 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.