customization - Android: Create a toggle button with image and no text -
is possible create toggle button in android has image no text? ideally this:
ive seen similar posts answer change background want preserve holo light layout , swap text image.
i need able programaticallly change image source,
any ideas how make this?
if cant done, there way can make normal button toggle on , off?
can replace toggle text image
no, can not, although can hide text overiding default style of toggle button, still won't give toggle button want can't replace text image.
how can make normal toggle button
create file ic_toggle in
res/drawable
folder<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="false" android:drawable="@drawable/ic_slide_switch_off" /> <item android:state_checked="true" android:drawable="@drawable/ic_slide_switch_on" /> </selector>
here
@drawable/ic_slide_switch_on
&@drawable/ic_slide_switch_off
images create.then create file in same folder, name ic_toggle_bg
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+android:id/background" android:drawable="@android:color/transparent" /> <item android:id="@+android:id/toggle" android:drawable="@drawable/ic_toggle" /> </layer-list>
now add custom theme, (if not have 1 create styles.xml file in
res/values/
folder)<style name="widget.button.toggle" parent="android:widget"> <item name="android:background">@drawable/ic_toggle_bg</item> <item name="android:disabledalpha">?android:attr/disabledalpha</item> </style> <style name="togglebutton" parent="@android:theme.black"> <item name="android:buttonstyletoggle">@style/widget.button.toggle</item> <item name="android:texton"></item> <item name="android:textoff"></item> </style>
this creates custom toggle button you.
how use it
use custom style , background in view.
<togglebutton android:id="@+id/togglebutton" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="right" style="@style/togglebutton" android:background="@drawable/ic_toggle_bg"/>
Comments
Post a Comment