'How use a button inside a fragment?

I'm trying to use 4 fragments

[F1]

[F2][F3][F4]

F1 will have 3 buttons to change between F2,F3 and F4 but i don´t know how to use the buttons properties like in a normal Activity

That's what i have in my F1 code

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    return inflater.inflate(R.layout.fragment_01, container, false);

}


Solution 1:[1]

Check this :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view = inflater.inflate(R.layout.fragment_01, container, false);
    // here you have the reference of your button
    Button yourButton = (Button)view.findViewById(R.id.your_button_id);
    return view;

}

Solution 2:[2]

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_01, container, false);
    Button firstButton = (Button)view.findViewById(R.id.firstbutton);

    Button seconButton = (Button)view.findViewById(R.id.seconbutton);

    Button thirdButton = (Button)view.findViewById(R.id.thirdbutton);
    return view;

}

Solution 3:[3]

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_one, container, false);


        Button button = (Button) rootView.findViewById(R.id.buttonSayHi);
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                onButtonClicked(v);
            }
        });
        return rootView;
    }

  public void onButtonClicked(View view)
  {
          //do your stuff here..           
    final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
    ft.replace(R.id.frameLayoutFragmentContainer, new FragmentTwo(), "NewFragmentTag"); 
    ft.commit(); 

    ft.addToBackStack(null);    
  }
}

Solution 4:[4]

It's a simple way to reference a button or any widget in a fragment.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fragment_one, container, false);

        btnOne = view.findViewById(R.id.btnOne);

return view;

Solution 5:[5]

You should get the view first, and then get the button from the view like this:

View rootView = inflater.inflate(R.layout.fragment_01, container, false);
Button btn = (Button) rootView.findViewById(R.id.btn);

You should do some tutoriasl first, its not that hard.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 diegoveloper
Solution 2 Sagar Patel
Solution 3 Niranjan
Solution 4 Pankaj Mundra
Solution 5 Sule