I am trying to create a button that has a default image, but when it is pressed it switches to a new image. I have used this tutorial along with this question but it will not work.
This is my info_button_selector.xml:
<?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/ic_menu_info_details2" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/ic_menu_info_details2" /> <!-- focused -->
<item android:drawable="#drawable/ic_menu_info_details" /> <!-- default -->
</selector>
This is the code with my ImageButton:
<ImageButton
android:id="#+id/apModeInfoButton"
android:layout_width="0dip"
android:background="#null"
android:layout_height="match_parent"
android:layout_weight="1.15"
android:clickable="true"
android:src="#drawable/info_button_selector" />
The button stays at the initial state looking like ic_menu_info_details the entire time.
I changed my info_button_selector.xml file to 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/ic_menu_info_details2" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/ic_menu_info_details2" /> <!-- focused -->
<item android:drawable="#drawable/ic_menu_info_details2" /> <!-- default -->
</selector>
The ic_menu_info_details2 image was the displayed the entire time, which is expected. This shows that the button IS using the XML file as its drawable resource, but why is it that the button does not change its image when pressed?
EDIT
Using Joss's answer below, the image still does not change, and is now stretched in a strange way. Note: Both images are exactly the same except the second image is scaled to be 60% the size. This might mean that it is working but I cannot tell as the view is stretched.
<ImageButton
android:id="#+id/apModeInfoButton"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1.15"
android:clickable="true"
android:background="#drawable/info_button_selector" />
I used both of these versions of the XML file as well as different combinations of the images, all of the combinations resulted in the same sized image never changing.
<?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/ic_menu_info_details" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/ic_menu_info_details" /> <!-- focused -->
<item android:drawable="#drawable/ic_menu_info_details" /> <!-- default -->
</selector>
<?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/ic_menu_info_details2" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/ic_menu_info_details2" /> <!-- focused -->
<item android:drawable="#drawable/ic_menu_info_details2" /> <!-- default -->
</selector>
Set that drawable to the ImageButton background rather than the src.
<ImageButton
android:id="#+id/apModeInfoButton"
android:layout_width="0dip"
android:background="#drawable/info_button_selector"
android:layout_height="match_parent"
android:layout_weight="1.15"
android:clickable="true" />
Edit to follow up the distortion problem:
Well what you can do is to have the layers separate.
Because the background is stretchable, set this to the BACKGROUND and separate the image which shouldn't stretch and set this to the SRC.
<ImageButton
android:id="#+id/apModeInfoButton"
android:layout_width="0dip"
android:src="#drawable/non_scaleable_image"
android:background="#drawable/gradient_bg"
android:layout_height="match_parent"
android:layout_weight="1.15"
android:clickable="true" />
This way, you could have a background as a gradient and have it stretch without loosing quality, and the SRC image would not resize and distort.
That is really what an ImageButton should be used for.
Alternatively, do what I suggested in the first answer, but use a Button instead. Let me know if this helps.
You should try using a completed different image for details2 and see if the image is in fact changing, but not being re-sized. I wouldn't expect a stateful button to re-size, maybe use two different images and set visibility.
Related
I would like to adding some effects like color change to my android button when user focus/click the button
My android button layout is here :
<Button
android:id="#+id/btnUpload"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#color/btn_bg"
android:text="தகவல் அனுப்புக"
android:textColor="#color/white"
android:padding="20dp"
android:margin="20dp"
android:layout_alignParentBottom="true" />
You should check out the documentation regarding color state lists.
Create an XML file in your drawable folder with the name of btn_background and place the following code in the file:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
After you have created this file, change the background attribute of your Button to point to the new background:
android:background="#color/btn_background"
Good luck and happy coding!
I know when a button(without background) is clicked, it changes its color and then flow goes to its onClickListner. But when i set an Image to the Button, it is not the same. I want may Image in Button to brighten up a little so that it will look nice.
I know it has something to do with styling but dont know what style to use,
below is my xml code for the button.
<Button
android:id="#+id/supplier"
style="?android:attr/buttonStyleSmall"
android:layout_width="160dp"
android:layout_height="60dp"
android:layout_alignLeft="#id/two_player"
android:layout_alignTop="#id/two_player"
android:layout_marginTop="75dp"
android:background="#drawable/supplier" />
Create one buttonselector in drawable folder then set it button background
btnselector.xml
<?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/btn_bg_clicked" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/btn_bg" /> <!-- focused -->
<item android:drawable="#drawable/btn_bg" /> <!-- default -->
</selector>
Now set this to button background as like
<Button
android:id="#+id/supplier"
style="?android:attr/buttonStyleSmall"
android:layout_width="160dp"
android:layout_height="60dp"
android:layout_alignLeft="#id/two_player"
android:layout_alignTop="#id/two_player"
android:layout_marginTop="75dp"
android:background="#drawable/btnselector" />
i think you got it ...
You have to use a Selector to achieve that.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active tab -->
<item
android:state_selected="true"
android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/tab_bg_selected" />
<!-- Inactive tab -->
<item
android:state_selected="false"
android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/tab_bg_unselected" />
<!-- Pressed tab -->
<item
android:state_pressed="true"
android:drawable="#drawable/tab_bg_pressed" />
<!-- Selected tab (using d-pad) -->
<item
android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="#android:color/transparent" />
</selector>
Supposing the above file is called bg_selector, you set it as the background of the object you want to brighten, and in your case the 'Pressed tab' is what you're looking for. In my 'tab_bg_pressed' you would define another drawable where you would set the background as a brighten effect or whatever you want.
Android Studio 0.3.7
Hello,
I have created 2 buttons png and patched using draw9patch.
The buttons will indicate whether the buttons is pressed or unpressed.
I have the following buttons_colours.xml in my values directory
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/rd_btn_press"/> <!-- pressed -->
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/rd_btn"/> <!-- unpressed -->
<!-- Default -->
<item android:drawable="#drawable/rd_btn"/>
</selector>
In my layout for the button I have this:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="#+id/button"
android:layout_gravity="center_horizontal"
android:background="#drawable/rd_btn"/>
Problem 1: I get an element selector must be declared in my buttons_colours.xml
Problem 2: Not sure if this is related to problem 1, but the button never changes to my rd_btn_press state when I press it.
Many thanks for any suggestions,
you have set wrong background for the button. Replace #drawable/rd_btn with `buttons_colours :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="#+id/button"
android:layout_gravity="center_horizontal"
android:background="#drawable/buttons_colours"/>
also, I don't know why you have two states in your items in the selector. Here an example of working selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/rd_btn_press"
android:state_pressed="true" />
<item android:drawable="#drawable/rd_btn"
android:state_focused="true" />
<item android:drawable="#drawable/rd_btn" />
</selector>
As the title, in a button I set a resource in the background (a square image).
The problem is that the button is now much higher than the classical buttons, how do I make it as high as the others?
I tried with 9.patch but have the same result...
This is the 9patch file (only the angle is important)
Code of the button :
<Button
android:id="#+id/buttonLogIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/button"
android:text="Log In"
android:textColor="#FFFFFF" />
Code of the resource :
<?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/bottonep" /> <!-- pressed -->
<item android:drawable="#drawable/bottone" /> <!-- default -->
</selector>
i've solved with this
the ninepath must be smallest
<?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/btn_default_pressed_holo_dark" /> <!-- pressed -->
<item android:drawable="#drawable/btn_default_normal_holo_dark" /> <!-- default -->
</selector>
I want to assign an image to one of the buttons in my activity?
How can i achieve that??
And for that where shall I place image.jpeg or any other image file?
Use ImageButton.
Put your image in resources, and use it 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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
its pretty old question but seems like you didn't get best solution on this post because still haven't accepted any answer.
So if we need a "button with Image" then we have two options
either we can go for Image Button as 'Orsol' mentioned in his answer
or simply you can Add Normal Button and then set Image to Background using Properties
code will be like this
<Button
android:background="#drawable/square_1_up"
android:layout_height="62dp"
android:layout_width="65dp"
android:id="#+id/button1">
</Button>
for this we need to put button image in Drawable folder named "square_1_up.png"
output Button will be like
Save these file under res/drawable/button.xml
<?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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
Suppose your main layout file main.xml which is under res/layout/main.xml
<LinearLayout>
<ImageButton
android:src="#drawable/button.xml"
android:layout_width="fill_parent"
android:layout_heght="wrap_content"
android:id="#+id/button1"
/>
</LinearLayout>