-3

I am implementing navigation drawer, I need to handle an onClick method from a fragment, how can I achieve this?. In Mainactivity.java this can be performed easily but when I use fragments it's not possible to use findViewbyid?

Thank You

Julian Schmuckli
  • 3,681
  • 11
  • 37
  • 64
Srinidhi Kowshik
  • 21
  • 1
  • 2
  • 4

1 Answers1

2

In fragment, you can simply use View to handle buttons in Fragment.

View view = inflater.inflate(R.layout.yourFragment, container, false);
myButton = (Button) view.findViewById(R.id.myButton);
myButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        //Some code...
    }
});
return view;
Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
Cagri Yalcin
  • 402
  • 4
  • 13