1

I have created a toggle button programmatically and using it but when it is running on device ,the status of toggle button is overlapping over the toggle button.like in the image enter image description here

the code for toggle is given below

  ToggleButton tb[];
            tb=new ToggleButton[len];
            tb[i]=new ToggleButton(this);

            LinearLayout.LayoutParams tbparams = new LinearLayout.LayoutParams
                    (90, LinearLayout.LayoutParams.WRAP_CONTENT);
            tbparams.setMargins(20, 0, 0, 0);
            tb[i].setBackgroundResource(R.drawable.bgtoggle);
            tb[i].setId(i+1);
            tb[i].setTextOff(" ");
            tb[i].setTextOn(" ");
            tb[i].setTag(stringList.get(i));
            tb[i].setPadding(4, 4, 4, 4);
            tb[i].setLayoutParams(tbparams);
            layout.addView(tb[i]);

    tb[i].setOnCheckedChangeListener(handleOnClick(
            tb[i], tb[i].getId(),tb[i].getTag(),stringList));

bgtoggle.xml

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

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



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

    <item android:drawable="@drawable/off" android:state_checked="false"/>
    <item android:drawable="@drawable/off"></item>-->

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

    <item android:drawable="@drawable/offsw" android:state_checked="false"/>
    <item android:drawable="@drawable/offsw"></item>


</selector>

please help

2 Answers2

2

Thanks all, I corrected it by setting textcolor tranparent

color value= #00ffffff
USB
  • 6,019
  • 15
  • 62
  • 93
0

If you add the toggle button in the layout then it will display as you expect it to be. But in your case, where you are creating it dynamically, you can add

tb[i].performClick();

just before you add it in your linear layout.

Karan
  • 356
  • 2
  • 6