I have used a selector file for ImageButton but the image which I have defined onPress is shown for only a very short period of time. I want that it will be visible until the next window opens.
Code:
<?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/bedpres_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/bedpres_pressed" /> <!-- focused -->
<item android:drawable="#drawable/bedpres" /> <!-- default -->
</selector>
You need to use ToggleButton in that case with the selector you have.
Button is considered pressed only between the ACTION_DOWN and ACTION_UP touch event actions, so if you want your button to change picture after it's been touched, you should make it manually, calling button.setBackgroundResource(R.drawable.bedpres_pressed); Hope this helps.
After Button Cliked i think you wrote in xml file for only click
so if you want your button to change picture after it's been touched, you try to change manually, calling button.setBackgroundResource(R.drawable.bedpres_pressed);
other wise try to use Toggle Button class
hope this helps.
Related
I have a button where the selector is like
<?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_circle_selected"/>
<item android:drawable="#drawable/bg_circle_disabled" />
</selector>
So when I click the button, the background will show a red colored circle.
I need to disable this button based on condition, so the highlight should not be shown.
If I so it as setEnabled false it will work
But there is one more case where the disabled button should give auditory feedback.
So when I give setEnabled as false the other requirements will not work because touch is disabled.
Is there any method to disable the button other than setEnabled ()?
You can achieve the above by custom selector where the button appears to be disabled but the fact it is still enabled and trigger click actions.
you can use
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:drawable="#drawable/bg_circle_disabled />
<item android:state_pressed="true" android:drawable="#drawable/bg_circle_disabled"/>
<item android:state_enabled="false" android:drawable="#drawable/bg_circle_disabled" />
also don't forget to keep track to the button status (enable/disabled)
You might want to consider setting the attributes of the button programmatically from the java file, not XML.
Give the button an id from your XML layout file, then reference it from the java file. In that way, you will have more control over how it behaves.
For example, android:id="#+id/my_button" in the XML.
Then Button button = findViewById(R.id.my_button); in the onCreate method.
Afterward, give it whatever attributes you want.
Ok I am having a LOT of trouble finding an answer for this question. So either I'm asking it wrong or going about it the completely wrong way. Either way I could use some help. So what I'm trying to do is implement dynamic image cycling on a button widget based on state.
What I mean by this is
On Mouse Down {
Change to this image
}
On Click {
Button functionality
}
On Mouse Up {
Change back to original
}
I've seen a LOT of tutorials that place this under the click event but this isn't really what I want. I don't just simply want to change the color of the button when the button is pressed I want it to flash with the alternate image quickly and then Change back to the Original image placed on the button. How would I go about doing this?
Take a look at this link: https://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
From the Link:
EXAMPLE:
XML file saved at 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:state_hovered="true"
android:drawable="#drawable/button_focused" /> <!-- hovered -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
This layout XML applies the state list drawable to a Button:
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="#drawable/button" />
I have an image button (star) which is used to mark something as favorite. I envision that when the user clicks on the star, the star will turn yellow. When they click on an already yellow start, it will go back to normal.
Transition from one color to the other would make a call to the server. I am doing that part already.
To change the color on click I did this.
<?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/ic_action_fav" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/ic_action_fav" />
<item android:drawable="#drawable/ic_action_ic_action_star" />
</selector>
However, this changes the color only for the time being when the buttons is clicked. It doesn't remain changed on the click, in other words, it doesn't toggle.
How can I toggle the color of a button on each click?
Use android:state_selected in the state list along with View.setSelected(boolean selected) in your Java code.
I'm trying to implement media player controls into my notification. I need the play/pause button to change between the "play" drawable and the "pause" drawable dynamically (basically on the user's touch). For example, as soon as the user touches the "pause" button, it needs to change to "play. When the user touches the "play" button, it needs to change back to "pause".
I'm assuming the best way to do this is by creating a StateListDrawable XML and setting it as the action button's drawable. Unfortunately, I can't seem to get the StateListDrawable to work. Here's my XML file for the drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_first="true"
android:drawable="#drawable/pause_track_notification" />
<item android:state_last="true"
android:drawable="#drawable/play_track_notification" />
</selector>
Am I missing anything important in the XML? Right now, I'm just getting an empty space where the play/pause button is supposed to show up. How do I get this to work? Thanks!
Use this for a Toggle button and the following selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true"
android:drawable="#drawable/pause_track_notification" />
<item android:state_checked="false"
android:drawable="#drawable/play_track_notification" />
<item android:drawable="#drawable/play_track_notification" /> <!-- the default -->
</selector>
By default when button is clicked something like orange color will surround the button for short time, that indicates buttons is clicked. But this feature is not working when button contains background image. This is happening in list view too.why ? Any Ideas? TIA
I used setBackgroundColor(Color.BLUE); But here the color is applied and not gone...
You need to use a selector as your background 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/button_pressed" />
<item android:state_focused="true" android:drawable="#drawable/button_focus" />
<item android:drawable="#drawable/button_normal" />
</selector>
This selector makes use of 3 separate drawables to change the image of the button , you may use the same image for pressed and focuses.
You need to put this XML into your drawables directory and use it as a background for your button.
For another solution refer : Standard Android Button with a different color
i too had the same problem. so instead of setting the background color,i included three images of button in three different colors , so based on state focused,pressed,default the respective image will replace the other. and it looks like change in the color of the button.
**<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/c2"
android:state_pressed="true" />
<item android:drawable="#drawable/c1"
android:state_focused="true" />
<item android:drawable="#drawable/c1" />
</selector>**
above is the content of the xml file,which must be assigned to the background of the respective button
android:background="#drawable/drawable_button
hope this might be helpful for you