Android: ImageButton not showing states pressed - android

I have an ImageButton and I swear I have everything set up right but it is not showing the different icon when a state is pressed. Here is what I have.
ImageButton
<!-- HOME BUTTON -->
<ImageButton
android:id="#+id/home"
android:layout_width="45dp"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
android:contentDescription="Home"
android:src="#drawable/title_home"
android:layout_alignParentLeft="true"
android:onClick="onClickHome" />
Title Home
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/title_home_pressed"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/title_home_pressed"/>
<item android:state_focused="true" android:drawable="#drawable/title_home_pressed"/>
<item android:state_focused="false" android:state_pressed="false" android:drawable="#drawable/title_home"/>
</selector>
And yes these are all valid images, and they are different. title_home is the only one that is showing up.
I would appreciate any ideas?

Notice how you are referring to #drawable/title_home in the first block of code, but you are intending to refer to the xml file. Although in the second block of code you are referring to another #drawable/title_home which I am assuming is an image.
Because of this I can only assume that from your information your first block of code is reading the image and not the xml file. To fix this, I believe changing the names of one of them would help.

android:src="#drawable/title_home"
<item android:state_focused="false" android:state_pressed="false" android:drawable="#drawable/title_home"/>
Same name. Change your xml file name to title_home_btn_selector.
Then make the first one as
android:src="#drawable/title_home_btn_selector"

Related

Changing Button UI in Android

I am trying to customize a Button UI in Android.
I tried the following things:
btn.setBackgroundColor
btn.setBackgroundResource
btn.setBackgroundColor
But all of these are increasing the size of the Button, and because of that the Buttons near by can not be segregated (??).
Please suggest something.
If you want to apply a Hover effect then you have to do this in your XML where button layout is like
Button
android:id="#+id/xyz"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="15dp"
android:background="#drawable/general_btn_hover_effect"
android:onClick="somefunction"
android:text="#string/search_number"
android:textColor="#ffffff"
android:textSize="20sp"
/>
Notice android:background="#drawable/general_btn_hover_effect" there and then in #drawable folder make a general_btn_hover_effect.xml and write this into it
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/blank_normal_bg" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="#drawable/blank_hover_bg" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="#drawable/blank_hover_bg" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="#drawable/blank_normal_bg"/>
</selector>
Changing background color can not change size of button. Maybe you changed size in xml layout. Check again

State List drawable does not show up in all screens

I have a drawable named btndonate.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize="true" android:dither="true"
android:variablePadding="true"
>
<item android:drawable="#drawable/donate_hover"
android:state_pressed="true" />
<item android:drawable="#drawable/btn_donate"
android:state_window_focused="true" />
<item android:state_focused="true" android:drawable="#drawable/donate_hover"> </item>
<item android:state_focused="false" android:drawable="#drawable/btn_donate"></item>
</selector>
I am using it in activity's xml like
<Button android:layout_centerInParent="true"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="#drawable/btndonate" android:id="#+id/btn_home_donate"></Button>
i am using the same button with different id's in my other activities xmls.. but it shows up in some and hidden in others i dont know y the same button does not show up n all my activities though the drawable is same for all of them .. ??
any one plz help ??
That's odd... Are you sure, you didn't make any mistakes?
Do you use the same xml file for your script?
Try using this:
Button anyButton = (Button) findViewById(R.id.anyButton);
anyButton.setVisibility(View.VISIBLE);
Hope this helps. Don't forget to mark correct when it works. ;)

How to reference the system select button image for custom preference in Android

I have a customer preference widget (xml) which basically contains a TextView and next to that a right aligned ImageButton.
What I am trying to do is set the image for my image button to whatever is used as default by the preferences system. For example see the attached image where I've highlighted the type of image I want to use.
I've tried looking online and saving the ic_btn_round image into my drawables and using that, but it just doesn't look right.
Any advice welcome, thank you
This circle button comes from DialogPreference. If we look into how this preference is implemented, we'll find, that it uses widget described in this xml file:
<!-- Layout used by DialogPreference widgets. This is inflated inside
android.R.layout.preference. -->
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="4dip"
android:layout_gravity="center_vertical"
android:background="#drawable/btn_circle"
android:src="#drawable/ic_btn_round_more" />
So you need to look into 2 more files: drawable/btn_circle and drawable/ic_btn_round_more.
btn_cicle.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/btn_circle_normal" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/btn_circle_disable" />
<item android:state_pressed="true" android:state_enabled="false"
android:drawable="#drawable/btn_circle_disable" />
<item android:state_pressed="true"
android:drawable="#drawable/btn_circle_pressed" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/btn_circle_selected" />
<item android:state_enabled="true"
android:drawable="#drawable/btn_circle_normal" />
<item android:state_focused="true"
android:drawable="#drawable/btn_circle_disable_focused" />
<item
android:drawable="#drawable/btn_circle_disable" />
</selector>
ic_btn_round_more.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#drawable/ic_btn_round_more_disabled" />
<item
android:drawable="#drawable/ic_btn_round_more_normal" />
</selector>
All drawables referenced from these two xml files are actual *.png files.
So basically you need to use two images to achieve desired effect. But all these resources are internal to Android, and you can't use them directly. The easiest way is to just copy them to your project (as you did with ic_btn_round ).

Android ImageButton state not changing

I have an issue with my ImageButton not changing state. When I click, or rather touch, the button it stays as the same image. Here is the XML I am using as a selector.
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/pushed" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/pushed" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/pushed" />
<item
android:drawable="#drawable/default" />
</selector>
I call this selector from my main.xml as
android:background="#drawable/imagechoice"
imagechoice.xml is the file with the selector
I don't understand why this is not working, unless I have to have some java code, but everything I've seen said this should work.
When using an ImageButton, isn't it the 'src' property you should use and not background?
Make sure that you copy the same images and the button XML into every "drawable" folders (hdpi,ldpi,mdpi).
That's how I solved this problem on my app.
Good luck :)
I have nearly the same XML and it works just fine. Are you sure you're not replacing the drawable in code somewhere?
On another note, your XML can be simplified by using the cascading nature of the state matching.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/pushed"
/>
<item android:state_focused="true"
android:drawable="#drawable/pushed"
/>
<item android:drawable="#drawable/default"
/>
</selector>
This is my xml of a button with my own custom image on it and it works great:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/btn_off" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/btn_pressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/btn_pressed" />
<item android:drawable="#drawable/btn_off" />
</selector>
Make sure you set Image button background as mentioned below.I think you are not setting the selector as background instead you are setting the image as background.
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button_up_selector"
android:text="1"
android:textColor="#fffafa"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"/>

Android - different image for rollover on ImageButton

Is it possible to specify a different image when the user's focus comes to an ImageButton? I want to display an image button on a LinearLayout and change the image when the user's focus comes on the button or when the user presses the button.
Thanks.
Yes, you can do this. What you need is a drawable xml file that defines a selector.
<selector xmlns:android...
<item android:state_enabled="false" android:state_focused="true" android:drawable="..." />
<item android:state_enabled="true" android:state_focused="false" android:drawable="..." />
</selector>
Then, use the id of this drawable XML when specifying the ImageButton in your layout XML.
The precedent answer did not work for me. Here is the code I found somewhere else:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/refresh_pushed" android:state_pressed="true" />
<item android:drawable="#drawable/refresh" />
</selector>
You can also add a state for foccussed objects by adding a line and using:
android:state_focused="true"

Categories

Resources