Selector.xml is not working when clicked on the image button

imagebutton
android image button click effect
android studio image button onclick
android image button with text
how to add icon image button in android studio
android button selector
android image button with text below
custom image button

I use the following selector to change image of image button, when I click on it. But this does not work most of the times. Only for few buttons it works. Don't know what the cause here is

 mapselector.xml
 <?xml version="1.0" encoding="utf-8"?>

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Button Pressed -->
        <item android:state_focused="true" android:state_pressed="true" 
    android:drawable="@drawable/mapiconover"/>
</selector>

And in the button,

Am doing like this,

  <ImageButton 
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:id="@+id/btnMap" android:layout_centerInParent="true"  android:src="@drawable/mapselector"
    android:background="@drawable/map" android:scaleType="fitXY"
    android:layout_alignParentBottom="true">
</ImageButton>

but still image is not changing when I click.

Any guess for this?

you have create like this in the ImageButton xml

create xml file using the button image like this

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item 
    android:state_pressed="true" 
    android:drawable="@drawable/backbutton" />
  <item 
   android:drawable="@drawable/closebutton" />
</selector>   

add that xml file as background for IMageButton

<ImageButton                 
    android:layout_height="50px" 
    android:layout_width="50px" 
    android:id="@+id/settings" 
    android:background="@drawable/settings_button" //setting_button in
                                                         the xml file            
    android:text="Settings"/>

Selector.xml is not working when clicked on the image button, I use the following selector to change image of image button, when I click on it. But this does not work most of the times. Only for few buttons it works. Don't know​  Image button click event does not work Apr 05, 2017 08:28 AM | acmedeepak | LINK I have a n imagebutton by clicking which, I will display a list of elements on the screen.

Not sure if this will work but you could try changing your selector to this:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Button Pressed -->
    <item android:state_focused="true" android:state_pressed="true" 
     android:drawable="@drawable/mapiconover"/>
    <item android:drawable="@drawable/map" />

</selector>

And your image button to this:

  <ImageButton 
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:id="@+id/btnMap" 
    android:layout_centerInParent="true"  
     android:background="@drawable/mapselector"
     android:scaleType="fitXY"
    android:layout_alignParentBottom="true">
</ImageButton>

ImageButton, E.g., a blue image by default, an orange one for when focused, and a yellow one for when pressed. An easy way to do this is with an XML drawable "selector. In last Android tutorial, you use “ImageButton” to display a “Button” with a customized background image easily. However, you can do more than that just a simple image, Android allow you to change the button’s image depends on different states like button is focused or button is pressed.

I had the same problem. For me it was because of

android:state_xxxx

conflict. It solved by clarifying the conditions between enabled and pressed. I used the following code to get different states between press and normal state:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:state_pressed="false" >
        <shape
            android:shape="rectangle">
            <gradient android:startColor="#1094ff"
                android:endColor="#7eb6e3"
                android:angle="270" />
            <corners android:radius="@dimen/fab_margin" />
            <stroke android:width="2px"
                android:color="#505050" />
        </shape>
    </item>
    <item android:state_pressed="true" android:state_enabled="true">
        <shape
            android:shape="rectangle">
            <gradient android:startColor="#0075d4"
                android:endColor="#0070cb"
                android:angle="270" />
            <corners android:radius="@dimen/fab_margin" />
            <stroke android:width="2px"
                android:color="#ff0000" />
        </shape>
    </item>
    <item android:state_enabled="false" >
        <shape
            android:shape="rectangle">
            <gradient android:startColor="#b9b9b9"
                android:endColor="#848484"
                android:angle="270" />
            <corners android:radius="@dimen/fab_margin" />
            <stroke android:width="2px"
                android:color="#505050" />
        </shape>
    </item>
</selector>

This example uses different gradient and stroke for different states. You can also use your drawable instead. Just create a file like "mybutton.xml" in your drawable folder. then set the background property of your button like this:

android:background="@drawable/mybutton"

I hope it be helpful.

Pro Android 4, First, you do not specify a <resources> tag as in values XML files. Second, the order of the button images is important. Android will test each item in the selector,​  The ImageButton view combines the Button view and Image view to create a button whose content is an image. The user presses the ImageButton with a finger or clicks it with a mouse to direct the application to carry out a particular task. However, unlike the Button view, the ImageButton view has no concept of text and text appearance.

Pro Android 5, Here we've created the image button in XML and set the button's image from a is an XML file and not an image file, yet that is where the selector file must go. For better working of the onClick attribute, you must take care of these: The method name must be the same in value of onClick attribute and in Activity. The method has to accept a View parameter. The view here means which holds onClick xml attribute or which view was clicked. here, that is button; it needs to return a void value.

Pro Android 3, Here we've created the image button in XML and set the button's image from a is an XML file and not an image file, yet that is where the selector file must go. Thanks for this awesome tutorial, it help me lot to clear my concept. but i'm not able to understand the logic which you done in program to find view of Button and TextView. when i hover over T , it show me [type parameter T extends object ].

Android Apps for Absolute Beginners, Equivalent: Mouse Up HOVERED (API 14) ImageButton State if focused (not touched) The reason this can be difficult is not because of the XML markup that is work which you will need to do for each button, across several ImageButton states This is done using an XML Drawable asset in the form of an image selector  I've created a simple app in PowerApps and want to have that app trigger a flow when a user clicks on a button in the app (similar to what’s described in this video). When I, the creator of the app and flow, click on the button in PowerApps, it works exactly as intended. However, when other users cl

Comments
  • What's the name of the xml file that you have your selector in?
  • @Kurtis..name of the selector is mapselector.xml. I have set that in the image button below.
  • you have create like this, follow this answer stackoverflow.com/questions/7567521/…
  • @Polam, I feel that is same as I did except that it has a color.
  • it is also not giving me the result.