1

I created a drawable for my ToggleButtons, but I also need to show an icon. How can I do this if there's just a background attribute?

Here's the ToggleButton drawable:

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <corners android:radius="8dp" />
    <stroke android:color="#CED2D4" android:width="1dp" />
    <solid android:color="#EDEEEF"/>

</shape>

And the code to place the ToggleButton (currently using IconFont, but I have to change as the items are dynamic):

<ToggleButton
                android:id="@+id/toggleCreditCard"
                android:layout_width="@dimen/width_filter_facilities_button"
                android:layout_height="@dimen/height_filter_facilities_button"
                android:background="@drawable/filter_service_toggle_default"
                android:textOff="@string/fa_credit_card"
                android:textOn="@string/fa_credit_card" />
juliano.net
  • 7,982
  • 13
  • 70
  • 164

1 Answers1

0

This may help you. You can create a layer-list and add two item. First item is your custom drawable, the second is your icon.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/list_bg_top"></item>
<item android:left="10dp">
    <bitmap android:src="@drawable/waiting_status_btn"
      android:gravity="center_vertical|left" />
</item>

Check it out: customize toggle button on Android

Community
  • 1
  • 1
Huy Nguyen
  • 1,032
  • 14
  • 38
  • This way I'll have two problems. I won't be able to create the buttons dynamically and I'd need a drawable for each button. – juliano.net Mar 06 '17 at 03:06
  • If your buttons is different, you have to create drawable for each button. That is obvious. Another way, You can design drawable include both custom background and icon in there, then use it as background of toggle button. In this way, you have to know design software as AI, PTS – Huy Nguyen Mar 06 '17 at 10:04