2

I am using a ToggleButton with an image. The image is 16x16, however, I want the button to be 50x50 so that when users click around the image even then the action is taken. This is how I'm avoiding the fat finger problem.

This is how my ToggleButton is in the XML

    <ToggleButton android:id="@+id/fav"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#FFFFFF"
        android:gravity="center"
        android:focusable="false"
        android:layout_marginRight="10dp"
        android:padding="15dp"
        />

and I set the background programmatically like this:

if (holder.favButton.isChecked()) 
  holder.favButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.star_2));
else 
  holder.favButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.star_1));

Problem

The image looks expanded and blurry because the width and height is 50dp, however, the image is only 16x16. Instead of setting the background with setBackgroundDrawable, I want to find a way to set the image src of the button. Because if I'm setting the background then the image takes over the entire background which is 50x50.

Anthony
  • 33,838
  • 42
  • 169
  • 278
  • I didn't know if there is a way to set the ToggleButton's src image in XML. Please let me know how to do that and I can try it. I can still set the `setChecked(..)` value programmatically. I have to do it programmatically because the results are being persisted in the DB and the first time the user opens the screen..what ever they clicked last time is fetched from the DB – Anthony Jun 03 '14 at 17:13
  • setting the background is how you set a custom togglebutton change your image or use wrap_content – tyczj Jun 03 '14 at 17:15
  • See this answer: http://stackoverflow.com/questions/18598255/android-create-a-toggle-button-with-image-and-no-text – Phantômaxx Jun 03 '14 at 17:16
  • `wrap_content` I tried but isn't working for me :( – Anthony Jun 03 '14 at 17:16
  • @DerGolem in 3rd part of that answer `How may I use it` they are still setting the background. which is still going to look weird because the background is 50*50 but image is 16*16.. – Anthony Jun 03 '14 at 17:18
  • Nonononononono... choose the part where they do all by XML: the selector method. – Phantômaxx Jun 03 '14 at 17:20
  • I did...same thing still persist. The toggle button image is bigger than rest of the icons (`ImageButton`). I think I'll just set the width height to 16 instead of 50.... – Anthony Jun 03 '14 at 18:21
  • I guess I should be asking the question "How to expand the clickable area of a ToggleButton without making its image bigger" – Anthony Jun 03 '14 at 18:42

1 Answers1

0

Can this help you? State List I alway using a custom drawable with Selector and set it to ToggleButton

Trần Đức Tâm
  • 4,037
  • 3
  • 30
  • 58