skayred
skayred

Reputation: 10713

Customizing ToggleButton without PNG

In my application I'm using ToggleButton, but application colors is black and dark-gray, so I want to make the ToggleButton black.

Is there a way to change the background for ToggleButton without using PNG files?

Upvotes: 2

Views: 2754

Answers (2)

PacQ
PacQ

Reputation: 334

I would recommend not using any buttons if you want to change their appearance. It's much more easier to use a View (for example) instead and set it's layout width and height and add custom background. That's background color or image that change on click.

For example:

View v1 = (View) findViewById(R.id.yourToggleView);
v1.setBackgroundColor(Color.WHITE);
tr2Main.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        if(v1.getBackgroundColor == Color.WHITE){
                v1.setBackgroundColor(Color.RED);
        }else{
        v1.setBackgroundColor(Color.WHITE);
        }
    }
});

If you have an XML with a View

<View android:id="@+id/yourToggleView"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="#FFFm"
            />

Upvotes: 1

87element
87element

Reputation: 1939

Maybe a simple selector is your choice. You just specify button states, shapes and colors, so as corners, gradients and whatevah:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="true">
   <shape xmlns:android="http://schemas.android.com/apk/res/android"
          android:shape="rectangle">
    <corners android:radius="7dip"/>
    <gradient android:startColor="#Color1" 
          android:endColor="#Color2"
          android:angle="90" />
   </shape>
 </item>
 <item android:state_selected="true">
....
</item>
</selector>

And then:

<ToggleButton android:id="@+id/main_togglebutton1"
android:background="@drawable/my_toggle_button_selector"
...
/>

Upvotes: 3

Related Questions