martin
martin

Reputation: 1139

Android ToggleButton indicator

I'm struggling with the Android ToggleButton because I try to change the green indicator light. In the Android SDK folder I've found the file drawable\btn_toggle_bg.xml where it says

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+android:id/background" android:drawable="@android:drawable/btn_default_small" /> 
  <item android:id="@+android:id/toggle" android:drawable="@android:drawable/btn_toggle" /> 
</layer-list>

I copied this file to the drawable folder of my project and changed the last item to

<item android:id="@+android:id/toggle" android:drawable="@drawable/btn_toggle" />

and then took the original btn_toggle.xml from the Android SDK, copied into the drawable folder, too. btn_toggle.xml is this:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_checked="false" android:drawable="@drawable/btn_toggle_off" /> 
  <item android:state_checked="true" android:drawable="@drawable/btn_toggle_on" /> 
</selector>

So then I asumed that it would be necessary to create btn_toggle_off.png and btn_toggle_on.png. At last I added the line

<ToggleButton
...
android:background="@drawable/btn_toggle_bg" />

but finally, the ToggleButton looks completely strange. What I noticed are files called btn_toggle_off.9.png and btn_toggle_on.9.png in the SDK but I couldn't find a reference to these files although they look exactely like the original indicator.

Can you help me? :)

Upvotes: 3

Views: 4737

Answers (1)

Jeff Gilfelt
Jeff Gilfelt

Reputation: 26159

Without completely re-styling the toggle button widget, the resources you are trying to replace should probably be nine patch PNGs like the originals are.

Copy btn_toggle_off.9.png and btn_toggle_on.9.png into your project and modify them, or use the draw9patch tool to properly add the nine patch metadata to your own images.

Refer to the following documentation for an explaination of Android's nine patch drawables:

http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

http://developer.android.com/guide/developing/tools/draw9patch.html

The ".9" part of the filename must be retained, but is not referenced in XML resources. So btn_toggle_on.9.png is referred to as simply "@drawable/btn_toggle_on".

Upvotes: 3

Related Questions