ImageButton selector not working

java imagebutton
android imagebutton
android image button with text
how to add icon image button in android studio
imagebutton constructor
android selector drawable
imagebutton properties
android-imagebutton tint

I am trying to set selector for ImageView, but it is not working

My layout

 <LinearLayout
        android:id="@+id/actionLayout"
        android:orientation="horizontal"
        android:background="@color/orange_color"
        android:gravity="center_vertical"
        android:paddingTop="@dimen/actions_top_bottom"
        android:paddingBottom="@dimen/actions_top_bottom"

        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="@dimen/actions_height"
            android:id="@+id/btnNoRecord"
            android:scaleType="centerInside"
            android:src="@drawable/ic_action_record"
            android:onClick="onRecordSwitcherClick"
            android:layout_weight="1"
            android:background="@drawable/selector"/>
...
</LinearLayout>

My selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/dark_orange_color"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/orange_color"/>
        </shape>
    </item>
</selector>

Where is bug? Maybe there is something bug inside LinearLayout properties?

EDIT I just found ImageButton outside LinearLayout working fine. But i'm really needed layout

Your selector doesn't look correct.

I think you might be missing the states (ie. state_focused="true", etc). It is important to remember that the selector is analyzed in order from top to bottom (so if a state is encountered first, the other items in the selector will be ignored - ie. order matters).

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/btn_settings_inset_top_pressed"
        android:state_focused="true"
        android:state_pressed="true"/>
    <item
        android:drawable="@drawable/btn_settings_inset_top_pressed"
        android:state_focused="false"
        android:state_pressed="true"/>
    <item
        android:drawable="@drawable/btn_settings_inset_top"
        android:state_focused="true"
        android:state_pressed="false"/>
    <item
        android:drawable="@drawable/btn_settings_inset_top"
        android:state_focused="false"
        android:state_pressed="false"/>
</selector>

Problem with ImageView/ImageButton selector src/background , Problem with ImageView/ImageButton selector src/background. I've tried literally everything under the sun. All I want to do is set a state list selector as the src or� Firstly despite having a strong 4-bar out of 4 signal, online services and the internet do not work at all when a 3G signal is being received (e.g. news, weather, all widgets, internet, email etc).Calls and texts do work however with a 3G signal.But when a 3g+ signal (HSDPA) is being received, all internet services as above work fine.However

Set clickable attribute in your ImageButton as shown below:

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="@dimen/actions_height"
    android:id="@+id/btnNoRecord"
    android:src="@drawable/ic_action_record"
    android:background="@drawable/selector"
    android:clickable="true"/>

ImageButton is extended from ImageView. The clickable attribute is false by default so you have to manually set it.

Android ImageButton selector example, Android ImageButton selector example. In last Android tutorial, you use “ ImageButton ” to display a “ Button ” with a customized background image easily. state_focuse is not working even i download ur project run it's also. Assuming that you are using version control (TFS, Git etc), compare your non-working code with the previous working code. If you put a breakpoint on the line: checkerBTtn = FindViewById(Resource.Id.checkerBtn); does the debugger hit that breakpoint? If you put a breakpoint on the line: healthBtn.Click += delegate does the debugger hit that

put padding in your imageview to see the background selector

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="@dimen/actions_height"
        android:padding="5dp"
        android:id="@+id/btnNoRecord"
        android:scaleType="centerInside"
        android:src="@drawable/ic_action_record"
        android:onClick="onRecordSwitcherClick"
        android:layout_weight="1"
        android:background="@drawable/selector"/>

                         OR 

Try it like this , this will work you have to give padding so that background selector is visible

<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_pressed="true">
       <shape android:shape="rectangle">
            <solid android:color="@color/dark_orange_color"/>
            <padding
            android:bottom="5dp"
            android:left="5dp"
            android:right="5dp"
            android:top="5dp" />
      </shape>
</item>
<item>
      <shape android:shape="rectangle">
          <solid android:color="@color/orange_color"/>
          <padding
            android:bottom="5dp"
            android:left="5dp"
            android:right="5dp"
            android:top="5dp" />
      </shape>
</item>

ImageButton, A Solution to the ImageButton Problem every button with a state list drawable XML, like so: <?xml version="1.0" encoding="utf-8"?> <selector� Ok, so when I'm using ImageButton in my xml, and i keep encountering problems when lauching and the console in eclipse says Unable to resolve drawable

I have solved it!

Inside my code i have

btnNoRecord.setOnTouchListener(new View.OnTouchListener() {
   @Override
   public boolean onTouch(View view, MotionEvent motionEvent)
   {
       ...
       return true;
   }
});

I change return true to return false and it is working!

A Solution to the ImageButton Problem – KlaasNotFound , 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 � ImageButton Tutorial With Example In Android Studio. In Android, ImageButton is used to display a normal button with a custom image in a button.In simple words we can say, ImageButton is a button with an image that can be pressed or clicked by the users.

In this case just swap attributes:

android:src
android:background

to

android:src="@drawable/selector"
android:background="@drawable/ic_action_record"

Hint. You can remove background by:

android:background="@null"

Selector.xml is not working when clicked on the image button, In Android you can use the ImageButton component to display a If the image does not appear in the Package Explorer under the folder� An ImageButton is an AbsoluteLayout which enables you to specify the exact location of its children. This shows a button with an image (instead of text) that can be pressed or clicked by the user. Android button style set ImageButton Attributes. Following are the important attributes related to ImageButton control.

Android ImageButton Selector Example, 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 � All I want to do is set a state list selector as the src or background for an imageview or imagebutton. It'll be a custom view/button in the end, but right now I can't even get it working with a stock widget. I literally have a blank project, add an imageView to the layout, and set its source to the selector. Tried background. Tried imageButton.

Selector.xml is not working when clicked on the image button, not working,and I can't set others color、shape or selector,they are is to use a FrameLayout with an ImageButton and a (Material)TextView� button_selector.xml: Define the selector drawable. activity_button_selector.xml: Define three buttons, and use button_selector as the OK button background. ButtonSelectorActivity.java: The java file that contain the logic code, such as button click listener. 1. button_selector.xml. This is the selector definition xml file.

[Button] android:background not working � Issue #889 � material , Keeping it at the top would not allow the other two item tags to get executed ever. For the above selector to work, we need to specify android:enabled as false. ImageButton comes with the android:scale attribute to resize the drawable�

Comments
  • the name is selector or Myselector.xml?
  • No. Name is selector.xml
  • Your selector is working on my device.What problem are you facing?
  • I do not see chenged background when i press button.
  • Do you want shadow?? @Sufferer
  • I am not needed focused state. Also <item android:state_pressed="true" android:state_focused="true">...</item> not working too
  • This is a selector I use that for sure is functional. If you don't need all the particular states, you can omit the ones you don't need.
  • Thank you. I have tried it, but it is not working. Are you tried your selector inside ImageButton as property android:background?
  • Yes, I am using this exact code now by setting this selector as a background. This is working in production.
  • ImageButton is extended from ImageView,clickable attribute is false by default.