Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

findViewById in specific layout?

Tags:

android

Instead of prefixing id's in xml, is it possible to specify the particular layout in code? For example, if I have 3 layouts, each with a button that has an id of "btn". Is it possible to specify which layout to findViewById(R.id.btn) in?

like image 838
Roger Avatar asked Dec 05 '25 13:12

Roger


1 Answers

The basic context is defined via setContentView(R.lyaout.my_layout). If you inflate another layout using LayoutInflater.inflate() you get a layout object, lets call it buttonLayout. You can now differ between this.findViewById(R.id.button) and buttonLayout.findViewById(R.id.button) and both will give you different button references.

like image 132
WarrenFaith Avatar answered Dec 08 '25 01:12

WarrenFaith



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!