0

I have a layout with view pager and tabs layout. Viewpager contains two layouts for fragments fragmentpage1 and fragmentpage2. In fragmentpage1.xml i have an imageview who's id is "myimgbtn"

And i have the following code in MainActivity.java :

Private FragmentPage1Binding mfpb;
..
@Override
Protected void oncreate(...){
..
mfpb=FragmentPage1Binding.inflate(getLayoutInflater());
 ImageView myimage=mfpb.myimgbtn;
myimage.setOnclicklistener(v->v.setVisibility(View.Gone););
..

But nothing happens when i click the image,any solutions ?

It works when i use attribute onclick in coresponding xml layout file.

Issasafar
  • 13
  • 2
  • Show the full code. From your limited snippet, you're inflating a random layout and setting a listener on it but not actually adding anything to the activity or fragment. – dominicoder Apr 21 '23 at 18:26
  • Did you debug to see if the listener is being called? – m0skit0 Apr 21 '23 at 18:31
  • Please provide enough code so others can better understand or reproduce the problem. – Chris Pi Apr 22 '23 at 10:35

1 Answers1

0

Finally i found the solution. In brief i was calling setContentView on mainlayout and so there is no reference to views contained inside the fragments that it holds. Here's the solution: ViewBinding in Fragment
But the question is what was myimage referring to?

Mattia Righetti
  • 1,265
  • 1
  • 18
  • 31
Issasafar
  • 13
  • 2