Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Button inside Activity from Fragment

Tags:

java

android

How to access Button inside Activity from Fragment. Actually I set myButton is disabled but when some of my radioButtons was clicked i want to set myButton enabled?

like image 313
eJoe Avatar asked Nov 29 '25 16:11

eJoe


1 Answers

There are a few things you could try. The first one is to use getActivity().findViewByIt(id) to get the Button, which I think should work but it's not a good solution from the engineering side of things since your Fragment would make assumptions about the Activity layout that hosts it. The second would be to provide a callback interface as described here. Just call the callback method every time the checkbox changes state. This approach is better since the Fragment can be freely re-used by any activity that implements the callback interface. Last but not least, you should try to put the Button inside the fragment if at all possible.

like image 178
ebarrenechea Avatar answered Dec 02 '25 06:12

ebarrenechea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!