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.
Related
My button has 3 states and the representation of each depends on a particular condition. (You can follow this question I've asked earlier to see the screenshots of what states my button has):
Different background for a button's states in Kotlin
I want my button to kind of "block" its pressed state if it is not clickable.
My button's states:
<?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/background_stroke_bluish_rounded_corners_2dp"/>
<item android:state_pressed="false"
android:drawable="#drawable/background_bluish_rounded_corners" /> <!-- pressed -->
<item android:state_pressed="true"
android:drawable="#drawable/background_dark_teal_rounded_corners" /> <!-- focused -->
<item android:state_enabled="true" android:drawable="#drawable/background_bluish_rounded_corners"/>
</selector>
How I tried to set the isClickable to false:
updateFragmentView?.mFragmentRootView?.btnUpdate?.isClickable = false
Right now it works and the needed event while isClickable = true doesn't happen, but what I want to implement is not to let the pressed state become visible if the button is not clickable. Is that possible to implement without changing the XML?
Something alike can be found in isUserInteractionEnabled for iOS. If set to false, the button's highlighted color doesn't work (if the button has one).
UPD: I've tried setEnabled(false) and also .isEnabled = true, but that does influence on the logic of my fragment, so that's not working for me.
I am writing an Android app and now I am styling it. I am using a custom theme which is a child of Theme.Holo.Light. I really like Theme.Holo.Light because its buttons have a special effect when you click and hold it. Like the lower button in the picture below:
Not click:
click:
The upper button has been changed color. Now when I click on that button, it doesn't have that effect. I really don't understand why. Can anyone tell me why this happens and how can I get the same effect with a colored button?
And also, the colored button seems fatter.
This is because the button uses a selector to display different colors/effects/drawables based on the state of the click. You can check out the link on Color State List Resource.
To create your own you have to create a slecetor cml file and put it in your drawables folder.
For example.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shape_btn_default_normal_gray" android:state_enabled="true" android:state_pressed="false"/>
<item android:drawable="#drawable/shape_btn_default_pressed_gray" android:state_pressed="true"/>
<item android:drawable="#drawable/shape_btn_default_disabled_gray"/>
</selector>
or with colors
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/dark_green" android:state_enabled="true" android:state_pressed="false"/>
<item android:drawable="#color/light_green" android:state_pressed="true"/>
<item android:drawable="#color/gray"/>
</selector>
To apply this you have to set the background drawable in your layout xml like this.
<Button
android:id="#+id/my_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Some text"
android:background="#drawable/selector_btn_default_gray"/>
That is the "ripple effect" of material design.You have define you own style for that effect.Link below may help you or you may search for many other answers on StackOverflow. Material effect on button with background color
It does not loses its behavior you can see after click (in your second image) the button show same scale as the above have...so by default the background is set as to show that it is button (like with padding or so) and can changes to show oncklick effect...
So when you set your desire background to button...It takes complete change on what is on presently on it and then you have to manually set onclick effect..
I have a ListView and a Button. The button should always be clickable, but the button background image should be disabled(grayed) when the ListView is empty and enabled(actual background) when the ListView has items.
I know this can be achieved from code by always setting the state enabled as true and changing the background image. But I am looking to achieve this in XML using selector.
Take a look at the following code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:constantSize="true" android:dither="true">
<item android:drawable="#drawable/button_enabled" android:state_enabled="true" />
<item android:drawable="#drawable/button_disabled" android:state_enabled="false" />
</selector>
Use this code in your drawables folder (as an XML resource).
Afterwards you use this drawable and set it as a background property to your button. (android:background="#drawable/your_selector_file" without the .xml extension, of course)
If you need more information just consult the following link: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
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.
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