Setting background on ImageButton doesn't work - android

I have an ImageButton and I wanted to be able to see a color when I clicked on it. I added a background, and used the same listselector I use for all my listviews, but it doesn't show anything when I click the ImageButton.
Here is the ImageButton xml
<ImageButton
android:id="#+id/button_biomes"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/biome"
android:background="#drawable/listselector"/>
And this is the listselector drawable:
<?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="true" android:drawable="#color/actionbar" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="#color/actionbar" />
<item
android:drawable="#color/android:transparent" />
</selector>
If anyone could help me It'd be greatly appreciated. Thanks!

It is possible that your image takes up entire space and doesn't show the background. Here, image and background are two different things, so do not expect the src image to be transparent.

Related

Button raised effect not working when background image attached

I am creating a simple button as follows :
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edSearch"
android:id="#+id/btnSearch"
android:layout_marginTop="30dp"
android:drawableTop="#drawable/ic_action_search"
android:textSize="16sp"
android:text="Search"
android:background="#color/md_green_500"
android:textColor="#fff"
/>
Now, when i try add any image in background like :android:background="#drawable/img123" then button raise effect does not work. is there any way i can achieve raise effect with background image ?
You can create a selector drawable for this. You need to provide 3 different images for this so that the button can toggle those images as per the state.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/image_normal" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/image_pressed" />
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/image_focused" />
</selector>
Also inspite of that you can use ImageButton which will keep your image in center of button and the effects will be default as per operating system.

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

Image selector not working well

This is my problem: I have an ImageView and I assign a selector to it, so the image have to change when I touch it. The problem is that the image doesn't change.
The selector has a different name than the image sources, so that is not a problem. When I touch the image, the app does an http call, so I think that there is the problem, but I don't know how to solve this.
Selector xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/pat_login_button_pressed"
android:state_selected="true" />
<item android:drawable="#drawable/pat_login_button_pressed"
android:state_pressed="true" />
<item android:drawable="#drawable/pat_login_button"
android:state_selected="false" />
</selector>
ImageView xml:
<ImageView
android:id="#+id/login_button"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:src="#drawable/pat_login_button_selector" />

Android Cusom Button Stretch Background

i am working custom button in android and i am set custom style and apply from back ground of button and my problem is image not stretch button background and i want to stretch background based on button size here' my custom button xml code:
custom_button.xml
<?xml version="1.0" encoding="UTF-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#drawable/disable" />
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="#drawable/pressed" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="#drawable/focused" />
<item android:state_enabled="true"
android:drawable="#drawable/normal" />
</selector>
here's my layout xml code refer button style xml code:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:text="Button"
android:background="#drawable/custom_button" />
here's my output below screen shot:
anyone can help me how to fix this issue with greatly appreciated!
Thanks in Advance!
use 9-patch images for selector images. this will help Android to stretch your images as you described in your 9-patch boundaries
Draw 9-patch
Draw 9-patch Tutorial
Another Android 9 Patch Image Tutorial
Use the draw9patch application found in android-sdk/tools. Check instructions how it works and backgrounds should be able to scale properly.

selector not working with layout and image view

I want to use selector for a layout and image view..But it is not working for me.
When I changed image view to imagebutton or button, it works fine. Any help will be greatly appreciated.. Thanks in advance..
here is the xmls I used..
<?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/bg"
/>
<item
android:drawable="#drawable/bg_01" />
</selector>
The layout xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">
<ImageButton
android:layout_width="100dp"
android:layout_height="50dp"
android:text="layout tester"
android:textColor="#000000"
android:textStyle="bold"
android:paddingTop="10dp"
android:src="#drawable/testselector"
android:layout_gravity="center_horizontal" />
</LinearLayout>
I ran into this problem just now. You'll have to set the LinearLayout to clickable. You can either do this in the XML with
android:clickable="true"
Or in code with
yourLinearLayout.setClickable(true);
try this
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/lastread_ok" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/lastread_ok" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/lastread_ok_selected" />
<item android:drawable="#drawable/lastread_ok" />
</selector>
you can use your image instead of lastread_ok & lastread_ok_selected
use the id of imageview instead of linear layout and setclicklistener for imageview itself, it will work!!
You have to change src to background
or
ImageButton to ImageView and alse write android:clickable="true"in ImageView attributes.
For me it helped to not assign the attribute android:clickable to the layout but to the ImageView instead. I just wanted the image to be clickable though, not the containing layout.
setOnClickListener() for selectable components solved the problem

Categories

Resources