1

I am trying to implement toggle functionality on a star button in Android. This is my imagebutton in res/my:

<ImageButton
            android:id="@+id/star_icon"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/star"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:clickable="true"
            android:onClick="onToggleStar"
            android:background="#00ffffff"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:padding="20dp"/>

This is drawable/star.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:state_selected="true" android:drawable="@android:drawable/btn_star"/> <!-- pressed -->
    <item android:drawable="@android:drawable/btn_star_big_off"/>

</selector>

This is my onclick handler:

public void onToggleStar(View view)
    {
        view.setSelected(!view.isSelected());
    }

the problem I am facing is button src is always taking default value i.e. btn_star_big_off

This question has been asked before here Android ImageButton with a selected state? and some other places as well but I am not able to figure out any problem.

Edit

I have tried this as well

<Button
        android:id="@+id/star_icon"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:background="@drawable/star"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:clickable="true"
        android:onClick="onToggleStar"

        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:padding="20dp"/>

with other two functions same. I am using genymotion emulator.

halfer
  • 19,824
  • 17
  • 99
  • 186
best wishes
  • 5,789
  • 1
  • 34
  • 59
  • can you try removing android:src="@drawable/star" line and change android:background="#00ffffff" to android:background="@drawable/star" , I am sure it will work. – dex Oct 27 '15 at 03:37

3 Answers3

0

ImageButton is subclass of ImageView,not Button,it's not clickable by default and it doesn't support selector drawable,so your selector won't work on ImageButton.
So to achieve your goal,you can set imagebutton's src programmally or change ImageButton to Button,and set @drawable/star as its background attr.

starkshang
  • 8,228
  • 6
  • 41
  • 52
  • stackoverflow.com/questions/2604599/android-imagebutton-with-a-selected-state , here also they used image button and selector was working fine ! – dex Oct 27 '15 at 03:30
  • but I have added android:clickable="true" ,@dex I tried that as well, but could not get it working, I am sorry if it sound childish. Please help – best wishes Oct 27 '15 at 03:47
  • @FireSun I tried that as well but this is also not working, any other guess thanks, please see my edit. – best wishes Oct 27 '15 at 04:09
  • @dex in your link of similar question,they make it by check the imagebutton's state and according to the state set image programmally,not by selector. – starkshang Oct 27 '15 at 05:03
  • @FireSun I tried this but still this is loading empty star, this means there is a problem loading image, any idea why this is happening? thanks – best wishes Oct 27 '15 at 05:19
0

The problem is

@android:drawable/btn_star

is itself a built-in button selector with many items,

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:state_window_focused="false" 
          android:drawable="@drawable/btn_star_big_off" />
    <item android:state_checked="true" android:state_window_focused="false" 
          android:drawable="@drawable/btn_star_big_on" />
    <item android:state_checked="true" android:state_window_focused="false" 
          android:state_enabled="false" android:drawable="@drawable/btn_star_big_on_disable" />
    <item android:state_checked="false" android:state_window_focused="false" 
          android:state_enabled="false" android:drawable="@drawable/btn_star_big_off_disable" />

    <item android:state_checked="true" android:state_pressed="true"
          android:drawable="@drawable/btn_star_big_on_pressed" />
    <item android:state_checked="false" android:state_pressed="true"
          android:drawable="@drawable/btn_star_big_off_pressed" />

    <item android:state_checked="true" android:state_focused="true"
          android:drawable="@drawable/btn_star_big_on_selected" />
    <item android:state_checked="false" android:state_focused="true"
          android:drawable="@drawable/btn_star_big_off_selected" />

    <item android:state_checked="true" android:state_focused="true" android:state_enabled="false"
          android:drawable="@drawable/btn_star_big_on_disable_focused" />
    <item android:state_checked="true" android:state_focused="false" android:state_enabled="false"
          android:drawable="@drawable/btn_star_big_on_disable" />

    <item android:state_checked="false" android:state_focused="true" android:state_enabled="false"
          android:drawable="@drawable/btn_star_big_off_disable_focused" />
    <item android:state_checked="false" android:state_focused="false" android:state_enabled="false"
          android:drawable="@drawable/btn_star_big_off_disable" />

    <item android:state_checked="false" android:drawable="@drawable/btn_star_big_off" />
    <item android:state_checked="true" android:drawable="@drawable/btn_star_big_on" />
</selector>

So to get your code working, change

android:src="@drawable/star"

to

android:src="@android:drawable/btn_star"
Sajith Sageer
  • 163
  • 3
  • 16
0

@user3892213 first try to add one more item with selected="false" after selected="true"

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item  android:state_selected="true" android:drawable="@android:drawable/btn_star"/> 
    <item  android:state_selected="false" android:drawable="@android:drawable/btn_star_big_off"/> 
    <item android:drawable="@android:drawable/btn_star_big_off"/>
</selector>

then on java code first set the selection of the star to false

<starObject>.setSelected(false);

then try to put log inside click listener and check the state of star

public void onToggleStar(View view)
{
    Log.d("STAR", view.isSelected()+"");
    view.setSelected(!view.isSelected());
}
Angad Tiwari
  • 1,738
  • 1
  • 12
  • 23