I have created a layout that contains two buttons, Next and Previous. In between the buttons I'm generating some dynamic views. So when I first launch the application I want to disable the "Previous" button since there wont be any previous views. I also want to disable the "Next" button when there are not more views to display. Is there anyway to disable the buttons?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
In my case,
is working fine and it is enabling and disabling the button as it should. But once the button state becomes disabled, it never goes back to the enabled state again, although it's clickable. I tried invalidating and refreshing the drawable state, but no luck.
If you or anyone having a similar issue, what works for me is setting the background drawable again. Works on any API Level.
Yes it can be disabled in XML just using
<Button android:enabled="false" />
Did you try this?
Update: Thanks to Gwen. Almost forgot that
android:clickable
can be set in your XML layout to determine whether a button can be clickable or not.You can't enable it or disable it in your XML (since your layout is set at runtime), but you can set if it's clickable at the launch of the activity with
android:clickable
.first in xml make the button as
android:clickable="false"
then in your code, inside
oncreate()
method set the button property asthen inside the button click change the code into
In Java, once you have the reference of the button:
To enable/disable the button, you can use either:
Or:
Since you want to disable the button from the beginning, you can use button.setEnabled(false); in the onCreate method. Otherwise, from XML, you can directly use:
So: